taxonomy_preview_terms

Definition

taxonomy_preview_terms($node)
modules/taxonomy/taxonomy.module, line 571

Description

Helper function to convert terms after a preview.

After preview the tags are an array instead of proper objects. This function converts them back to objects with the exception of 'free tagging' terms, because new tags can be added by the user before preview and those do not yet exist in the database. We therefore save those tags as a string so we can fill the form again after the preview.

Code

<?php
function taxonomy_preview_terms($node) {
  $taxonomy = array();
  if (isset($node->taxonomy)) {
    foreach ($node->taxonomy as $key => $term) {
      unset($node->taxonomy[$key]);
      // A 'Multiple select' and a 'Free tagging' field returns an array.
      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($key == 'tags') {
            // Free tagging; the values will be saved for later as strings
            // instead of objects to fill the form again.
            $taxonomy['tags'] = $term;
          }
          else {
            $taxonomy[$tid] = taxonomy_get_term($tid);
          }
        }
      }
      // A 'Single select' field returns the term id.
      elseif ($term) {
        $taxonomy[$term] = taxonomy_get_term($term);
      }
    }
  }
  return $taxonomy;
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.