function NodeSearch::indexNode

Same name in other branches
  1. 8.9.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::indexNode()
  2. 10 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::indexNode()
  3. 11.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::indexNode()

Indexes a single node.

Parameters

\Drupal\node\NodeInterface $node: The node to index.

Return value

array An array of words to update after indexing.

1 call to NodeSearch::indexNode()
NodeSearch::updateIndex in core/modules/node/src/Plugin/Search/NodeSearch.php
Updates the search index for this plugin.

File

core/modules/node/src/Plugin/Search/NodeSearch.php, line 506

Class

NodeSearch
Handles searching for node entities using the Search module index.

Namespace

Drupal\node\Plugin\Search

Code

protected function indexNode(NodeInterface $node) {
    $words = [];
    $languages = $node->getTranslationLanguages();
    $node_render = $this->entityTypeManager
        ->getViewBuilder('node');
    foreach ($languages as $language) {
        $node = $node->getTranslation($language->getId());
        // Render the node.
        $build = $node_render->view($node, 'search_index', $language->getId());
        unset($build['#theme']);
        // Add the title to text so it is searchable.
        $build['search_title'] = [
            '#prefix' => '<h1>',
            '#plain_text' => $node->label(),
            '#suffix' => '</h1>',
            '#weight' => -1000,
        ];
        $text = $this->renderer
            ->renderPlain($build);
        // Fetch extra data normally not visible.
        $extra = $this->moduleHandler
            ->invokeAll('node_update_index', [
            $node,
        ]);
        foreach ($extra as $t) {
            $text .= $t;
        }
        // Update index, using search index "type" equal to the plugin ID.
        $words += $this->searchIndex
            ->index($this->getPluginId(), $node->id(), $language->getId(), $text, FALSE);
    }
    return $words;
}

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