You are here

public function ForumIndexStorage::createIndex in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/forum/src/ForumIndexStorage.php \Drupal\forum\ForumIndexStorage::createIndex()

Creates a {forum_index} entry for the given node.

Parameters

\Drupal\node\NodeInterface $node: The node for which the index records are to be created.

Overrides ForumIndexStorageInterface::createIndex

File

core/modules/forum/src/ForumIndexStorage.php, line 130

Class

ForumIndexStorage
Handles CRUD operations to {forum_index} table.

Namespace

Drupal\forum

Code

public function createIndex(NodeInterface $node) {
  $query = $this->database
    ->insert('forum_index')
    ->fields([
    'nid',
    'title',
    'tid',
    'sticky',
    'created',
    'comment_count',
    'last_comment_timestamp',
  ]);
  foreach ($node
    ->getTranslationLanguages() as $langcode => $language) {
    $translation = $node
      ->getTranslation($langcode);
    foreach ($translation->taxonomy_forums as $item) {
      $query
        ->values([
        'nid' => $node
          ->id(),
        'title' => $translation
          ->label(),
        'tid' => $item->target_id,
        'sticky' => (int) $node
          ->isSticky(),
        'created' => $node
          ->getCreatedTime(),
        'comment_count' => 0,
        'last_comment_timestamp' => $node
          ->getCreatedTime(),
      ]);
    }
  }
  $query
    ->execute();

  // The logic for determining last_comment_count is fairly complex, so
  // update the index too.
  if ($node
    ->isNew()) {
    $this
      ->updateIndex($node);
  }
}