diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 391a255..fc859ba 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -5,6 +5,7 @@ * Install, update and uninstall functions for the Comment module. */ +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\field\Entity\FieldConfig; /** @@ -48,7 +49,7 @@ function comment_schema() { 'type' => 'varchar', 'not null' => TRUE, 'default' => 'node', - 'length' => 255, + 'length' => EntityTypeInterface::ID_MAX_LENGTH, 'description' => 'The entity_type of the entity to which this comment is a reply.', ), 'field_name' => array( diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 8930d76..5e21a9b 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -206,7 +206,7 @@ function comment_field_config_insert(FieldConfigInterface $field) { function comment_field_instance_config_delete(FieldInstanceConfigInterface $instance) { if ($instance->getType() == 'comment') { // Delete all comments that used by the entity bundle. - $comments = db_query("SELECT cid FROM {comment} WHERE entity_type = :entity_type AND field_name = :field_name", array( + $comments = db_query("SELECT cid FROM {comment_field_data} WHERE entity_type = :entity_type AND field_name = :field_name", array( ':entity_type' => $instance->getEntityTypeId(), ':field_name' => $instance->getName(), ))->fetchCol(); @@ -276,7 +276,7 @@ function comment_new_page_count($num_comments, $new_replies, ContentEntityInterf // thread with a new comment. // 1. Find all the threads with a new comment. - $unread_threads_query = db_select('comment') + $unread_threads_query = db_select('comment_field_data') ->fields('comment', array('thread')) ->condition('entity_id', $entity->id()) ->condition('entity_type', $entity->getEntityTypeId()) @@ -300,7 +300,7 @@ function comment_new_page_count($num_comments, $new_replies, ContentEntityInterf $first_thread = substr($first_thread, 0, -1); // Find the number of the first comment of the first unread thread. - $count = db_query('SELECT COUNT(*) FROM {comment} WHERE entity_id = :entity_id + $count = db_query('SELECT COUNT(*) FROM {comment_field_data} WHERE entity_id = :entity_id AND entity_type = :entity_type AND field_name = :field_name AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array( @@ -605,7 +605,7 @@ function comment_add(EntityInterface $entity, $field_name = 'comment', $pid = NU * to consider the trailing "/" so we use a substring only. */ function comment_get_thread(EntityInterface $entity, $field_name, $mode, $comments_per_page, $pager_id = 0) { - $query = db_select('comment', 'c') + $query = db_select('comment_field_data', 'c') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); if ($pager_id) { $query->element($pager_id); @@ -622,7 +622,7 @@ function comment_get_thread(EntityInterface $entity, $field_name, $mode, $commen ->addMetaData('field_name', $field_name) ->limit($comments_per_page); - $count_query = db_select('comment', 'c'); + $count_query = db_select('comment_field_data', 'c'); $count_query->addExpression('COUNT(*)'); $count_query ->condition('c.entity_id', $entity->id()) @@ -1065,7 +1065,7 @@ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestam $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); // Use the timestamp to retrieve the number of new comments. - $query = db_select('comment', 'c'); + $query = db_select('comment_field_data', 'c'); $query->addExpression('COUNT(cid)'); $query->condition('c.entity_type', $entity_type) ->condition('c.entity_id', $entity_id) @@ -1104,8 +1104,8 @@ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestam function comment_get_display_ordinal($cid, FieldDefinitionInterface $field_definition) { // Count how many comments (c1) are before $cid (c2) in display order. This is // the 0-based display ordinal. - $query = db_select('comment', 'c1'); - $query->innerJoin('comment', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name'); + $query = db_select('comment_field_data', 'c1'); + $query->innerJoin('comment_field_data', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name'); $query->addExpression('COUNT(*)', 'count'); $query->condition('c2.cid', $cid); if (!user_access('administer comments')) { diff --git a/core/modules/comment/src/CommentStatistics.php b/core/modules/comment/src/CommentStatistics.php index fbbdff4..afb4d56 100644 --- a/core/modules/comment/src/CommentStatistics.php +++ b/core/modules/comment/src/CommentStatistics.php @@ -179,7 +179,7 @@ public function update(CommentInterface $comment) { return; } - $query = $this->database->select('comment', 'c'); + $query = $this->database->select('comment_field_data', 'c'); $query->addExpression('COUNT(cid)'); $count = $query->condition('c.entity_id', $comment->getCommentedEntityId()) ->condition('c.entity_type', $comment->getCommentedEntityTypeId()) @@ -190,7 +190,7 @@ public function update(CommentInterface $comment) { if ($count > 0) { // Comments exist. - $last_reply = $this->database->select('comment', 'c') + $last_reply = $this->database->select('comment_field_data', 'c') ->fields('c', array('cid', 'name', 'changed', 'uid')) ->condition('c.entity_id', $comment->getCommentedEntityId()) ->condition('c.entity_type', $comment->getCommentedEntityTypeId()) diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index f463d0a..c98d269 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -69,7 +69,7 @@ public function updateEntityStatistics(CommentInterface $comment) { * {@inheritdoc} */ public function getMaxThread(EntityInterface $comment) { - $query = $this->database->select('comment', 'c') + $query = $this->database->select('comment_field_data', 'c') ->condition('entity_id', $comment->getCommentedEntityId()) ->condition('field_name', $comment->getFieldName()) ->condition('entity_type', $comment->getCommentedEntityTypeId()); @@ -82,7 +82,7 @@ public function getMaxThread(EntityInterface $comment) { * {@inheritdoc} */ public function getMaxThreadPerThread(EntityInterface $comment) { - $query = $this->database->select('comment', 'c') + $query = $this->database->select('comment_field_data', 'c') ->condition('entity_id', $comment->getCommentedEntityId()) ->condition('field_name', $comment->getFieldName()) ->condition('entity_type', $comment->getCommentedEntityTypeId()) @@ -96,7 +96,7 @@ public function getMaxThreadPerThread(EntityInterface $comment) { * {@inheritdoc} */ public function getChildCids(array $comments) { - return $this->database->select('comment', 'c') + return $this->database->select('comment_field_data', 'c') ->fields('c', array('cid')) ->condition('pid', array_keys($comments)) ->execute() @@ -111,19 +111,16 @@ public function getSchema() { // Marking the respective fields as NOT NULL makes the indexes more // performant. - $schema['comment']['fields']['pid']['not null'] = TRUE; - $schema['comment']['fields']['status']['not null'] = TRUE; - $schema['comment']['fields']['entity_id']['not null'] = TRUE; - $schema['comment']['fields']['created']['not null'] = TRUE; - $schema['comment']['fields']['thread']['not null'] = TRUE; - - unset($schema['comment']['indexes']['field__pid']); - unset($schema['comment']['indexes']['field__entity_id']); - $schema['comment']['indexes'] += array( + $schema['comment_field_data']['fields']['created']['not null'] = TRUE; + $schema['comment_field_data']['fields']['thread']['not null'] = TRUE; + + unset($schema['comment_field_data']['indexes']['comment_field__pid__target_id']); + unset($schema['comment_field_data']['indexes']['comment_field__entity_id__target_id']); + $schema['comment_field_data']['indexes'] += array( 'comment__status_pid' => array('pid', 'status'), 'comment__num_new' => array( 'entity_id', - array('entity_type', 32), + 'entity_type', 'comment_type', 'status', 'created', @@ -132,13 +129,13 @@ public function getSchema() { ), 'comment__entity_langcode' => array( 'entity_id', - array('entity_type', 32), + 'entity_type', 'comment_type', - 'langcode', + 'default_langcode', ), 'comment__created' => array('created'), ); - $schema['comment']['foreign keys'] += array( + $schema['comment_field_data']['foreign keys'] += array( 'comment__author' => array( 'table' => 'users', 'columns' => array('uid' => 'uid'), @@ -152,7 +149,7 @@ public function getSchema() { * {@inheritdoc} */ public function getUnapprovedCount() { - return $this->database->select('comment', 'c') + return $this->database->select('comment_field_data', 'c') ->condition('status', CommentInterface::NOT_PUBLISHED, '=') ->countQuery() ->execute() diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 1773b31..3294c88 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -34,6 +34,7 @@ * "translation" = "Drupal\comment\CommentTranslationHandler" * }, * base_table = "comment", + * data_table = "comment_field_data", * uri_callback = "comment_uri", * fieldable = TRUE, * translatable = TRUE, @@ -228,6 +229,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['subject'] = FieldDefinition::create('string') ->setLabel(t('Subject')) ->setDescription(t('The comment title or subject.')) + ->setTranslatable(TRUE) ->setSetting('max_length', 64); $fields['uid'] = FieldDefinition::create('entity_reference') @@ -279,7 +281,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['entity_type'] = FieldDefinition::create('string') ->setLabel(t('Entity type')) - ->setDescription(t('The entity type to which this comment is attached.')); + ->setDescription(t('The entity type to which this comment is attached.')) + ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH); $fields['comment_type'] = FieldDefinition::create('entity_reference') ->setLabel(t('Comment Type')) @@ -289,7 +292,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['field_name'] = FieldDefinition::create('string') ->setLabel(t('Comment field name')) ->setDescription(t('The field name through which this comment was added.')) - ->setSetting('max_length', 32); + ->setSetting('max_length', FieldConfig::NAME_MAX_LENGTH); return $fields; } diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php index 5ccf4e2..f7e0436 100644 --- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php @@ -118,7 +118,7 @@ public function preRender(&$values) { } if ($nids) { - $result = $this->database->query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment} c ON n.nid = c.entity_id AND c.entity_type = 'node' + $result = $this->database->query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment_field_data} c ON n.nid = c.entity_id AND c.entity_type = 'node' LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN (:nids) AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp) AND c.status = :status GROUP BY n.nid", array( ':status' => CommentInterface::PUBLISHED, diff --git a/core/modules/comment/src/Tests/CommentLanguageTest.php b/core/modules/comment/src/Tests/CommentLanguageTest.php index aa3d5aa..cb33ca1 100644 --- a/core/modules/comment/src/Tests/CommentLanguageTest.php +++ b/core/modules/comment/src/Tests/CommentLanguageTest.php @@ -117,7 +117,7 @@ function testCommentLanguage() { $this->drupalPostForm(NULL, $edit, t('Save')); // Check that comment language matches the current content language. - $cid = db_select('comment', 'c') + $cid = db_select('comment_field_data', 'c') ->fields('c', array('cid')) ->condition('entity_id', $node->id()) ->condition('entity_type', 'node') diff --git a/core/modules/forum/src/ForumIndexStorage.php b/core/modules/forum/src/ForumIndexStorage.php index d732806..e7ba619 100644 --- a/core/modules/forum/src/ForumIndexStorage.php +++ b/core/modules/forum/src/ForumIndexStorage.php @@ -96,14 +96,14 @@ public function update(NodeInterface $node) { */ public function updateIndex(NodeInterface $node) { $nid = $node->id(); - $count = $this->database->query("SELECT COUNT(cid) FROM {comment} 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", array( + $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", array( ':nid' => $nid, ':status' => CommentInterface::PUBLISHED, ))->fetchField(); if ($count > 0) { // Comments exist. - $last_reply = $this->database->queryRange("SELECT cid, name, created, uid FROM {comment} WHERE entity_id = :nid AND field_name = 'comment_forum' AND entity_type = 'node' AND status = :status ORDER BY cid DESC", 0, 1, array( + $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 ORDER BY cid DESC", 0, 1, array( ':nid' => $nid, ':status' => CommentInterface::PUBLISHED, ))->fetchObject(); diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index 9195600..6594d24 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -81,7 +81,7 @@ function tracker_cron() { )) ->execute(); - $query = db_select('comment', 'c', array('target' => 'replica')); + $query = db_select('comment_field_data', 'c', array('target' => 'replica')); // Force PostgreSQL to do an implicit cast by adding 0. $query->addExpression('0 + :changed', 'changed', array(':changed' => $changed)); $query->addField('c', 'status', 'published'); @@ -291,7 +291,7 @@ function _tracker_calculate_changed($nid) { // @todo This should be actually filtering on the desired language and just // fall back to the default language. $changed = db_query('SELECT changed FROM {node_field_data} WHERE nid = :nid AND default_langcode = 1 ORDER BY changed DESC', array(':nid' => $nid), array('target' => 'replica'))->fetchField(); - $latest_comment = db_query_range("SELECT cid, changed FROM {comment} WHERE entity_type = 'node' AND entity_id = :nid AND status = :status ORDER BY changed DESC", 0, 1, array( + $latest_comment = db_query_range("SELECT cid, changed FROM {comment_field_data} WHERE entity_type = 'node' AND entity_id = :nid AND status = :status ORDER BY changed DESC", 0, 1, array( ':nid' => $nid, ':status' => CommentInterface::PUBLISHED, ), array('target' => 'replica'))->fetchObject(); @@ -324,7 +324,7 @@ function _tracker_remove($nid, $uid = NULL, $changed = NULL) { // Or if they have commented on the node. if (!$keep_subscription) { // Check if the user has commented at least once on the given nid. - $keep_subscription = db_query_range("SELECT COUNT(*) FROM {comment} WHERE entity_type = 'node' AND entity_id = :nid AND uid = :uid AND status = :status", 0, 1, array( + $keep_subscription = db_query_range("SELECT COUNT(*) FROM {comment_field_data} WHERE entity_type = 'node' AND entity_id = :nid AND uid = :uid AND status = :status", 0, 1, array( ':nid' => $nid, ':uid' => $uid, ':status' => CommentInterface::PUBLISHED,