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

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

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.