You are here

public function ForumIndexStorage::createIndex in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 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 134
Contains \Drupal\forum\ForumIndexStorage.

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(array(
    '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(array(
        '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);
  }
}