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

jdelaune’s picture

Late in the day, but here's how I got it working:

<?php
$field = array(
  'field_name' => 'field_category',
  'type' => 'taxonomy_term_reference'
  'settings' => array(
    'allowed_values' => array(
      array(
        'vocabulary' => $vocabulary->machine_name,
        'parent' => 0
      ),
    ),
  ),
);

field_create_field($field);
?>

and

<?php
$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')
    )
);

field_create_instance($instance);
?>
vivefree’s picture

I was going crazy. I wish there were better docs for this kind of stuff.

FMB’s picture

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.

scuba_fly’s picture

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.

rta’s picture

How to use this code in a module?

puddyglum’s picture

I really appreciate your example. Was exactly what I was looking for!

TuWebO’s picture

@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'

<?php
$field = array(
  'field_name' => 'field_category',
  'type' => 'taxonomy_term_reference'
  'instance_settings' => array(
    'allowed_values' => array(
      array(
        'vocabulary' => $vocabulary->machine_name,
        'parent' => 0
      ),
    ),
  ),
);

field_create_field($field);
?>

I Wanted to add some more info here, just in case someone get stuck as I did!

Thomas Cys’s picture

Stumbled across this.. Do NOT use 'instance_settings' instead of 'settings'. It simply doesn't work!