[Drupal] How to create custom field values in taxonomy terms

By | 25th Feb 2015 | 1 min read

Drupal 7 provides a flexible supports to create a Taxonomy. There is powerful core module called Taxonomy which allows us to create, manage and apply vocabularies. In Drupal, the terms are gathered within "vocabularies". Taxonomy has a ability to add taxonomy fields to vocabularies and terms. When we come across inserting the taxonomy terms programmatically, we will need to add fields values to that taxonomy term. So In this section we are going to create a custom field values to the taxonomy terms. Below is the code snippet, that is the array structure to create field values.

  
    <php
    $terms['field_yourfield_name'] = array(
          'und' => array(
            '0' => array(
              'value' => 'Field_value',
            ),
          ),
        );
    >
  

The above array structure can also be rewritten as:

  
    <php
      $terms['field_yourfield_name']['und'][0]['value'] = 'Field_value';
    >
  

You can save this taxonomy terms field values by passing this array into the taxonomy_term_save() function, like below.

  
    <php
      $terms['field_yourfield_name']['und'][0]['value'] = 'Field_value';
    >