Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Anonymous (not verified) on
I created a new field based on taxonomy field 'taxonomy_term_reference':
$field = array('field_name' => 'field_category', 'type' => 'taxonomy_term_reference');
field_create_field($field);
then, an instance of this field to content type 'test':
$instance = array(
'field_name' => 'field_category',
'entity_type' => 'node',
'label' => 'Category',
'bundle' => 'test',
'required' => true,
'widget' => array(
'type' => 'options_select'
),
'display' => array(
'default' => array('type' => 'hidden'),
'teaser' => array('type' => 'hidden')
)
'settings' => array(
'allowed_values' => array(
array('vid' => 1, 'parent' => 0)
)
)
);
field_create_instance($instance);
The field successfully appeared in the node insert form, but when this select box drops down, there's nothing inside (vocabulary with vid = 1 is existed with several sub terms). To make it work, I have to go to Field UI and manually re-select the Vocabulary of this field (vid = 1 again).
Someone experienced this problem already, pls help me, thank you...
Comments
Try this
Late in the day, but here's how I got it working:
and
THANK YOU!
I was going crazy. I wish there were better docs for this kind of stuff.
Thank you so much!
Apparently, they made the same mistake in the book «Drupal 7 Development», page 538... their code would lead to an error like «...undefined index: vocabulary...». I posted an erratum, I hope they will publish it.
Looks like the error is still
Looks like the error is still in my book at least how do I fix it? Am I overlooking it ???
Freelance Drupal developer, contact me if you want help on your Drupal project.
How t use?
How to use this code in a module?
Great example
I really appreciate your example. Was exactly what I was looking for!
You may also want to check...
@jordan thank you for sharing!
You guys may also want to check this to documents/issues:
http://drupal.org/node/1286658#comment-5073652
http://api.drupal.org/api/drupal/modules!field!field.module/group/field/7
In some cases it is better to use 'instance_settings' instead of 'settings'
I Wanted to add some more info here, just in case someone get stuck as I did!
Stumbled across this.. Do NOT
Stumbled across this.. Do NOT use 'instance_settings' instead of 'settings'. It simply doesn't work!