Hi,

I'm new to programming for Drupal. I'm using Drupal 7, and I want to create a custom content type programmatically. A couple of the fields are taxonomy term references. I haven't been able to find out how to set up these fields when I create my field instances. I would like to use a vocabulary I've defined called 'Artists', and I would like to use the 'Autocomplete term widget (tagging)' widget.

So far I've just got:
'performance_artist' => array(
'field_name' => 'performance_artist',
'label' => $t('Artist or Band'),
'type' => 'taxonomy_term_reference',
'widget' => array(
'type' => '', //no idea what goes here
),

Any help is greatly appreciated, thanks!

Comments

Anonymous’s picture

Figured it out. In case anyone else comes across the same problem, here is how you set it up. You define the vocabulary you want to use when you create the field. In my case I set up a field like this:

'performance_artist' => array(
  'field_name' => 'performance_artist',
  'label' => $t('Artist or Band'),
  'type' => 'taxonomy_term_reference',
  'settings' => array(
    'allowed_values' => array(
      array(
        'vocabulary' => 'artists', 
        'parent' => 0,
       ),
     ),
   ),
),

and then called field_create_field.

To use the autocomplete widget, you define it when you set your field_create_instance

       'performance_artist' => array(
          'field_name' => 'performance_artist',
          'label' => $t('Artist or Band'),
          'type' => 'taxonomy_term_reference',
          'widget' => array(
            'type' => 'taxonomy_autocomplete',            
          ),
        ),
bike2live’s picture

Any chance anyone knows how to provide a select list of current terms in the vocabulary? Surely I don't have to read all of the terms in the vocabulary and create my own list of 'allowed_values"...

nevets’s picture

You would want to change

          'widget' => array(
            'type' => 'taxonomy_autocomplete',           
          ),
richardp’s picture

Fantastic! Just what I was looking for. Thanks for the code.

Yorgg’s picture

Hi.

How would you attach the taxonomy term ("tags") into the content type?

Regards,
Jorge

nevets’s picture

In Drupal 7 you add a taxonomy term reference field

Yorgg’s picture

I'm able to save the term into the vocabulary but not the "tag" term in the term.

Edit: I believe I read that node_save($node) couldn't achieve this in definitive guide drupal 7

In case someone is reading this I am using rules_autotag successfully to do this.