Programatically adding taxonomy terms as a Hierachy

Submitted by nmeegama on Fri, 09/05/2014 - 21:00

Problem

We can programatically add taxonomies, thats easy but adding it as a child term is the hard part

Solution

We need to fist create a validation function dedicated for the term reference filed 

$form[TAG_FIELD_NAME]['#element_validate'][] = 'taxonomy_field_tags_validate';

Next we need the Function. Use the following code block

/**
 * Valiadtor for field field_cl_tags
 * @param $element
 * @param $form_state
 */
function taxonomy_field_cl_tags_validate(&$element, &$form_state) {
  $value = array();
  //if the hidden taxonomy field is
  //location.citytown->Paris,location.country->France
  if (isset($form_state['input']['contentlinks_tags']) && !empty($form_state['input']['contentlinks_tags'])) {
    //format : [location.citytown->Paris,location.country->France]
    $tag_value_array = explode(',', $form_state['input']['contentlinks_tags']);
    foreach ($tag_value_array as $pair) {
 
      $pair_array = explode('->', $pair);
      $parent = $pair_array[0];
      $child = $pair_array[1];
      //if the parent is not Empty
      //Start processing the parent term
      if ($parent != CONTENTLINKS_ENTITY_TYPE_EMPTY) {
        //load the vocabulary cl_tags
        $vocabulary = taxonomy_vocabulary_machine_name_load(CONTENTLINKS_TAXONOMY_MACHINE_NAME);
        $vid = $vocabulary->vid;
        //check if the parent term is already available
        $parent_obj = taxonomy_get_term_by_name($parent, CONTENTLINKS_TAXONOMY_MACHINE_NAME);
        //if parent term is not available
        if (empty($parent_obj)) {
          $parent_term = new stdClass();
          $parent_term->name = $parent;
          $parent_term->vid = $vid;
          //build and save the taxonomy term
          taxonomy_term_save($parent_term);
          $parent_obj = taxonomy_get_term_by_name($parent, CONTENTLINKS_TAXONOMY_MACHINE_NAME);
 
 
        }
        //convert the parent_obj from an array to a stdObj
        if (is_array($parent_obj)) {
          $parent_obj_keys = array_keys($parent_obj);
          $key = array_pop($parent_obj_keys);
          $parent_obj = $parent_obj[$key];
        }
        //end processing the parent term
        //Start processing the child term
        $child_obj = taxonomy_get_term_by_name($child, CONTENTLINKS_TAXONOMY_MACHINE_NAME);
        $child_parents_array = array();
        if (is_array($child_obj) && !empty($child_obj)) {
          $child_obj_keys = array_keys($child_obj);
          $key = array_pop($child_obj_keys);
          $child_obj = $child_obj[$key];
          //get the current parents of the child term
          $child_parents = taxonomy_get_parents($child_obj->tid);
          //get the tid array of the parents
          $child_parents_array = array_keys($child_parents);
        }
 
        if (empty($child_obj) || !in_array($parent_obj->tid, $child_parents_array)) {
          $child_term = new stdClass();
          $child_term->name = $child;
          $child_term->vid = $vid;
          $child_term->parent = $parent_obj->tid;
          taxonomy_term_save($child_term);
          $child_obj = taxonomy_get_term_by_name($child, CONTENTLINKS_TAXONOMY_MACHINE_NAME);
          if (is_array($child_obj)) {
            $child_obj_keys = array_keys($child_obj);
            $key = array_pop($child_obj_keys);
            $child_obj = $child_obj[$key];
          }
 
        }
 
        $child_array = (array) $child_obj;
        $value[] = $child_array;
      }
    }
  }
  form_set_value($element['und'], $value, $form_state);
}