Хорошо бы пример кода найти:
Для текстовых полей и файлов есть решение, для таксономии гугл не помог.
Что-то вроде этого должно быть?
<?php
function _node_example_installed_fields() {
return array(
'node_example_color' => array(
'field_name' => 'node_example_color',
'cardinality' => 3,
'type' => 'text',
),
'node_example_image' => array(
'field_name' => 'node_example_image',
'type' => 'image',
'cardinality' => 1,
),
);
}?>
Комментарии
если понадобиться, примерно так работает:
MYMODULE.install
<?php function MYMODULE_install()
{
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['MYMODULE']);
add_custom_fields();
}
function
MYMODULE_uninstall() {$ournewtype = 'MYMODULE';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $ournewtype));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
delete_custom_fields();
node_type_delete($ournewtype);
field_purge_batch(500);
}
function
_MYMODULE_installed_fields() {$t = get_t();
return array(
'MYMODULE_price' => array(
'field_name' => 'MYMODULE_price',
'label' => $t('Price of the product'),
'type' => 'text'
),
'MYMODULE_quantity' => array(
'field_name' => 'MYMODULE_quantity',
'label' => $t('Quantity of the product'),
'type' => 'text'
),
'MYMODULE_image' => array(
'field_name' => 'MYMODULE_image',
'type' => 'image'
),
'MYMODULE_tag' => array(
'field_name' => 'MYMODULE_tag',
'type' => 'taxonomy_term_reference',
'label' => $t('Label'),
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => "tags",
'parent' => 0
)
)
)
)
);
}
function
_MYMODULE_installed_instances() {$t = get_t();
return array(
'MYMODULE_price' => array(
'field_name' => 'MYMODULE_price',
'type' => 'text',
'label' => $t('Price of the product'),
'widget' => array(
'type' => 'text_textfield'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Price of the product'),
'type' => 'text'
)
)
),
'MYMODULE_quantity' => array(
'field_name' => 'MYMODULE_quantity',
'type' => 'text',
'label' => $t('Quantity of the product'),
'widget' => array(
'type' => 'text_textfield'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Quantity of the product'),
'type' => 'text'
)
)
),
'MYMODULE_image' => array(
'field_name' => 'MYMODULE_image',
'label' => t('Upload an image:'),
'required' => FALSE,
'widget' => array(
'type' => 'image_image',
'weight' => 2.10,
),
'display' => array(
'example_node_list' => array(
'label' => 'hidden',
'type' => 'image_link_content__thumbnail',
),
),
),
'MYMODULE_tag' => array(
'field_name' => 'MYMODULE_tag',
'label' => t('Select the tag'),
'required' => TRUE,
'widget' => array(
'type' => 'taxonomy_autocomplete',
'weight' => 2.10,
),
'display' => array(
'MYMODULE_list' => array(
'label' => 'hidden',
'type' => 'taxonomy_term_reference',
),
),
)
);
}
function
add_custom_fields(){
foreach (_MYMODULE_installed_fields() as $field) {
field_create_field($field);
}
foreach (_MYMODULE_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'MYMODULE';
print_r($fieldinstance);
field_create_instance($fieldinstance);
}
}
function
delete_custom_fields() {foreach (array_keys(_MYMODULE_installed_fields()) as $field) {
field_delete_field($field);
}
$instances = field_info_instances('node', 'MYMODULE');
foreach ($instances as $instance_name => $fieldinstance) {
field_delete_instance($fieldinstance);
}
}
?>