function taxonomy_build_node_index

Same name in other branches
  1. 9 core/modules/taxonomy/taxonomy.module \taxonomy_build_node_index()
  2. 8.9.x core/modules/taxonomy/taxonomy.module \taxonomy_build_node_index()
  3. 10 core/modules/taxonomy/taxonomy.module \taxonomy_build_node_index()
  4. 11.x core/modules/taxonomy/taxonomy.module \taxonomy_build_node_index()

Builds and inserts taxonomy index entries for a given node.

The index lists all terms that are related to a given node entity, and is therefore maintained at the entity level.

Parameters

$node: The node object.

Related topics

2 calls to taxonomy_build_node_index()
taxonomy_node_insert in modules/taxonomy/taxonomy.module
Implements hook_node_insert().
taxonomy_node_update in modules/taxonomy/taxonomy.module
Implements hook_node_update().

File

modules/taxonomy/taxonomy.module, line 1930

Code

function taxonomy_build_node_index($node) {
    // We maintain a denormalized table of term/node relationships, containing
    // only data for current, published nodes.
    $status = NULL;
    if (variable_get('taxonomy_maintain_index_table', TRUE)) {
        // If a node property is not set in the node object when node_save() is
        // called, the old value from $node->original is used.
        if (!empty($node->original)) {
            $status = (int) (!empty($node->status) || !isset($node->status) && !empty($node->original->status));
            $sticky = (int) (!empty($node->sticky) || !isset($node->sticky) && !empty($node->original->sticky));
        }
        else {
            $status = (int) (!empty($node->status));
            $sticky = (int) (!empty($node->sticky));
        }
    }
    // We only maintain the taxonomy index for published nodes.
    if ($status) {
        // Collect a unique list of all the term IDs from all node fields.
        $tid_all = array();
        foreach (field_info_instances('node', $node->type) as $instance) {
            $field_name = $instance['field_name'];
            $field = field_info_field($field_name);
            if ($field['module'] == 'taxonomy' && $field['storage']['type'] == 'field_sql_storage') {
                // If a field value is not set in the node object when node_save() is
                // called, the old value from $node->original is used.
                if (isset($node->{$field_name})) {
                    $items = $node->{$field_name};
                }
                elseif (isset($node->original->{$field_name})) {
                    $items = $node->original->{$field_name};
                }
                else {
                    continue;
                }
                foreach (field_available_languages('node', $field) as $langcode) {
                    if (!empty($items[$langcode])) {
                        foreach ($items[$langcode] as $item) {
                            $tid_all[$item['tid']] = $item['tid'];
                        }
                    }
                }
            }
        }
        // Insert index entries for all the node's terms.
        if (!empty($tid_all)) {
            $query = db_insert('taxonomy_index')->fields(array(
                'nid',
                'tid',
                'sticky',
                'created',
            ));
            foreach ($tid_all as $tid) {
                $query->values(array(
                    'nid' => $node->nid,
                    'tid' => $tid,
                    'sticky' => $sticky,
                    'created' => $node->created,
                ));
            }
            $query->execute();
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.