3 namespace Drupal\forum;
4 use Drupal\comment\CommentInterface;
5 use Drupal\Core\Database\Connection;
6 use Drupal\node\NodeInterface;
9 * Handles CRUD operations to {forum_index} table.
11 class ForumIndexStorage implements ForumIndexStorageInterface {
14 * The active database connection.
16 * @var \Drupal\Core\Database\Connection
21 * Constructs a ForumIndexStorage object.
23 * @param \Drupal\Core\Database\Connection $database
24 * The current database connection.
26 public function __construct(Connection $database) {
27 $this->database = $database;
33 public function getOriginalTermId(NodeInterface $node) {
34 return $this->database->queryRange("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, [':nid' => $node->id()])->fetchField();
40 public function create(NodeInterface $node) {
41 $this->database->insert('forum')
43 'tid' => $node->forum_tid,
44 'vid' => $node->getRevisionId(),
53 public function read(array $vids) {
54 return $this->database->select('forum', 'f')
55 ->fields('f', ['nid', 'tid'])
56 ->condition('f.vid', $vids, 'IN')
63 public function delete(NodeInterface $node) {
64 $this->database->delete('forum')
65 ->condition('nid', $node->id())
72 public function deleteRevision(NodeInterface $node) {
73 $this->database->delete('forum')
74 ->condition('nid', $node->id())
75 ->condition('vid', $node->getRevisionId())
82 public function update(NodeInterface $node) {
83 $this->database->update('forum')
84 ->fields(['tid' => $node->forum_tid])
85 ->condition('vid', $node->getRevisionId())
92 public function updateIndex(NodeInterface $node) {
94 $count = $this->database->query("SELECT COUNT(cid) FROM {comment_field_data} c INNER JOIN {forum_index} i ON c.entity_id = i.nid WHERE c.entity_id = :nid AND c.field_name = 'comment_forum' AND c.entity_type = 'node' AND c.status = :status AND c.default_langcode = 1", [
96 ':status' => CommentInterface::PUBLISHED,
101 $last_reply = $this->database->queryRange("SELECT cid, name, created, uid FROM {comment_field_data} WHERE entity_id = :nid AND field_name = 'comment_forum' AND entity_type = 'node' AND status = :status AND default_langcode = 1 ORDER BY cid DESC", 0, 1, [
103 ':status' => CommentInterface::PUBLISHED,
105 $this->database->update('forum_index')
107 'comment_count' => $count,
108 'last_comment_timestamp' => $last_reply->created,
110 ->condition('nid', $nid)
114 // Comments do not exist.
115 // @todo This should be actually filtering on the desired node language
116 $this->database->update('forum_index')
118 'comment_count' => 0,
119 'last_comment_timestamp' => $node->getCreatedTime(),
121 ->condition('nid', $nid)
129 public function createIndex(NodeInterface $node) {
130 $query = $this->database->insert('forum_index')
131 ->fields(['nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp']);
132 foreach ($node->getTranslationLanguages() as $langcode => $language) {
133 $translation = $node->getTranslation($langcode);
134 foreach ($translation->taxonomy_forums as $item) {
136 'nid' => $node->id(),
137 'title' => $translation->label(),
138 'tid' => $item->target_id,
139 'sticky' => (int) $node->isSticky(),
140 'created' => $node->getCreatedTime(),
141 'comment_count' => 0,
142 'last_comment_timestamp' => $node->getCreatedTime(),
147 // The logic for determining last_comment_count is fairly complex, so
148 // update the index too.
149 if ($node->isNew()) {
150 $this->updateIndex($node);
157 public function deleteIndex(NodeInterface $node) {
158 $this->database->delete('forum_index')
159 ->condition('nid', $node->id())