function ForumIndexStorage::createIndex

Same name in other branches
  1. 9 core/modules/forum/src/ForumIndexStorage.php \Drupal\forum\ForumIndexStorage::createIndex()
  2. 8.9.x core/modules/forum/src/ForumIndexStorage.php \Drupal\forum\ForumIndexStorage::createIndex()
  3. 11.x core/modules/forum/src/ForumIndexStorage.php \Drupal\forum\ForumIndexStorage::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);
    }
}

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