You are here

function content_taxonomy_field in Content Taxonomy 6.2

Same name and namespace in other branches
  1. 5 content_taxonomy.module \content_taxonomy_field()
  2. 6 content_taxonomy.module \content_taxonomy_field()

Implementation of hook_field().

File

./content_taxonomy.module, line 154
Defines a field type for referencing a taxonomy term.

Code

function content_taxonomy_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'presave':
      if ($field['save_term_node']) {
        static $_content_taxonomy_array_cleared;
        if (!is_array($_content_taxonomy_array_cleared) || !$_content_taxonomy_array_cleared[$node->nid][$field['vid']]) {
          _content_taxonomy_taxonomy_unset($node->taxonomy, array(
            $field['vid'],
          ));
          $_content_taxonomy_array_cleared[$node->nid][$field['vid']] = TRUE;
        }
        foreach ($items as $key => $entry) {
          if ($entry['value']) {
            if (is_object($node->taxonomy[$entry['value']]) || is_array($node->taxonomy) && in_array($entry['value'], $node->taxonomy) || isset($entry['_remove']) && $entry['_remove'] == 1) {
              continue;
            }
            elseif (is_array($node->taxonomy[$field['vid']])) {
              if (!in_array($entry['value'], $node->taxonomy[$field['vid']])) {
                $node->taxonomy[$field['vid']][] = $entry['value'];
              }
            }
            else {
              $node->taxonomy[$field['vid']] = array(
                $entry['value'],
              );
            }
          }
        }

        // the $node->taxonomy array should never be empty, because in this case the
        // taxonomy nodeapi doesn't call taxonomy_node_save which handles removing
        // and inserting of terms
        if (empty($node->taxonomy)) {
          $node->taxonomy[$field['vid']] = NULL;
        }
        if ($node->nid) {

          // Remove old associations of a node to its terms
          db_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid);
        }
      }
      break;
  }
}