function taxonomy_build_node_index

Same name in other branches
  1. 7.x modules/taxonomy/taxonomy.module \taxonomy_build_node_index()
  2. 9 core/modules/taxonomy/taxonomy.module \taxonomy_build_node_index()
  3. 8.9.x 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

\Drupal\node\Entity\Node $node: The node entity.

Related topics

2 calls to taxonomy_build_node_index()
taxonomy_node_insert in core/modules/taxonomy/taxonomy.module
Implements hook_ENTITY_TYPE_insert() for node entities.
taxonomy_node_update in core/modules/taxonomy/taxonomy.module
Implements hook_ENTITY_TYPE_update() for node entities.

File

core/modules/taxonomy/taxonomy.module, line 211

Code

function taxonomy_build_node_index($node) {
    // We maintain a denormalized table of term/node relationships, containing
    // only data for current, published nodes.
    if (!\Drupal::config('taxonomy.settings')->get('maintain_index_table') || !\Drupal::entityTypeManager()->getStorage('node') instanceof SqlContentEntityStorage) {
        return;
    }
    $status = $node->isPublished();
    $sticky = (int) $node->isSticky();
    // We only maintain the taxonomy index for published nodes.
    if ($status && $node->isDefaultRevision()) {
        // Collect a unique list of all the term IDs from all node fields.
        $tid_all = [];
        $entity_reference_class = 'Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem';
        foreach ($node->getFieldDefinitions() as $field) {
            $field_name = $field->getName();
            $class = $field->getItemDefinition()
                ->getClass();
            $is_entity_reference_class = $class === $entity_reference_class || is_subclass_of($class, $entity_reference_class);
            if ($is_entity_reference_class && $field->getSetting('target_type') == 'taxonomy_term') {
                foreach ($node->getTranslationLanguages() as $language) {
                    foreach ($node->getTranslation($language->getId())->{$field_name} as $item) {
                        if (!$item->isEmpty()) {
                            $tid_all[$item->target_id] = $item->target_id;
                        }
                    }
                }
            }
        }
        // Insert index entries for all the node's terms.
        if (!empty($tid_all)) {
            $connection = \Drupal::database();
            foreach ($tid_all as $tid) {
                $connection->merge('taxonomy_index')
                    ->keys([
                    'nid' => $node->id(),
                    'tid' => $tid,
                    'status' => $node->isPublished(),
                ])
                    ->fields([
                    'sticky' => $sticky,
                    'created' => $node->getCreatedTime(),
                ])
                    ->execute();
            }
        }
    }
}

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