You are here

protected function NodeSearch::indexNode in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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 517

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;
}