Fix for Function to add taxonomy and content_taxonomy to a node object

  1. /**
  2.  * Modifies a node object adding taxonomy terms and, optionally, modifying a
  3.  * content_taxonomy type field.
  4.  *
  5.  * @param <type> $node
  6.  *   A node object.
  7.  * @param <type> $vid
  8.  *   The vocabulary ID for the tags.
  9.  * @param <type> $tags
  10.  *   An array of strings to use for the term names.
  11.  * @param <type> $not_found_action
  12.  *   Action to take when terms are not found. Options are: 'ignore' (default)
  13.  *   which will simply not insert that term into the node, 'create' will create
  14.  *   new taxonomy terms, and 'other' will assign the term 'other' for a term that
  15.  *   was not found.
  16.  * @param <type> $fieldname
  17.  *   Optional; a content_taxonomy field machine name. E.g. field_my_tags
  18.  * @return
  19.  *   A node object that can be stored in the database using node_save().
  20.  */
  21. function my_nodeobject_add_content_taxonomy(&$node, $vid, $tags = array(), $not_found_action = 'ignore', $fieldname = NULL) {
  22.   static $translate, $errors;
  23.   if (empty($translate)) {
  24.     // The following contains an array of translation rules. For instance,
  25.     // If you'd like to convert an incoming terms 'en' and 'eng' into 'english'
  26.     // for the vocabulary ID 6, write:
  27.     //    $translate[6] = array('en' => 'english', 'eng' => 'english');
  28.     $translate = array(
  29.       29 => array(
  30.         'en' => 'english',
  31.         'sp' => 'spanish',
  32.       ),
  33.     );
  34.   }
  35.  
  36.   // Convert a single tag in a string to a one-element array.
  37.   if (!is_array($tags)) {
  38.     $tags = array($tags);
  39.   }
  40.  
  41.   // Cycle through each given tag
  42.   foreach ($tags as $term_name) {
  43.     $term_name = trim($term_name);
  44.     if ($term_name == "") continue;
  45.  
  46.     // Translate if necessary.
  47.     if (isset($translate[$vid][$term_name])) {
  48.       $term_name = $translate[$vid][$term_name];
  49.     }
  50.  
  51.     // Get all terms with this name
  52.     $terms = taxonomy_get_term_by_name($term_name);
  53.     if (sizeof($terms) == 0) {
  54.       if ($not_found_action == "use_other") {
  55.         $terms = taxonomy_get_term_by_name("Other");
  56.       }
  57.       elseif ($not_found_action == "create") {
  58.         // Create a new tag with this term
  59.         $term_to_import= array(
  60.           'name' => $term_name,
  61.           'vid' => $vid,
  62.         );
  63.         $ok = taxonomy_save_term($term_to_import);
  64.         $term_tmp = taxonomy_get_term($term_to_import['tid']);
  65.         $terms = array($term_tmp);
  66.       }
  67.       else {
  68.         if (empty($errors[$vid][$term_name])) {
  69.           drupal_set_message("WARNING: Could not find '{$term_name}' in <a href=/admin/content/taxonomy/{$vid}>vid={$vid}</a>");
  70.           // Mark so error is not flagged again
  71.           $errors[$vid][$term_name] = 1;
  72.         }
  73.         continue;
  74.       }
  75.     }
  76.  
  77.     foreach ($terms as $term) {
  78.       if ($term->vid == $vid && $term->parent == 0) {
  79.         if ($fieldname) {
  80.           $node->{$fieldname}[]['value'] = (string)$term->tid;
  81.         }
  82.         $node->taxonomy[$term->tid] = $term;
  83.       }
  84.     }
  85.   }
  86.   if (isset($node->$fieldname) && !is_array($node->$fieldname)) {
  87.     $node->$fieldname[0]['value'] = '';
  88.   }
  89. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.