diff --git a/core/modules/comment/comment-entity-form.js b/core/modules/comment/comment-entity-form.js new file mode 100644 index 0000000..e068ef5 --- /dev/null +++ b/core/modules/comment/comment-entity-form.js @@ -0,0 +1,19 @@ +/** + * @file + * Attaches comment behaviors to the entity form. + */ + +(function ($) { + +"use strict"; + +Drupal.behaviors.commentFieldsetSummaries = { + attach: function (context) { + var $context = $(context); + $context.find('fieldset.comment-entity-settings-form').drupalSetSummary(function (context) { + return Drupal.checkPlain($(context).find('.form-item-comment input:checked').next('label').text()); + }); + } +}; + +})(jQuery); diff --git a/core/modules/comment/comment-node-form.js b/core/modules/comment/comment-node-form.js deleted file mode 100644 index 0249d96..0000000 --- a/core/modules/comment/comment-node-form.js +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @file - * Attaches comment behaviors to the node form. - */ - -(function ($) { - -"use strict"; - -Drupal.behaviors.commentFieldsetSummaries = { - attach: function (context) { - var $context = $(context); - $context.find('fieldset.comment-node-settings-form').drupalSetSummary(function (context) { - return Drupal.checkPlain($(context).find('.form-item-comment input:checked').next('label').text()); - }); - - // Provide the summary for the node type form. - $context.find('fieldset.comment-node-type-settings-form').drupalSetSummary(function(context) { - var $context = $(context); - var vals = []; - - // Default comment setting. - vals.push($context.find(".form-item-comment select option:selected").text()); - - // Threading. - var threading = $(context).find(".form-item-comment-default-mode input:checked").next('label').text(); - if (threading) { - vals.push(threading); - } - - // Comments per page. - var number = $context.find(".form-item-comment-default-per-page select option:selected").val(); - vals.push(Drupal.t('@number comments per page', {'@number': number})); - - return Drupal.checkPlain(vals.join(', ')); - }); - } -}; - -})(jQuery); diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index dcbd1bc..c99a239 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -9,6 +9,51 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** + * Page callback for displaying the comment bundle admin overview page. + */ +function comment_overview_bundles() { + $header = array(t('Field name'), t('Used in'), array('data' => t('Operations'), 'colspan' => (module_exists('field_ui') ? '4' : '2'))); + $rows = array(); + + foreach (comment_get_comment_fields() as $field_name => $field_info) { + foreach ($field_info['bundles'] as $entity_type => $field_bundles) { + $bundles = array_intersect_key(field_info_bundles($entity_type), array_flip($field_bundles)); + + foreach ($bundles as $bundle => $bundle_info) { + if (!isset($rows[$field_name])) { + $rows[$field_name]['class'] = $field_info['locked'] ? array('menu-disabled') : array(''); + $rows[$field_name]['data']['label'] = $field_info['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name; + } + + if (module_exists('field_ui') && $path = _field_ui_bundle_admin_path($entity_type, $bundle)) { + $rows[$field_name]['data']['usage'][] = l($bundle_info['label'], $path . '/fields'); + } + else { + $rows[$field_name]['data']['usage'][] = $bundle_info['label']; + } + + if (module_exists('field_ui')) { + $name = strtr($field_name, '_', '-'); + $rows[$field_name]['data']['fields'] = l(t('manage fields'), 'admin/structure/comments/' . $name . '/fields'); + $rows[$field_name]['data']['display'] = l(t('manage display'), 'admin/structure/comments/' . $name . '/display'); + } + } + } + + $rows[$field_name]['data']['usage'] = implode(', ', $rows[$field_name]['data']['usage']); + } + + $build['overview'] = array( + '#theme' => 'table', + '#header' => $header, + '#rows' => $rows, + '#empty' => t('No comment bundles available.'), + ); + + return $build; +} + +/** * Page callback: Presents an administrative comment listing. * * @param $type @@ -82,23 +127,26 @@ function comment_admin_overview($form, &$form_state, $arg) { $query = db_select('comment', 'c') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') ->extend('Drupal\Core\Database\Query\TableSortExtender'); - $query->join('node', 'n', 'n.nid = c.nid'); - $query->addField('n', 'title', 'node_title'); + $query->leftJoin('node', 'n', "n.nid = c.entity_id AND c.entity_type = 'node'"); $query->addTag('node_access'); + $query->addTag('entity_access'); $result = $query - ->fields('c', array('cid', 'subject', 'name', 'changed')) + ->fields('c', array('cid', 'subject', 'name', 'changed', 'entity_id', 'entity_type', 'field_name')) ->condition('c.status', $status) ->limit(50) ->orderByHeader($header) ->execute(); - $cids = array(); + $cids = $entity_ids = $entities = array(); // We collect a sorted list of node_titles during the query to attach to the // comments later. foreach ($result as $row) { + $entity_ids[$row->entity_type][] = $row->entity_id; $cids[] = $row->cid; - $node_titles[] = $row->node_title; + } + foreach ($entity_ids as $entity_type => $ids) { + $entities[$entity_type] = entity_load_multiple($entity_type, $ids); } $comments = comment_load_multiple($cids); @@ -107,9 +155,9 @@ function comment_admin_overview($form, &$form_state, $arg) { $destination = drupal_get_destination(); foreach ($comments as $comment) { - // Remove the first node title from the node_titles array and attach to - // the comment. - $comment->node_title = array_shift($node_titles); + // Use the first entity label. + $comment->entity_title = $entities[$comment->entity_type][$comment->entity_id]->label(); + $comment->entity_uri = $entities[$comment->entity_type][$comment->entity_id]->uri(); $comment_body = field_get_items('comment', $comment, 'comment_body'); $options[$comment->cid] = array( 'subject' => array( @@ -124,8 +172,9 @@ function comment_admin_overview($form, &$form_state, $arg) { 'posted_in' => array( 'data' => array( '#type' => 'link', - '#title' => $comment->node_title, - '#href' => 'node/' . $comment->nid, + '#title' => $comment->entity_title, + '#href' => $comment->entity_uri['path'], + '#options' => $comment->entity_uri['options'] ), ), 'changed' => format_date($comment->changed, 'short'), @@ -284,10 +333,12 @@ function comment_confirm_delete($form, &$form_state, Comment $comment) { $form_state['comment'] = $comment; // Always provide entity id in the same form key as in the entity edit form. $form['cid'] = array('#type' => 'value', '#value' => $comment->cid); + $entity = entity_load($comment->entity_type, $comment->entity_id); + $uri = $entity->uri(); return confirm_form( $form, t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)), - 'node/' . $comment->nid, + $uri['path'], t('Any replies to this comment will be lost. This action cannot be undone.'), t('Delete'), t('Cancel'), @@ -306,5 +357,7 @@ function comment_confirm_delete_submit($form, &$form_state) { // Clear the cache so an anonymous user sees that his comment was deleted. cache_invalidate(array('content' => TRUE)); - $form_state['redirect'] = "node/$comment->nid"; + $entity = entity_load($comment->entity_type, $comment->entity_id); + $uri = $entity->uri(); + $form_state['redirect'] = $uri['path']; } diff --git a/core/modules/comment/comment.api.php b/core/modules/comment/comment.api.php index 95c3e1d..a40d95d 100644 --- a/core/modules/comment/comment.api.php +++ b/core/modules/comment/comment.api.php @@ -34,7 +34,9 @@ function hook_comment_presave(Drupal\comment\Comment $comment) { */ function hook_comment_insert(Drupal\comment\Comment $comment) { // Reindex the node when comments are added. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** @@ -45,7 +47,9 @@ function hook_comment_insert(Drupal\comment\Comment $comment) { */ function hook_comment_update(Drupal\comment\Comment $comment) { // Reindex the node when comments are updated. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** @@ -66,9 +70,9 @@ function hook_comment_load(Drupal\comment\Comment $comments) { * * @param Drupal\comment\Comment $comment * Passes in the comment the action is being performed on. - * @param $view_mode + * @param string $view_mode * View mode, e.g. 'full', 'teaser'... - * @param $langcode + * @param string $langcode * The language code used for rendering. * * @see hook_entity_view() @@ -91,7 +95,7 @@ function hook_comment_view(Drupal\comment\Comment $comment, $view_mode, $langcod * comment.tpl.php. See drupal_render() and theme() documentation respectively * for details. * - * @param $build + * @param array $build * A renderable array representing the comment. * @param Drupal\comment\Comment $comment * The comment being rendered. @@ -99,7 +103,7 @@ function hook_comment_view(Drupal\comment\Comment $comment, $view_mode, $langcod * @see comment_view() * @see hook_entity_view_alter() */ -function hook_comment_view_alter(&$build, Drupal\comment\Comment $comment) { +function hook_comment_view_alter(array &$build, Drupal\comment\Comment $comment) { // Check for the existence of a field added by another module. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) { // Change its weight. @@ -170,5 +174,31 @@ function hook_comment_delete(Drupal\comment\Comment $comment) { } /** + * Controls access to commenting on an entity where the no {%entity_type}_access + * function exists for the given entity. + * + * Modules may implement this hook if they want to have a say in whether or not + * the logged in user has access to view an entity in order to reply to a + * comment. + * + * @param Drupal\entity\Entity $entity + * The entity to which the comment field is attached. + * + * @return mixed + * - COMMENT_ENTITY_ACCESS_DENY: if the operation is to be denied. + * - FALSE: to not affect this operation at all. + */ +function hook_comment_entity_access(Drupal\entity\Entity $entity) { + $type = $entity->entityType(); + + if ($type == 'comment') { + return COMMENT_ENTITY_ACCESS_DENY; + } + + // Returning nothing from this function would have the same effect. + return FALSE; +} + +/** * @} End of "addtogroup hooks". */ diff --git a/core/modules/comment/comment.field.inc b/core/modules/comment/comment.field.inc new file mode 100644 index 0000000..efcbc63 --- /dev/null +++ b/core/modules/comment/comment.field.inc @@ -0,0 +1,292 @@ + array( + 'label' => t('Comments'), + 'default_widget' => 'comment_default', + 'settings' => array(), + 'instance_settings' => array( + 'comment' => array( + 'comment' => COMMENT_ENTITY_OPEN, + 'comment_default_mode' => COMMENT_MODE_THREADED, + 'comment_default_per_page' => 50, + 'comment_anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT, + 'comment_subject_field' => 1, + 'comment_form_location' => COMMENT_FORM_BELOW, + 'comment_preview' => DRUPAL_OPTIONAL + ) + ), + 'description' => t('This field manages configuration and presentation of comments on an entity'), + 'default_formatter' => 'comment_default' + ) + ); +} + +/** + * Implements hook_field_instance_settings_form(). + */ +function comment_field_instance_settings_form($field, $instance) { + $settings = $instance['settings']; + $form['comment'] = array( + '#type' => 'fieldset', + '#title' => t('Comment settings'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + '#attributes' => array( + 'class' => array('comment-instance-settings-form'), + ), + '#attached' => array( + 'library' => array('comment', 'drupal.comment'), + ), + ); + $form['comment']['comment'] = array( + '#type' => 'select', + '#title' => t('Default comment setting for new content'), + '#default_value' => empty($settings['comment']) ? COMMENT_ENTITY_OPEN : $settings['comment'], + '#options' => array( + COMMENT_ENTITY_OPEN => t('Open'), + COMMENT_ENTITY_CLOSED => t('Closed'), + COMMENT_ENTITY_HIDDEN => t('Hidden'), + ), + ); + $form['comment']['comment_default_mode'] = array( + '#type' => 'checkbox', + '#title' => t('Threading'), + '#default_value' => empty($settings['comment_default_mode']) ? COMMENT_MODE_THREADED : $settings['comment_default_mode'], + '#description' => t('Show comment replies in a threaded list.'), + ); + $form['comment']['comment_default_per_page'] = array( + '#type' => 'select', + '#title' => t('Comments per page'), + '#default_value' => empty($settings['comment_default_per_page']) ? 50 : $settings['comment_default_per_page'], + '#options' => _comment_per_page(), + ); + $form['comment']['comment_anonymous'] = array( + '#type' => 'select', + '#title' => t('Anonymous commenting'), + '#default_value' => empty($settings['comment_anonymous']) ? COMMENT_ANONYMOUS_MAYNOT_CONTACT : $settings['comment_anonymous'], + '#options' => array( + COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'), + COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'), + COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'), + ), + '#access' => user_access('post comments', drupal_anonymous_user()), + ); + $form['comment']['comment_subject_field'] = array( + '#type' => 'checkbox', + '#title' => t('Allow comment title'), + '#default_value' => empty($settings['comment_subject_field']) ? 1 : $settings['comment_subject_field'], + ); + $form['comment']['comment_form_location'] = array( + '#type' => 'checkbox', + '#title' => t('Show reply form on the same page as comments'), + '#default_value' => empty($settings['comment_form_location']) ? COMMENT_FORM_BELOW : $settings['comment_form_location'], + ); + $form['comment']['comment_preview'] = array( + '#type' => 'radios', + '#title' => t('Preview comment'), + '#default_value' => empty($settings['comment_preview']) ? DRUPAL_OPTIONAL : $settings['comment_preview'], + '#options' => array( + DRUPAL_DISABLED => t('Disabled'), + DRUPAL_OPTIONAL => t('Optional'), + DRUPAL_REQUIRED => t('Required'), + ), + ); + return $form; +} + +/** + * Implements hook_field_formatter_info(). + */ +function comment_field_formatter_info() { + return array( + 'comment_default' => array( + 'label' => t('Comments'), + 'field types' => array('comment'), + ), + ); +} + +/** + * Implements hook_field_widget_info(). + * + * @todo convert this to a WidgetPlugin. + */ +function comment_field_widget_info() { + return array( + 'comment_default' => array( + 'label' => t('Comment'), + 'field types' => array('comment'), + 'settings' => array(), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_CUSTOM, + 'default value' => FIELD_BEHAVIOR_NONE, + ), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + * + * @todo convert this to a WidgetPlugin. + */ +function comment_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + $entity = $element['#entity']; + $settings = $instance['settings']['comment']; + $element += array( + '#type' => 'fieldset', + '#access' => user_access('administer comments'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#group' => 'additional_settings', + '#attributes' => array( + 'class' => array('comment-node-settings-form'), + ), + '#attached' => array( + 'library' => array('comment', 'drupal.comment'), + ), + '#weight' => 30, + ); + $values = reset($items); + if (empty($values)) { + $values = array( + // More here from $instance['settings']; + 'comment' => $settings['comment'], + 'comment_count' => 0 + ); + } + $entity_id = $entity->id(); + $comment_count = empty($values['comment_count']) ? 0 : $values['comment_count']; + $comment_settings = ($values['comment'] == COMMENT_ENTITY_HIDDEN && empty($comment_count)) ? COMMENT_ENTITY_CLOSED : $values['comment']; + $element['comment'] = array( + '#type' => 'radios', + '#title' => t('Comments'), + '#title_display' => 'invisible', + '#default_value' => $comment_settings, + '#options' => array( + COMMENT_ENTITY_OPEN => t('Open'), + COMMENT_ENTITY_CLOSED => t('Closed'), + COMMENT_ENTITY_HIDDEN => t('Hidden'), + ), + COMMENT_ENTITY_OPEN => array( + '#description' => t('Users with the "Post comments" permission can post comments.'), + ), + COMMENT_ENTITY_CLOSED => array( + '#description' => t('Users cannot post comments, but existing comments will be displayed.'), + ), + COMMENT_ENTITY_HIDDEN => array( + '#description' => t('Comments are hidden from view.'), + ), + ); + // If the node doesn't have any comments, the "hidden" option makes no + // sense, so don't even bother presenting it to the user. + if (empty($comment_count)) { + $element['comment'][COMMENT_ENTITY_HIDDEN]['#access'] = FALSE; + // Also adjust the description of the "closed" option. + $element['comment'][COMMENT_ENTITY_CLOSED]['#description'] = t('Users cannot post comments.'); + } + return array($element); +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function comment_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + // This is where we put the settings for 'display below' etc. + // @todo. + return $element; +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function comment_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + $summary = array(); + // @todo. + $summary[] = 'Settings here'; + return implode('
', $summary); +} + + +/** + * Implements hook_field_formatter_view(). + */ +function comment_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + + switch ($display['type']) { + case 'comment_default': + // We only ever have one value. + $values = reset($items); + if ($values['comment'] != COMMENT_ENTITY_HIDDEN) { + $additions = FALSE; + if ($values['comment'] && empty($entity->in_preview)) { + // Only attempt to render comments if the node has visible comments. + // Unpublished comments are not included in $node->comment_count, so show + // comments unconditionally if the user is an administrator. + $comment_settings = $instance['settings']['comment']; + if ((!empty($entity->comment_statistics[$field['field_name']]->comment_count) && user_access('access comments')) || user_access('administer comments')) { + $mode = $comment_settings['comment_default_mode']; + $comments_per_page = $comment_settings['comment_default_per_page']; + if ($cids = comment_get_thread($entity, $field['field_name'], $mode, $comments_per_page)) { + $comments = comment_load_multiple($cids); + comment_prepare_thread($comments); + $build = comment_view_multiple($comments); + $build['pager']['#theme'] = 'pager'; + $additions['comments'] = $build; + } + } + + // Append comment form if needed. + if ($values['comment'] == COMMENT_ENTITY_OPEN && $comment_settings['comment_form_location'] == COMMENT_FORM_BELOW) { + if (user_access('post comments')) { + $additions['comment_form'] = comment_add($entity, $field['field_name']); + } + } + + if ($additions) { + $additions += array( + '#theme' => 'comment_wrapper__' . $entity->entityType() . '__' . $entity->bundle() . '__' . $field['field_name'], + '#entity' => $entity, + '#display_mode' => $instance['settings']['comment']['comment_default_mode'], + 'comments' => array(), + 'comment_form' => array(), + ); + } + if (!empty($additions)) { + $element[] = $additions; + } + } + } + break; + + } + + return $element; +} + +/** + * Implements hook_field_is_empty(). + */ +function comment_field_is_empty($item, $field) { + // We always want the values saved so we can rely on them. + return FALSE; +} diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index f47151f..d51fad3 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -14,38 +14,67 @@ function comment_uninstall() { // Remove variables. variable_del('comment_block_count'); - $node_types = array_keys(node_type_get_types()); - foreach ($node_types as $node_type) { - field_attach_delete_bundle('comment', 'comment_node_' . $node_type); - variable_del('comment_' . $node_type); - variable_del('comment_anonymous_' . $node_type); - variable_del('comment_controls_' . $node_type); - variable_del('comment_default_mode_' . $node_type); - variable_del('comment_default_order_' . $node_type); - variable_del('comment_default_per_page_' . $node_type); - variable_del('comment_form_location_' . $node_type); - variable_del('comment_preview_' . $node_type); - variable_del('comment_subject_field_' . $node_type); + $comment_fields = comment_get_comment_fields(); + foreach ($comment_fields as $entity_type => $bundles) { + foreach ($bundles as $bundle => $fields) { + foreach ($fields as $field_name => $field) { + field_attach_delete_bundle('comment', $field_name); + } + } } } /** * Implements hook_enable(). + * @todo make this work with entities instead of nodes. */ function comment_enable() { - // Insert records into the node_comment_statistics for nodes that are missing. - $query = db_select('node', 'n'); - $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid'); - $query->addField('n', 'created', 'last_comment_timestamp'); - $query->addField('n', 'uid', 'last_comment_uid'); - $query->addField('n', 'nid'); - $query->addExpression('0', 'comment_count'); - $query->addExpression('NULL', 'last_comment_name'); - $query->isNull('ncs.comment_count'); + $comment_fields = comment_get_comment_fields(); + $entity_info = entity_get_info(); + foreach ($comment_fields as $entity_type => $bundles) { + foreach ($bundles as $bundle => $fields) { + foreach ($fields as $field_name => $field) { + $entity_detail = $entity_info[$entity_type]; + if (!empty($entity_detail['base table']) && + !empty($entity_detail['entity keys']['id'])) { + $table = $entity_detail['base table']; + $schema = drupal_get_schema($table); + // Insert records into the comment_entity_statistics for entities that + // are missing. + $query = db_select($table, 'e'); + // Filter by bundle. + $query->condition($entity_detail['entity keys']['bundle'], $bundle); + $query->leftJoin('comment_entity_statistics', 'ces', 'ces.entity_id = e.' . $entity_detail['entity keys']['id'] . ' AND ces.entity_type = ' . $entity_type); + if (!empty($schema[$table]['fields']['created'])) { + $query->addField('e', 'created', 'last_comment_timestamp'); + } + else { + // No created field for this entity type, default to now. + $query->addExpression(REQUEST_TIME, 'last_comment_timestamp'); + } + if (!empty($schema[$table]['fields']['uid'])) { + $query->addField('e', 'uid', 'last_comment_uid'); + } + else { + // No uid field for this entity type, default to anonymous. + $query->addExpression(0, 'last_comment_uid'); + } + $query->addField('e', $entity_detail['entity keys']['id'], 'entity_id'); + $query->addExpression($entity_type, 'entity_type'); + $query->addExpression($field_name, 'field_name'); + $query->addExpression('0', 'comment_count'); + $query->addExpression('NULL', 'last_comment_name'); + $query->isNull('ces.comment_count'); - db_insert('node_comment_statistics') - ->from($query) - ->execute(); + db_insert('comment_entity_statistics') + ->from($query) + ->execute(); + } + } + } + } + // Set default value of comment_maintain_entity_statistics. + state()->set('comment_maintain_entity_statistics', TRUE); } /** @@ -63,6 +92,8 @@ function comment_enable() { * may override that default. * * @see comment_node_type_insert() + * + * @todo make this work with entity types. */ function comment_modules_enabled($modules) { // Only react if the Comment module is one of the modules being enabled. @@ -103,12 +134,26 @@ function comment_schema() { 'default' => 0, 'description' => 'The {comment}.cid to which this comment is a reply. If set to 0, this comment is not a reply to an existing comment.', ), - 'nid' => array( + 'entity_id' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, - 'description' => 'The {node}.nid to which this comment is a reply.', + 'description' => 'The entity_id to which this comment is a reply.', + ), + 'entity_type' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'node', + 'length' => 255, + 'description' => 'The entity_type to which this comment is a reply.', + ), + 'field_name' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'comment', + 'length' => 255, + 'description' => 'The field_name through which this comment was added.', ), 'uid' => array( 'type' => 'int', @@ -185,9 +230,14 @@ function comment_schema() { ), 'indexes' => array( 'comment_status_pid' => array('pid', 'status'), - 'comment_num_new' => array('nid', 'status', 'created', 'cid', 'thread'), + 'comment_num_new' => array('entity_id', 'entity_type', 'field_name', 'status', 'created', 'cid', 'thread'), 'comment_uid' => array('uid'), - 'comment_nid_langcode' => array('nid', 'langcode'), + 'comment_entity_langcode' => array( + 'entity_id', + 'entity_type', + 'field_name', + 'langcode' + ), 'comment_created' => array('created'), ), 'primary key' => array('cid'), @@ -195,10 +245,6 @@ function comment_schema() { 'uuid' => array('uuid'), ), 'foreign keys' => array( - 'comment_node' => array( - 'table' => 'node', - 'columns' => array('nid' => 'nid'), - ), 'comment_author' => array( 'table' => 'users', 'columns' => array('uid' => 'uid'), @@ -206,15 +252,29 @@ function comment_schema() { ), ); - $schema['node_comment_statistics'] = array( - 'description' => 'Maintains statistics of node and comments posts to show "new" and "updated" flags.', + $schema['comment_entity_statistics'] = array( + 'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.', 'fields' => array( - 'nid' => array( + 'entity_id' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, - 'description' => 'The {node}.nid for which the statistics are compiled.', + 'description' => 'The entity_id for which the statistics are compiled.', + ), + 'entity_type' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'node', + 'length' => 255, + 'description' => 'The entity_type to which this comment is a reply.', + ), + 'field_name' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'comment', + 'length' => 255, + 'description' => 'The field_name through which this comment was added.', ), 'cid' => array( 'type' => 'int', @@ -249,17 +309,13 @@ function comment_schema() { 'description' => 'The total number of comments on this node.', ), ), - 'primary key' => array('nid'), + 'primary key' => array('entity_id', 'entity_type', 'field_name'), 'indexes' => array( 'node_comment_timestamp' => array('last_comment_timestamp'), 'comment_count' => array('comment_count'), 'last_comment_uid' => array('last_comment_uid'), ), 'foreign keys' => array( - 'statistics_node' => array( - 'table' => 'node', - 'columns' => array('nid' => 'nid'), - ), 'last_comment_author' => array( 'table' => 'users', 'columns' => array( @@ -273,6 +329,26 @@ function comment_schema() { } /** + * Implements hook_field_schema(). + */ +function comment_field_schema($field) { + $columns = array(); + if ($field['type'] == 'comment') { + $columns += array( + 'comment' => array( + 'description' => 'Whether comments are allowed on this entity: 0 = no, 1 = closed (read only), 2 = open (read/write).', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ); + } + return array( + 'columns' => $columns, + ); +} + +/** * @addtogroup updates-7.x-to-8.x * @{ */ @@ -382,6 +458,194 @@ function comment_update_8003(&$sandbox) { } /** + * Update the comment_node_statistics and comment tables to new structure. + */ +function comment_update_8004(&$sandbox) { + // Rename {node}.comment to {node}.comment_old to avoid clashes with new + // field api field named comment. + db_change_field('node', 'comment', 'comment_old', array( + 'description' => 'Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write).', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + )); + // Rename {node_revision}.comment to {node_revision}.comment_old to avoid + // clashes with new field api field named comment. + db_change_field('node_revision', 'comment', 'comment_old', array( + 'description' => 'Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + )); + // Remove the comment_node foreign key. + db_drop_index('comment', 'comment_node'); + // Remove the comment_nid_langcode index. + db_drop_index('comment', 'comment_nid_langcode'); + // Add the comment_entity_langcode index. + db_add_index('comment', 'comment_entity_langcode', array( + 'entity_id', + 'entity_type', + 'field_name', + 'langcode' + )); + // Add the entity_type and field_name columns to comment. + db_add_field('comment', 'entity_type', array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'node', + 'length' => 255, + 'description' => 'The entity_type to which this comment is a reply.', + )); + db_add_field('comment', 'field_name', array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'comment', + 'length' => 255, + 'description' => 'The field_name to which this comment is a reply.', + )); + // Rename the nid column to entity_id. + db_change_field('comment', 'nid', 'entity_id', array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The entity_id to which this comment is a reply.', + )); + // Rename node_comment_statistics to comment_entity_statistics. + db_rename_table('node_comment_statistics', 'comment_entity_statistics'); + // Remove the statistics_node foreign key from entity_comment_statistics. + // @todo - how to remove foreign keys? + // Add the entity_type and field_name columns to comment_entity_statistics. + db_add_field('entity_comment_statistics', 'entity_type', array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'node', + 'length' => 255, + 'description' => 'The entity_type to which this comment is a reply.', + )); + db_add_field('entity_comment_statistics', 'field_name', array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'comment', + 'length' => 255, + 'description' => 'The field_name to which this comment is a reply.', + )); + // Rename the nid column in entity_comment_statistics to entity_id. + db_change_field('entity_comment_statistics', 'nid', 'entity_id', array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The entity_id to which this comment is a reply.', + )); + // Loop over defined node_types. + $node_types = array_keys(node_type_get_types()); + foreach ($node_types as $node_type) { + // Add a default comment field for existing node comments. + $field = array( + 'cardinality' => '1', + // We need one per node type to match the existing bundles. + 'field_name' => 'comment_' . $node_type, + 'module' => 'comment', + 'settings' => array(), + 'translatable' => '0', + 'type' => 'comment', + ); + // Make sure field doesn't already exist. + if (!field_info_field('comment_' . $node_type)) { + // Create the field. + field_create_field($field); + } + // Add the comment field, setting the instance settings to match those for the + // give node_type. + $instance = array( + 'bundle' => $node_type, + 'default_value' => variable_get('comment_' . $node_type, COMMENT_ENTITY_HIDDEN), + 'deleted' => '0', + 'description' => '', + 'display' => array( + 'default' => array( + 'label' => 'above', + 'module' => 'comment', + 'settings' => array(), + 'type' => 'comment_default', + 'weight' => '1', + ), + ), + 'entity_type' => 'node', + 'field_name' => 'comment_' . $node_type, + 'label' => 'Comment settings', + 'required' => 1, + 'settings' => array( + 'comment' => array( + 'comment' => variable_get('comment_' . $node_type, COMMENT_ENTITY_HIDDEN), + 'comment_default_mode' => variable_get('comment_default_mode_' . $node_type, COMMENT_MODE_THREADED), + 'comment_default_per_page' => variable_get('comment_default_per_page_' . $node_type, 50), + 'comment_anonymous' => variable_get('comment_anonymous_' . $node_type, COMMENT_ANONYMOUS_MAYNOT_CONTACT), + 'comment_subject_field' => variable_get('comment_subject_field_' . $node_type, 1), + 'comment_form_location' => variable_get('comment_form_location_' . $node_type, COMMENT_FORM_BELOW), + 'comment_preview' => variable_get('comment_preview_' . $node_type, DRUPAL_OPTIONAL) + ) + ), + 'widget' => array( + 'active' => 0, + 'module' => 'comment', + 'settings' => array(), + 'type' => 'comment_default', + 'weight' => '50', + ), + ); + field_create_instance($instance); + // Rename the comment bundle for this node type. + field_attach_rename_bundle('comment', 'comment_node_' . $node_type, 'comment_' . $node_type); + } + return t('Updated comment module to field api.'); +} + +/** + * Write values for existing nodes. + */ +function comment_update_8005(&$sandbox) { + // Load each node in batch and initialize field values for comment field. + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['current_nid'] = 0; + // We'll -1 to disregard the uid 0... + $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField(); + } + + $nids = db_select('node', 'n') + ->fields('n', array('nid')) + ->condition('nid', $sandbox['current_nid'], '>') + ->range(0, 50) + ->orderBy('nid', 'ASC') + ->execute() + ->fetchCol(); + + $nodes = node_load_multiple($nids); + + foreach ($nodes as $node) { + // We use the value of comment_old to populate the field. + $node->comment[$node->langcode][] = array('comment' => $node->comment_old); + $node->save(); + + $sandbox['progress']++; + $sandbox['current_nid'] = $node->nid; + } + + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); + + if ($sandbox['#finished']) { + // Node table will always exist up until here because in 7.x comment + // depends on node. + // Remove the {node}.comment field. + db_drop_field('node', 'comment_old'); + // Remove the {node_revision}.comment field. + db_drop_field('node_revision', 'comment_old'); + } + + return t('Set initial value of comment field for existing nodes.'); +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index df0a33a..6b3f19a 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -4,18 +4,22 @@ * @file * Enables users to comment on published content. * - * When enabled, the Comment module creates a discussion board for each Drupal - * node. Users can post comments to discuss a forum topic, story, collaborative - * book page, etc. + * When enabled, the Comment module creates a field that facilitates a + * discussion board for each Drupal entity to which a comment field is attached. + * Users can post comments to discuss a forum topic, story, collaborative + * book page, user etc. */ use Drupal\node\Node; use Drupal\file\File; -use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityInterfaceInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; +// Load all Field module hooks for Comment. +require_once DRUPAL_ROOT . '/core/modules/comment/comment.field.inc'; + /** * Comment is awaiting approval. */ @@ -62,19 +66,37 @@ const COMMENT_FORM_BELOW = 1; /** - * Comments for this node are hidden. + * Comments for this entity are hidden. */ -const COMMENT_NODE_HIDDEN = 0; +const COMMENT_ENTITY_HIDDEN = 0; /** - * Comments for this node are closed. + * Comments for this entity are closed. */ -const COMMENT_NODE_CLOSED = 1; +const COMMENT_ENTITY_CLOSED = 1; /** - * Comments for this node are open. + * Comments for this entity are open. + */ +const COMMENT_ENTITY_OPEN = 2; + +/** + * Denotes that access is denied for an entity to which a comment field is + * attached and no {%entity_type}_access function exists. + * + * Modules should return this value from hook_comment_entity_access() to deny + * access to commenting on an entity + */ +const COMMENT_ENTITY_ACCESS_DENY = 'deny'; + +/** + * Denotes the time cutoff for comments marked as read. + * + * Comments changed before this time are always marked as read. + * Comments changed after this time may be marked new, updated, or read, + * depending on their state for the current user. Defaults to 30 days ago. */ -const COMMENT_NODE_OPEN = 2; +define('COMMENT_NEW_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60); use Drupal\comment\Comment; @@ -89,7 +111,7 @@ function comment_help($path, $arg) { $output .= '

' . t('Uses') . '

'; $output .= '
'; $output .= '
' . t('Default and custom settings') . '
'; - $output .= '
' . t("Each content type can have its own default comment settings configured as: Open to allow new comments, Hidden to hide existing comments and prevent new comments, or Closed to view existing comments, but prevent new comments. These defaults will apply to all new content created (changes to the settings on existing content must be done manually). Other comment settings can also be customized per content type, and can be overridden for any given item of content. When a comment has no replies, it remains editable by its author, as long as the author has a user account and is logged in.", array('@content-type' => url('admin/structure/types'))) . '
'; + $output .= '
' . t("Comment functionality can be attached to any Drupal entity, eg a content type and the behavior can be customised to suit. Each entity can have its own default comment settings configured as: Open to allow new comments, Hidden to hide existing comments and prevent new comments, or Closed to view existing comments, but prevent new comments. These defaults will apply to all new content created (changes to the settings on existing content must be done manually). Other comment settings can also be customized per content type and entity, and can be overridden for any given item of content. When a comment has no replies, it remains editable by its author, as long as the author has a user account and is logged in.", array('@content-type' => url('admin/structure/types'))) . '
'; $output .= '
' . t('Comment approval') . '
'; $output .= '
' . t("Comments from users who have the Skip comment approval permission are published immediately. All other comments are placed in the Unapproved comments queue, until a user who has permission to Administer comments publishes or deletes them. Published comments can be bulk managed on the Published comments administration page.", array('@comment-approval' => url('admin/content/comment/approval'), '@admin-comment' => url('admin/content/comment'))) . '
'; $output .= '
'; @@ -98,10 +120,128 @@ function comment_help($path, $arg) { } /** + * Implements hook_entity_view(). + */ +function comment_entity_view($entity, $view_mode, $langcode) { + $fields = field_info_instances($entity->entityType(), $entity->bundle()); + foreach ($fields as $field_name => $instance) { + $links = array(); + $field = field_info_field($instance['field_name']); + if ($field['type'] != 'comment') { + continue; + } + $values = field_get_items($entity->entityType(), $entity, $field_name); + if ($values && is_array($values) && ($value = reset($values)) && + !empty($value['comment']) && + $value['comment'] != COMMENT_ENTITY_HIDDEN) { + $uri = $entity->uri(); + if ($view_mode == 'rss') { + // Add a comments RSS element which is a URL to the comments of this node. + if (!empty($uri['options'])) { + $uri['options']['fragment'] = 'comments'; + $uri['options']['absolute'] = TRUE; + } + $entity->rss_elements[] = array( + 'key' => 'comments', + 'value' => url($uri['path'], $uri['options']) + ); + } + elseif ($view_mode == 'teaser') { + // Teaser view: display the number of comments that have been posted, + // or a link to add new comments if the user has permission, the node + // is open to new comments, and there currently are none. + if (user_access('access comments')) { + if (!empty($entity->comment_statistics[$field_name]->comment_count)) { + $links['comment-comments'] = array( + 'title' => format_plural($entity->comment_statistics[$field_name]->comment_count, '1 comment', '@count comments'), + 'href' => $uri['path'], + 'attributes' => array('title' => t('Jump to the first comment of this posting.')), + 'fragment' => 'comments', + 'html' => TRUE, + ); + // Show a link to the first new comment. + if ($new = comment_num_new($entity->id(), $entity->entityType(), $field_name)) { + $links['comment-new-comments'] = array( + 'title' => format_plural($new, '1 new comment', '@count new comments'), + 'href' => $uri['path'], + 'query' => comment_new_page_count($entity->comment_statistics[$field_name]->comment_count, $new, $entity, $field_name), + 'attributes' => array('title' => t('Jump to the first new comment of this posting.')), + 'fragment' => 'new', + 'html' => TRUE, + ); + } + } + } + $values = field_get_items('node', $entity, $field_name); + $value = reset($values); + if (!empty($value['comment']) && $value['comment'] == COMMENT_ENTITY_OPEN) { + $comment_form_location = $instance['settings']['comment']['comment_form_location']; + if (user_access('post comments')) { + $links['comment-add'] = array( + 'title' => t('Add new comment'), + 'href' => $uri['path'], + 'attributes' => array('title' => t('Add a new comment to this page.')), + 'fragment' => 'comment-form', + ); + if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) { + $links['comment-add']['href'] = 'comment/reply/'. $entity->entityType() . '/' . $entity->id() .'/' . $field_name; + } + } + else { + $links['comment-forbidden'] = array( + 'title' => theme('comment_post_forbidden', array('entity' => $entity, 'field_name' => $field_name)), + 'html' => TRUE, + ); + } + } + } + elseif ($view_mode != 'search_index' && $view_mode != 'search_result') { + // Entity in other view modes: add a "post comment" link if the user is + // allowed to post comments and if this entity is allowing new comments. + // But we don't want this link if we're building the entity for search + // indexing or constructing a search result excerpt. + $values = field_get_items('node', $entity, $field_name); + $value = reset($values); + if (!empty($value['comment']) && $value['comment'] == COMMENT_ENTITY_OPEN) { + $comment_form_location = $instance['settings']['comment']['comment_form_location']; + if (user_access('post comments')) { + // Show the "post comment" link if the form is on another page, or + // if there are existing comments that the link will skip past. + if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($entity->comment_statistics[$field_name]->comment_count) && user_access('access comments'))) { + $links['comment-add'] = array( + 'title' => t('Add new comment'), + 'attributes' => array('title' => t('Share your thoughts and opinions related to this item.')), + 'href' => $uri['path'], + 'fragment' => 'comment-form', + ); + if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) { + $links['comment-add']['href'] = 'comment/reply/'. $entity->entityType() . '/' . $entity->id() .'/' . $field_name; + } + } + } + else { + $links['comment-forbidden'] = array( + 'title' => theme('comment_post_forbidden', array('entity' => $entity, 'field_name' => $field_name)), + 'html' => TRUE, + ); + } + } + } + } + + $entity->content['links']['comment__' . $field_name] = array( + '#theme' => 'links__entity__comment__' . $field_name, + '#links' => $links, + '#attributes' => array('class' => array('links', 'inline')), + ); + } +} + +/** * Implements hook_entity_info(). */ function comment_entity_info() { - $return = array( + $info = array( 'comment' => array( 'label' => t('Comment'), 'base table' => 'comment', @@ -114,7 +254,7 @@ function comment_entity_info() { 'entity class' => 'Drupal\comment\Comment', 'entity keys' => array( 'id' => 'cid', - 'bundle' => 'node_type', + 'bundle' => 'field_name', 'label' => 'subject', 'uuid' => 'uuid', ), @@ -130,47 +270,31 @@ function comment_entity_info() { ), ); - foreach (node_type_get_names() as $type => $name) { - $return['comment']['bundles']['comment_node_' . $type] = array( - 'label' => t('@node_type comment', array('@node_type' => $name)), - // Provide the node type/bundle name for other modules, so it does not - // have to be extracted manually from the bundle name. - 'node bundle' => $type, + foreach (comment_get_comment_fields() as $field_name => $field_info) { + $info['comment']['bundles'][$field_name] = array( + 'label' => $field_name, 'admin' => array( - // Place the Field UI paths for comments one level below the - // corresponding paths for nodes, so that they appear in the same set - // of local tasks. Note that the paths use a different placeholder name - // and thus a different menu loader callback, so that Field UI page - // callbacks get a comment bundle name from the node type in the URL. - // See comment_node_type_load() and comment_menu_alter(). - 'path' => 'admin/structure/types/manage/%comment_node_type/comment', - 'bundle argument' => 4, - 'real path' => 'admin/structure/types/manage/' . $type . '/comment', - 'access arguments' => array('administer content types'), + 'path' => 'admin/structure/comments/%comment_field_name', + 'bundle argument' => 3, + 'real path' => 'admin/structure/comments/' . strtr($field_name, '_', '-'), + 'access arguments' => array('administer comments'), ), ); } - return $return; + return $info; } /** - * Loads the comment bundle name corresponding a given content type. - * - * This function is used as a menu loader callback in comment_menu(). - * - * @param $name - * The machine name of the node type whose comment fields are to be edited. - * - * @return - * The comment bundle name corresponding to the node type. - * - * @see comment_menu_alter() + * Menu callback for loading the actual name of a comment field. */ -function comment_node_type_load($name) { - if ($type = node_type_load($name)) { - return 'comment_node_' . $type->type; +function comment_field_name_load($arg) { + $field_name = strtr($arg, array('-' => '_')); + if (($field = field_info_field($field_name)) && $field['type'] == 'comment') { + return $field_name; } + + return FALSE; } /** @@ -188,24 +312,21 @@ function comment_uri(Comment $comment) { */ function comment_field_extra_fields() { $return = array(); - - foreach (node_type_get_types() as $type) { - if (variable_get('comment_subject_field_' . $type->type, 1) == 1) { - $return['comment']['comment_node_' . $type->type] = array( - 'form' => array( - 'author' => array( - 'label' => t('Author'), - 'description' => t('Author textfield'), - 'weight' => -2, - ), - 'subject' => array( - 'label' => t('Subject'), - 'description' => t('Subject textfield'), - 'weight' => -1, - ), + foreach (comment_get_comment_fields() as $field_name => $field_info) { + $return['comment'][$field_name] = array( + 'form' => array( + 'author' => array( + 'label' => t('Author'), + 'description' => t('Author textfield'), + 'weight' => -2, ), - ); - } + 'subject' => array( + 'label' => t('Subject'), + 'description' => t('Subject textfield'), + 'weight' => -1, + ), + ), + ); } return $return; @@ -227,7 +348,7 @@ function comment_theme() { 'render element' => 'elements', ), 'comment_post_forbidden' => array( - 'variables' => array('node' => NULL), + 'variables' => array('entity' => NULL, 'field_name' => 'comment'), ), 'comment_wrapper' => array( 'template' => 'comment-wrapper', @@ -240,6 +361,13 @@ function comment_theme() { * Implements hook_menu(). */ function comment_menu() { + $items['admin/structure/comments'] = array( + 'title' => 'Comment bundles', + 'description' => 'Manage fields and displays settings for comment bundles.', + 'page callback' => 'comment_overview_bundles', + 'access arguments' => array('administer comments'), + 'file' => 'comment.admin.inc', + ); $items['admin/content/comment'] = array( 'title' => 'Comments', 'description' => 'List and edit site comments and the comment approval queue.', @@ -300,12 +428,13 @@ function comment_menu() { 'file' => 'comment.admin.inc', 'weight' => 2, ); - $items['comment/reply/%node'] = array( + $items['comment/reply/%/%comment_entity_reply/%'] = array( 'title' => 'Add new comment', 'page callback' => 'comment_reply', - 'page arguments' => array(2), - 'access callback' => 'node_access', - 'access arguments' => array('view', 2), + 'page arguments' => array(3, 4), + 'load arguments' => array('%map'), + 'access callback' => 'comment_entity_reply_access', + 'access arguments' => array(3), 'file' => 'comment.pages.inc', ); @@ -313,80 +442,70 @@ function comment_menu() { } /** - * Implements hook_menu_alter(). - */ -function comment_menu_alter(&$items) { - // Add comments to the description for admin/content. - $items['admin/content']['description'] = 'Administer content and comments.'; - - // Adjust the Field UI tabs on admin/structure/types/manage/[node-type]. - // See comment_entity_info(). - $items['admin/structure/types/manage/%comment_node_type/comment/fields']['title'] = 'Comment fields'; - $items['admin/structure/types/manage/%comment_node_type/comment/fields']['weight'] = 3; - $items['admin/structure/types/manage/%comment_node_type/comment/display']['title'] = 'Comment display'; - $items['admin/structure/types/manage/%comment_node_type/comment/display']['weight'] = 4; -} - -/** - * Returns a menu title which includes the number of unapproved comments. - */ -function comment_count_unpublished() { - $count = db_query('SELECT COUNT(cid) FROM {comment} WHERE status = :status', array( - ':status' => COMMENT_NOT_PUBLISHED, - ))->fetchField(); - return t('Unapproved comments (@count)', array('@count' => $count)); -} - -/** - * Implements hook_node_type_insert(). + * Dynamic menu loader callback for comment/reply/%/%commenty_entity/%. * - * Creates a comment body field for a node type created while the Comment module - * is enabled. For node types created before the Comment module is enabled, - * hook_modules_enabled() serves to create the body fields. + * Loads the entity given and entity id and entity type * - * @see comment_modules_enabled() + * @param array $args + * The menu args passed from the %map load argument + * + * @return Drupal\entity\Entity or FALSE if not found. */ -function comment_node_type_insert($info) { - _comment_body_field_create($info); +function comment_entity_reply_load($entity_id, $args) { + list(, ,$entity_type, $entity_id, $field_name) = $args; + return entity_load($entity_type, $entity_id); } /** - * Implements hook_node_type_update(). + * Access callback for testing user has access to view the subject of comments. + * + * Checks the user has view access to the entity which the comment reply is + * against. + + * @param Drupal\Core\Entity\EntityInterface $entity + * The entity which the comment field is attached to. + * + * @return bool + * TRUE or FALSE if the user has access */ -function comment_node_type_update($info) { - if (!empty($info->old_type) && $info->type != $info->old_type) { - field_attach_rename_bundle('comment', 'comment_node_' . $info->old_type, 'comment_node_' . $info->type); +function comment_entity_reply_access(Drupal\Core\Entity\EntityInterface $entity) { + $function = $entity->entityType() . '_access'; + // @todo replace this with entity access controls once generic access controller lands, + // @see http://drupal.org/node/1696660 + if (function_exists($function)) { + return $function('view', $entity); } + // We can't know how to control access to this entity, invoke + // hook_comment_entity_access and if no other modules object, grant access. + $access = module_invoke_all('comment_entity_access', $entity); + return !in_array(COMMENT_ENTITY_ACCESS_DENY, $access, TRUE); } /** - * Implements hook_node_type_delete(). + * Returns a menu title which includes the number of unapproved comments. */ -function comment_node_type_delete($info) { - field_attach_delete_bundle('comment', 'comment_node_' . $info->type); - $settings = array( - 'comment', - 'comment_default_mode', - 'comment_default_per_page', - 'comment_anonymous', - 'comment_subject_field', - 'comment_preview', - 'comment_form_location', - ); - foreach ($settings as $setting) { - variable_del($setting . '_' . $info->type); - } +function comment_count_unpublished() { + $count = db_query('SELECT COUNT(cid) FROM {comment} WHERE status = :status', array( + ':status' => COMMENT_NOT_PUBLISHED, + ))->fetchField(); + return t('Unapproved comments (@count)', array('@count' => $count)); } /** - * Creates a comment_body field instance for a given node type. + * Creates a comment_body field instance for a given entity type, bundle and + * field name. + * + * @param string $entity_type + * Entity type to which the comment field is attached. + * @param string $bundle + * Bundle of entity type to which comment field is attached + * @param string $field_name + * Name of the comment field attached to the entity type and bundle. * - * @param $info - * An object representing the content type. The only property that is - * currently used is $info->type, which is the machine name of the content - * type for which the body field (instance) is to be created. + * @todo make this trigger when the comment field is created, rather than the + * node type. */ -function _comment_body_field_create($info) { +function _comment_body_field_create($entity_type, $bundle, $field_name) { // Create the field if needed. if (!field_read_field('comment_body', array('include_inactive' => TRUE))) { $field = array( @@ -396,15 +515,15 @@ function _comment_body_field_create($info) { ); field_create_field($field); } - // Create the instance if needed. - if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) { - field_attach_create_bundle('comment', 'comment_node_' . $info->type); + // Create the instance if needed, field name defaults to 'comment'. + if (!field_read_instance('comment', 'comment_body', $field_name, array('include_inactive' => TRUE))) { + field_attach_create_bundle('comment', $bundle); // Attaches the body field by default. $instance = array( 'field_name' => 'comment_body', 'label' => 'Comment', 'entity_type' => 'comment', - 'bundle' => 'comment_node_' . $info->type, + 'bundle' => $field_name, 'settings' => array('text_processing' => 1), 'required' => TRUE, 'display' => array( @@ -505,14 +624,16 @@ function comment_block_view($delta = '') { * The comment listing set to the page on which the comment appears. */ function comment_permalink($cid) { - if (($comment = comment_load($cid)) && ($node = node_load($comment->nid))) { + if (($comment = comment_load($cid)) && ($entity = entity_load($comment->entity_type, $comment->entity_id))) { + $instance = field_info_instance($entity->entityType(), $comment->field_name, $entity->bundle()); // Find the current display page for this comment. - $page = comment_get_display_page($comment->cid, $node->type); + $page = comment_get_display_page($comment->cid, $instance); // @todo: Cleaner sub request handling. $request = drupal_container()->get('request'); - $subrequest = Request::create('/node/' . $node->nid, 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all()); + $uri = $entity->uri(); + $subrequest = Request::create($uri['path'], 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all()); $subrequest->query->set('page', $page); // @todo: Convert the pager to use the request object. $_GET['page'] = $page; @@ -530,16 +651,22 @@ function comment_permalink($cid) { * @return * An array of comment objects or an empty array if there are no recent * comments visible to the current user. + * + * @todo entity access for other entity types? */ function comment_get_recent($number = 10) { $query = db_select('comment', 'c'); - $query->innerJoin('node', 'n', 'n.nid = c.nid'); + $query->leftJoin('node', 'n', "n.nid = c.entity_id AND c.entity_type = 'node'"); $query->addTag('node_access'); + $query->addTag('comment_entity_access'); $query->addMetaData('base_table', 'comment'); $comments = $query ->fields('c') ->condition('c.status', COMMENT_PUBLISHED) - ->condition('n.status', NODE_PUBLISHED) + ->condition(db_or() + ->condition('n.status', NODE_PUBLISHED) + ->condition('n.status', NULL, 'IS NULL') + ) ->orderBy('c.created', 'DESC') // Additionally order by cid to ensure that comments with the same timestamp // are returned in the exact order posted. @@ -558,15 +685,18 @@ function comment_get_recent($number = 10) { * Number of comments. * @param $new_replies * Number of new replies. - * @param Drupal\node\Node $node - * The first new comment node. + * @param Drupal\Core\Entity\EntityInterface $entity + * The first new comment entity. + * @param string $field_name + * The field name * * @return * "page=X" if the page number is greater than zero; empty string otherwise. */ -function comment_new_page_count($num_comments, $new_replies, Node $node) { - $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED); - $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50); +function comment_new_page_count($num_comments, $new_replies, Drupal\Core\Entity\EntityInterface $entity, $field_name = 'comment') { + $instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()); + $mode = $instance['settings']['comment']['comment_default_mode']; + $comments_per_page = $instance['settings']['comment']['comment_default_per_page']; $pagenum = NULL; $flat = $mode == COMMENT_MODE_FLAT ? TRUE : FALSE; if ($num_comments <= $comments_per_page) { @@ -585,7 +715,9 @@ function comment_new_page_count($num_comments, $new_replies, Node $node) { // 1. Find all the threads with a new comment. $unread_threads_query = db_select('comment') ->fields('comment', array('thread')) - ->condition('nid', $node->nid) + ->condition('entity_id', $entity->id()) + ->condition('entity_type', $entity->entityType()) + ->condition('field_name', $field_name) ->condition('status', COMMENT_PUBLISHED) ->orderBy('created', 'DESC') ->orderBy('cid', 'DESC') @@ -603,9 +735,14 @@ function comment_new_page_count($num_comments, $new_replies, Node $node) { $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 nid = :nid AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array( + $count = db_query('SELECT COUNT(*) FROM {comment} 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( ':status' => COMMENT_PUBLISHED, - ':nid' => $node->nid, + ':entity_id' => $entity->id(), + ':field_name' => $field_name, + ':entity_type' => $entity->entityType(), ':thread' => $first_thread, ))->fetchField(); @@ -640,163 +777,8 @@ function theme_comment_block() { } /** - * Implements hook_node_view(). - */ -function comment_node_view(Node $node, $view_mode) { - $links = array(); - - if ($node->comment != COMMENT_NODE_HIDDEN) { - if ($view_mode == 'rss') { - // Add a comments RSS element which is a URL to the comments of this node. - $node->rss_elements[] = array( - 'key' => 'comments', - 'value' => url('node/' . $node->nid, array('fragment' => 'comments', 'absolute' => TRUE)) - ); - } - elseif ($view_mode == 'teaser') { - // Teaser view: display the number of comments that have been posted, - // or a link to add new comments if the user has permission, the node - // is open to new comments, and there currently are none. - if (user_access('access comments')) { - if (!empty($node->comment_count)) { - $links['comment-comments'] = array( - 'title' => format_plural($node->comment_count, '1 comment', '@count comments'), - 'href' => "node/$node->nid", - 'attributes' => array('title' => t('Jump to the first comment of this posting.')), - 'fragment' => 'comments', - 'html' => TRUE, - ); - // Show a link to the first new comment. - if ($new = comment_num_new($node->nid)) { - $links['comment-new-comments'] = array( - 'title' => format_plural($new, '1 new comment', '@count new comments'), - 'href' => "node/$node->nid", - 'query' => comment_new_page_count($node->comment_count, $new, $node), - 'attributes' => array('title' => t('Jump to the first new comment of this posting.')), - 'fragment' => 'new', - 'html' => TRUE, - ); - } - } - } - if ($node->comment == COMMENT_NODE_OPEN) { - $comment_form_location = variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW); - if (user_access('post comments')) { - $links['comment-add'] = array( - 'title' => t('Add new comment'), - 'href' => "node/$node->nid", - 'attributes' => array('title' => t('Add a new comment to this page.')), - 'fragment' => 'comment-form', - ); - if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) { - $links['comment-add']['href'] = "comment/reply/$node->nid"; - } - } - else { - $links['comment-forbidden'] = array( - 'title' => theme('comment_post_forbidden', array('node' => $node)), - 'html' => TRUE, - ); - } - } - } - elseif ($view_mode != 'search_index' && $view_mode != 'search_result') { - // Node in other view modes: add a "post comment" link if the user is - // allowed to post comments and if this node is allowing new comments. - // But we don't want this link if we're building the node for search - // indexing or constructing a search result excerpt. - if ($node->comment == COMMENT_NODE_OPEN) { - $comment_form_location = variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW); - if (user_access('post comments')) { - // Show the "post comment" link if the form is on another page, or - // if there are existing comments that the link will skip past. - if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($node->comment_count) && user_access('access comments'))) { - $links['comment-add'] = array( - 'title' => t('Add new comment'), - 'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')), - 'href' => "node/$node->nid", - 'fragment' => 'comment-form', - ); - if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) { - $links['comment-add']['href'] = "comment/reply/$node->nid"; - } - } - } - else { - $links['comment-forbidden'] = array( - 'title' => theme('comment_post_forbidden', array('node' => $node)), - 'html' => TRUE, - ); - } - } - } - - $node->content['links']['comment'] = array( - '#theme' => 'links__node__comment', - '#links' => $links, - '#attributes' => array('class' => array('links', 'inline')), - ); - - // Only append comments when we are building a node on its own node detail - // page. We compare $node and $page_node to ensure that comments are not - // appended to other nodes shown on the page, for example a node_reference - // displayed in 'full' view mode within another node. - if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) { - $node->content['comments'] = comment_node_page_additions($node); - } - } -} - -/** - * Builds the comment-related elements for node detail pages. - * - * @param Drupal\node\Node $node - * The node entity for which to build the comment-related elements. - * - * @return - * A renderable array representing the comment-related page elements for the - * node. - */ -function comment_node_page_additions(Node $node) { - $additions = array(); - - // Only attempt to render comments if the node has visible comments. - // Unpublished comments are not included in $node->comment_count, so show - // comments unconditionally if the user is an administrator. - if (($node->comment_count && user_access('access comments')) || user_access('administer comments')) { - $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED); - $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50); - if ($cids = comment_get_thread($node, $mode, $comments_per_page)) { - $comments = comment_load_multiple($cids); - comment_prepare_thread($comments); - $build = comment_view_multiple($comments); - $build['pager']['#theme'] = 'pager'; - $additions['comments'] = $build; - } - } - - // Append comment form if needed. - if (user_access('post comments') && $node->comment == COMMENT_NODE_OPEN && (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW)) { - $additions['comment_form'] = comment_add($node); - } - - if ($additions) { - $additions += array( - '#theme' => 'comment_wrapper__node_' . $node->type, - '#node' => $node, - 'comments' => array(), - 'comment_form' => array(), - ); - } - - return $additions; -} - -/** - * Returns a rendered form to comment the given node. + * Returns a rendered form to comment the given entity. * - * @param Drupal\node\Node $node - * The node entity to be commented. * @param int $pid * (optional) Some comments are replies to other comments. In those cases, * $pid is the parent comment's comment ID. Defaults to NULL. @@ -804,8 +786,8 @@ function comment_node_page_additions(Node $node) { * @return array * The renderable array for the comment addition form. */ -function comment_add(Node $node, $pid = NULL) { - $values = array('nid' => $node->nid, 'pid' => $pid, 'node_type' => 'comment_node_' . $node->type); +function comment_add(Drupal\Core\Entity\EntityInterface $entity, $field_name = 'comment', $pid = NULL) { + $values = array('entity_id' => $entity->id(), 'pid' => $pid, 'entity_type' => $entity->entityType(), 'field_name' => $field_name); $comment = entity_create('comment', $values); return entity_get_form($comment); } @@ -813,8 +795,10 @@ function comment_add(Node $node, $pid = NULL) { /** * Retrieves comments for a thread. * - * @param Drupal\node\Node $node - * The node whose comment(s) needs rendering. + * @param Drupal\Core\Entity\EntityInterface $entity + * The entity whose comment(s) needs rendering. + * @param string $field_name + * The field_name whose comment(s) needs rendering. * @param $mode * The comment display mode; COMMENT_MODE_FLAT or COMMENT_MODE_THREADED. * @param $comments_per_page @@ -877,26 +861,32 @@ function comment_add(Node $node, $pid = NULL) { * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need * to consider the trailing "/" so we use a substring only. */ -function comment_get_thread(Node $node, $mode, $comments_per_page) { +function comment_get_thread(Drupal\Core\Entity\EntityInterface $entity, $field_name, $mode, $comments_per_page) { $query = db_select('comment', 'c') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); $query->addField('c', 'cid'); $query - ->condition('c.nid', $node->nid) - ->addTag('node_access') + ->condition('c.entity_id', $entity->id()) + ->condition('c.entity_type', $entity->entityType()) + ->condition('c.field_name', $field_name) + ->addTag('entity_access') ->addTag('comment_filter') ->addMetaData('base_table', 'comment') - ->addMetaData('node', $node) + ->addMetaData('entity', $entity) + ->addMetaData('field_name', $field_name) ->limit($comments_per_page); $count_query = db_select('comment', 'c'); $count_query->addExpression('COUNT(*)'); $count_query - ->condition('c.nid', $node->nid) - ->addTag('node_access') + ->condition('c.entity_id', $entity->id()) + ->condition('c.entity_type', $entity->entityType()) + ->condition('c.field_name', $field_name) + ->addTag('entity_access') ->addTag('comment_filter') ->addMetaData('base_table', 'comment') - ->addMetaData('node', $node); + ->addMetaData('entity', $entity) + ->addMetaData('field_name', $field_name); if (!user_access('administer comments')) { $query->condition('c.status', COMMENT_PUBLISHED); @@ -969,8 +959,10 @@ function comment_prepare_thread(&$comments) { * * @param Drupal\comment\Comment $comment * The comment object. + * @param Drupal\Core\Entity\EntityInterface $entity + * The entity the comment is attached to. * @param $view_mode - * View mode, e.g. 'full', 'teaser'... + * (optional) View mode, e.g. 'full', 'teaser'... Defaults to 'full'. * @param $langcode * (optional) A language code to use for rendering. Defaults to the global * content language of the current request. @@ -987,15 +979,19 @@ function comment_view(Comment $comment, $view_mode = 'full', $langcode = NULL) { * * @param Drupal\comment\Comment $comment * The comment object. - * @param Drupal\node\Node $node - * The node the comment is attached to. + * @param Drupal\Core\Entity\EntityInterface $entity + * The entity the comment is attached to. + * @param string $field_name + * The field the comment is attached to. * * @return * A structured array of links. */ -function comment_links(Comment $comment, Node $node) { +function comment_links(Comment $comment, Drupal\Core\Entity\EntityInterface $entity, $field_name) { $links = array(); - if ($node->comment == COMMENT_NODE_OPEN) { + $items = field_get_items($entity->entityType(), $entity, $field_name); + $status = reset($items); + if ($status['comment'] == COMMENT_ENTITY_OPEN) { if (user_access('administer comments') && user_access('post comments')) { $links['comment-delete'] = array( 'title' => t('delete'), @@ -1009,7 +1005,7 @@ function comment_links(Comment $comment, Node $node) { ); $links['comment-reply'] = array( 'title' => t('reply'), - 'href' => "comment/reply/$comment->nid/$comment->cid", + 'href' => "comment/reply/$comment->entity_type/$comment->entity_id/$comment->field_name/$comment->cid", 'html' => TRUE, ); if ($comment->status == COMMENT_NOT_PUBLISHED) { @@ -1031,12 +1027,15 @@ function comment_links(Comment $comment, Node $node) { } $links['comment-reply'] = array( 'title' => t('reply'), - 'href' => "comment/reply/$comment->nid/$comment->cid", + 'href' => "comment/reply/$comment->entity_type/$comment->entity_id/$comment->field_name/$comment->cid", 'html' => TRUE, ); } else { - $links['comment-forbidden']['title'] = theme('comment_post_forbidden', array('node' => $node)); + $links['comment-forbidden']['title'] = theme('comment_post_forbidden', array( + 'entity' => $entity, + 'field_name' => $field_name + )); $links['comment-forbidden']['html'] = TRUE; } } @@ -1051,8 +1050,9 @@ function comment_links(Comment $comment, Node $node) { * @param $view_mode * View mode, e.g. 'full', 'teaser'... * @param $langcode - * A string indicating the language field values are to be shown in. If no - * language is provided the current content language is used. + * (optional) A string indicating the language field values are to be shown + * in. If no language is provided the current content language is used. + * Defaults to NULL. * * @return * An array in the format expected by drupal_render(). @@ -1064,249 +1064,98 @@ function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) } /** - * Implements hook_form_FORM_ID_alter(). + * Implements hook_entity_load(). */ -function comment_form_node_type_form_alter(&$form, $form_state) { - if (isset($form['type'])) { - $form['comment'] = array( - '#type' => 'fieldset', - '#title' => t('Comment settings'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#group' => 'additional_settings', - '#attributes' => array( - 'class' => array('comment-node-type-settings-form'), - ), - '#attached' => array( - 'library' => array('comment', 'drupal.comment'), - ), - ); - // Unlike coment_form_node_form_alter(), all of these settings are applied - // as defaults to all new nodes. Therefore, it would be wrong to use #states - // to hide the other settings based on the primary comment setting. - $form['comment']['comment'] = array( - '#type' => 'select', - '#title' => t('Default comment setting for new content'), - '#default_value' => variable_get('comment_' . $form['#node_type']->type, COMMENT_NODE_OPEN), - '#options' => array( - COMMENT_NODE_OPEN => t('Open'), - COMMENT_NODE_CLOSED => t('Closed'), - COMMENT_NODE_HIDDEN => t('Hidden'), - ), - ); - $form['comment']['comment_default_mode'] = array( - '#type' => 'checkbox', - '#title' => t('Threading'), - '#default_value' => variable_get('comment_default_mode_' . $form['#node_type']->type, COMMENT_MODE_THREADED), - '#description' => t('Show comment replies in a threaded list.'), - ); - $form['comment']['comment_default_per_page'] = array( - '#type' => 'select', - '#title' => t('Comments per page'), - '#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50), - '#options' => _comment_per_page(), - ); - $form['comment']['comment_anonymous'] = array( - '#type' => 'select', - '#title' => t('Anonymous commenting'), - '#default_value' => variable_get('comment_anonymous_' . $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT), - '#options' => array( - COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'), - COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'), - COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'), - ), - '#access' => user_access('post comments', drupal_anonymous_user()), - ); - $form['comment']['comment_subject_field'] = array( - '#type' => 'checkbox', - '#title' => t('Allow comment title'), - '#default_value' => variable_get('comment_subject_field_' . $form['#node_type']->type, 1), - ); - $form['comment']['comment_form_location'] = array( - '#type' => 'checkbox', - '#title' => t('Show reply form on the same page as comments'), - '#default_value' => variable_get('comment_form_location_' . $form['#node_type']->type, COMMENT_FORM_BELOW), - ); - $form['comment']['comment_preview'] = array( - '#type' => 'radios', - '#title' => t('Preview comment'), - '#default_value' => variable_get('comment_preview_' . $form['#node_type']->type, DRUPAL_OPTIONAL), - '#options' => array( - DRUPAL_DISABLED => t('Disabled'), - DRUPAL_OPTIONAL => t('Optional'), - DRUPAL_REQUIRED => t('Required'), - ), - ); - } -} - -/** - * Implements hook_form_BASE_FORM_ID_alter(). - */ -function comment_form_node_form_alter(&$form, $form_state) { - $node = $form_state['controller']->getEntity($form_state); - $form['comment_settings'] = array( - '#type' => 'fieldset', - '#access' => user_access('administer comments'), - '#title' => t('Comment settings'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#group' => 'additional_settings', - '#attributes' => array( - 'class' => array('comment-node-settings-form'), - ), - '#attached' => array( - 'library' => array('comment', 'drupal.comment'), - ), - '#weight' => 30, - ); - $comment_count = isset($node->nid) ? db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField() : 0; - $comment_settings = ($node->comment == COMMENT_NODE_HIDDEN && empty($comment_count)) ? COMMENT_NODE_CLOSED : $node->comment; - $form['comment_settings']['comment'] = array( - '#type' => 'radios', - '#title' => t('Comments'), - '#title_display' => 'invisible', - '#parents' => array('comment'), - '#default_value' => $comment_settings, - '#options' => array( - COMMENT_NODE_OPEN => t('Open'), - COMMENT_NODE_CLOSED => t('Closed'), - COMMENT_NODE_HIDDEN => t('Hidden'), - ), - COMMENT_NODE_OPEN => array( - '#description' => t('Users with the "Post comments" permission can post comments.'), - ), - COMMENT_NODE_CLOSED => array( - '#description' => t('Users cannot post comments, but existing comments will be displayed.'), - ), - COMMENT_NODE_HIDDEN => array( - '#description' => t('Comments are hidden from view.'), - ), - ); - // If the node doesn't have any comments, the "hidden" option makes no - // sense, so don't even bother presenting it to the user. - if (empty($comment_count)) { - $form['comment_settings']['comment'][COMMENT_NODE_HIDDEN]['#access'] = FALSE; - // Also adjust the description of the "closed" option. - $form['comment_settings']['comment'][COMMENT_NODE_CLOSED]['#description'] = t('Users cannot post comments.'); - } -} - -/** - * Implements hook_node_load(). - */ -function comment_node_load($nodes, $types) { - $comments_enabled = array(); - - // Check if comments are enabled for each node. If comments are disabled, - // assign values without hitting the database. - foreach ($nodes as $node) { - // Store whether comments are enabled for this node. - if ($node->comment != COMMENT_NODE_HIDDEN) { - $comments_enabled[] = $node->nid; - } - else { - $node->cid = 0; - $node->last_comment_timestamp = $node->created; - $node->last_comment_name = ''; - $node->last_comment_uid = $node->uid; - $node->comment_count = 0; +function comment_entity_load($entities, $entity_type) { + // Load comment information from the database and add it to the entity's + // comment_statistics property, which is an array keyed by field_name. + $result = db_select('comment_entity_statistics', 'ces') + ->fields('ces') + ->condition('ces.entity_id', array_keys($entities)) + ->condition('ces.entity_type', $entity_type) + ->execute(); + foreach ($result as $record) { + if (empty($entities[$record->entity_id]->comment_statistics)) { + $entities[$record->entity_id]->comment_statistics = array(); } - } - - // For nodes with comments enabled, fetch information from the database. - if (!empty($comments_enabled)) { - $result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled)); - foreach ($result as $record) { - $nodes[$record->nid]->cid = $record->cid; - $nodes[$record->nid]->last_comment_timestamp = $record->last_comment_timestamp; - $nodes[$record->nid]->last_comment_name = $record->last_comment_name; - $nodes[$record->nid]->last_comment_uid = $record->last_comment_uid; - $nodes[$record->nid]->comment_count = $record->comment_count; + if (empty($entities[$record->entity_id]->comment_statistics[$record->field_name])) { + $entities[$record->entity_id]->comment_statistics[$record->field_name] = new StdClass(); } + $comment_statistics = &$entities[$record->entity_id]->comment_statistics[$record->field_name]; + $comment_statistics->cid = $record->cid; + $comment_statistics->last_comment_timestamp = $record->last_comment_timestamp; + $comment_statistics->last_comment_name = $record->last_comment_name; + $comment_statistics->last_comment_uid = $record->last_comment_uid; + $comment_statistics->comment_count = $record->comment_count; } } /** - * Implements hook_node_prepare(). + * Implements hook_entity_insert(). */ -function comment_node_prepare(Node $node) { - if (!isset($node->comment)) { - $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN); - } -} - -/** - * Implements hook_node_insert(). - */ -function comment_node_insert(Node $node) { +function comment_entity_insert($entity) { + global $user; // Allow bulk updates and inserts to temporarily disable the - // maintenance of the {node_comment_statistics} table. - if (variable_get('comment_maintain_node_statistics', TRUE)) { - db_insert('node_comment_statistics') - ->fields(array( - 'nid' => $node->nid, - 'cid' => 0, - 'last_comment_timestamp' => $node->changed, - 'last_comment_name' => NULL, - 'last_comment_uid' => $node->uid, - 'comment_count' => 0, - )) - ->execute(); + // maintenance of the {comment_entity_statistics} table. + $fields = comment_get_comment_fields(); + if (state()->get('comment_maintain_entity_statistics', TRUE)) { + $query = db_insert('comment_entity_statistics') + ->fields(array( + 'entity_id', + 'entity_type', + 'field_name', + 'cid', + 'last_comment_timestamp', + 'last_comment_name', + 'last_comment_uid', + 'comment_count' + )); + $execute = FALSE; + foreach ($fields as $field_name => $detail) { + if (!empty($entity->{$field_name})) { + // There is at least one comment field, the query needs to be executed. + $execute = TRUE; + $query->values(array( + 'entity_id' => $entity->id(), + 'entity_type' => $entity->entityType(), + 'field_name' => $field_name, + 'cid' => 0, + // Default to REQUEST_TIME when entity does not have a changed property. + 'last_comment_timestamp' => isset($entity->changed) ? $entity->changed : REQUEST_TIME, + 'last_comment_name' => NULL, + // Default to current user when entity does not have a uid property. + 'last_comment_uid' => isset($entity->uid) ? $entity->uid : $user->uid, + 'comment_count' => 0, + )); + } + } + if ($execute) { + $query->execute(); + } } } /** - * Implements hook_node_predelete(). + * Implements hook_entity_predelete(). */ -function comment_node_predelete(Node $node) { - $cids = db_query('SELECT cid FROM {comment} WHERE nid = :nid', array(':nid' => $node->nid))->fetchCol(); +function comment_entity_predelete($entity) { + $cids = db_select('comment', 'c') + ->fields('c', array('cid')) + ->condition('entity_id', $entity->id()) + ->condition('entity_type', $entity->entityType()) + ->execute() + ->fetchCol(); comment_delete_multiple($cids); - db_delete('node_comment_statistics') - ->condition('nid', $node->nid) + db_delete('comment_entity_statistics') + ->condition('entity_id', $entity->id()) + ->condition('entity_type', $entity->entityType()) ->execute(); } /** - * Implements hook_node_update_index(). - */ -function comment_node_update_index(Node $node, $langcode) { - $index_comments = &drupal_static(__FUNCTION__); - - if ($index_comments === NULL) { - // Find and save roles that can 'access comments' or 'search content'. - $perms = array('access comments' => array(), 'search content' => array()); - $result = db_query("SELECT rid, permission FROM {role_permission} WHERE permission IN ('access comments', 'search content')"); - foreach ($result as $record) { - $perms[$record->permission][$record->rid] = $record->rid; - } - - // Prevent indexing of comments if there are any roles that can search but - // not view comments. - $index_comments = TRUE; - foreach ($perms['search content'] as $rid) { - if (!isset($perms['access comments'][$rid]) && (($rid == DRUPAL_AUTHENTICATED_RID || $rid == DRUPAL_ANONYMOUS_RID) || !isset($perms['access comments'][DRUPAL_AUTHENTICATED_RID]))) { - $index_comments = FALSE; - break; - } - } - } - - if ($index_comments) { - $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED); - $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50); - if ($node->comment && $cids = comment_get_thread($node, $mode, $comments_per_page)) { - $comments = comment_load_multiple($cids); - comment_prepare_thread($comments); - $build = comment_view_multiple($comments, $langcode); - return drupal_render($build); - } - } - return ''; -} - -/** * Implements hook_update_index(). + * + * @todo - find out what this is for? */ function comment_update_index() { // Store the maximum possible comments per thread (used for ranking by reply count) @@ -1314,24 +1163,6 @@ function comment_update_index() { } /** - * Implements hook_node_search_result(). - * - * Formats a comment count string and returns it, for display with search - * results. - */ -function comment_node_search_result(Node $node) { - // Do not make a string if comments are hidden. - if (user_access('access comments') && $node->comment != COMMENT_NODE_HIDDEN) { - $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField(); - // Do not make a string if comments are closed and there are currently - // zero comments. - if ($node->comment != COMMENT_NODE_CLOSED || $comments > 0) { - return array('comment' => format_plural($comments, '1 comment', '@count comments')); - } - } -} - -/** * Implements hook_user_cancel(). */ function comment_user_cancel($edit, $account, $method) { @@ -1432,7 +1263,7 @@ function comment_delete_multiple($cids) { * An array of comment objects, indexed by comment ID. * * @see entity_load() - * @see Drupal\Core\Entity\EntityFieldQuery + * @see Drupal\Core\Entity\EntityInterfaceFieldQuery */ function comment_load_multiple(array $cids = NULL, $reset = FALSE) { return entity_load_multiple('comment', $cids, $reset); @@ -1457,8 +1288,12 @@ function comment_load($cid, $reset = FALSE) { /** * Gets the number of new comments for the current user and the specified node. * - * @param $nid - * Node ID to count comments for. + * @param integer $entity_id + * Entity ID to count comments for. + * @param string $entity_type + * The entity type to count comments for. + * @param string $field_name + * (optional) The field_name to count comments for. Defaults to NULL. * @param $timestamp * Time to count from (defaults to time of last user access * to node). @@ -1466,22 +1301,40 @@ function comment_load($cid, $reset = FALSE) { * @return * The number of new comments or FALSE if the user is not logged in. */ -function comment_num_new($nid, $timestamp = 0) { +function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) { global $user; if ($user->uid) { - // Retrieve the timestamp at which the current user last viewed this node. + // Retrieve the timestamp at which the current user last viewed this entity. if (!$timestamp) { - $timestamp = node_last_viewed($nid); + $function = $entity_type . '_last_viewed'; + if (function_exists($function)) { + $timestamp = $function($entity_id); + } + else { + // Default to 30 days ago. + $timestamp = COMMENT_NEW_LIMIT; + // Let other modules alter this value. + // @todo document this hook. + drupal_alter('comment_num_new_timestamp', $timestamp, $entity_id, $entity_type, $field_name); + } } - $timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT); + $timestamp = ($timestamp > COMMENT_NEW_LIMIT ? $timestamp : COMMENT_NEW_LIMIT); // Use the timestamp to retrieve the number of new comments. - return db_query('SELECT COUNT(cid) FROM {comment} WHERE nid = :nid AND created > :timestamp AND status = :status', array( - ':nid' => $nid, - ':timestamp' => $timestamp, - ':status' => COMMENT_PUBLISHED, - ))->fetchField(); + $query = db_select('comment', 'c'); + $query->addExpression('COUNT(cid)'); + $query->condition('c.entity_type', $entity_type) + ->condition('c.entity_id', $entity_id) + ->condition('c.status', COMMENT_PUBLISHED) + ->condition('c.created', $timestamp, '>'); + if ($field_name) { + // Limit to a particular field. + $query->condition('c.field_name', $field_name); + } + + return $query->execute() + ->fetchField(); } else { return FALSE; @@ -1495,27 +1348,28 @@ function comment_num_new($nid, $timestamp = 0) { * Count the number of comments which appear before the comment we want to * display, taking into account display settings and threading. * - * @param $cid + * @param integer $cid * The comment ID. - * @param $node_type - * The node type of the comment's parent. + * @param array $instance + * Field instance as returned from field_info_instance. * * @return * The display ordinal for the comment. * * @see comment_get_display_page() + * @see field_info_instance(). */ -function comment_get_display_ordinal($cid, $node_type) { +function comment_get_display_ordinal($cid, $instance) { // 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.nid = c1.nid'); + $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->addExpression('COUNT(*)', 'count'); $query->condition('c2.cid', $cid); if (!user_access('administer comments')) { $query->condition('c1.status', COMMENT_PUBLISHED); } - $mode = variable_get('comment_default_mode_' . $node_type, COMMENT_MODE_THREADED); + $mode = $instance['settings']['comment']['comment_default_mode']; if ($mode == COMMENT_MODE_FLAT) { // For flat comments, cid is used for ordering comments due to @@ -1541,15 +1395,15 @@ function comment_get_display_ordinal($cid, $node_type) { * * @param $cid * The comment ID. - * @param $node_type - * The node type the comment is attached to. + * @param array $instance + * Field instance as returned from field_info_instance(). * * @return * The page number. */ -function comment_get_display_page($cid, $node_type) { - $ordinal = comment_get_display_ordinal($cid, $node_type); - $comments_per_page = variable_get('comment_default_per_page_' . $node_type, 50); +function comment_get_display_page($cid, $instance) { + $ordinal = comment_get_display_ordinal($cid, $instance); + $comments_per_page = $instance['settings']['comment']['comment_default_per_page']; return floor($ordinal / $comments_per_page); } @@ -1574,6 +1428,7 @@ function comment_edit_page(Comment $comment) { function comment_preview(Comment $comment) { global $user; $preview_build = array(); + $entity = entity_load($comment->entity_type, $comment->entity_id); if (!form_get_errors()) { $comment_body = field_get_items('comment', $comment, 'comment_body'); @@ -1608,13 +1463,18 @@ function comment_preview(Comment $comment) { if ($comment->pid) { $build = array(); - $comment = comment_load($comment->pid); - if ($comment && $comment->status == COMMENT_PUBLISHED) { - $build = comment_view($comment); + $parent = comment_load($comment->pid); + if ($parent && $parent->status == COMMENT_PUBLISHED) { + $build = comment_view($parent); } } else { - $build = node_view(node_load($comment->nid)); + // We temporarily disable rendering of this field to prevent infinite + // loop. + $values = $entity->{$comment->field_name}; + unset($entity->{$comment->field_name}); + $build = entity_view($entity, 'full'); + $entity->{$comment->field_name} = $values; } $preview_build['comment_output_below'] = $build; @@ -1639,9 +1499,9 @@ function comment_preprocess_block(&$variables) { */ function template_preprocess_comment(&$variables) { $comment = $variables['elements']['#comment']; - $node = $variables['elements']['#node']; + $comment_entity = entity_load($comment->entity_type, $comment->entity_id); $variables['comment'] = $comment; - $variables['node'] = $node; + $variables['comment_entity'] = $comment_entity; $variables['author'] = theme('username', array('account' => $comment)); $variables['created'] = format_date($comment->created); $variables['changed'] = format_date($comment->changed); @@ -1685,8 +1545,8 @@ function template_preprocess_comment(&$variables) { $variables['attributes']['class'][] = 'by-anonymous'; } else { - if ($comment->uid == $variables['node']->uid) { - $variables['attributes']['class'][] = 'by-node-author'; + if (!empty($comment_entity->uid) && $comment->uid == $comment_entity->uid) { + $variables['attributes']['class'][] = 'by-' . $comment_entity->entityType() . '-author'; } if ($comment->uid == $variables['user']->uid) { $variables['attributes']['class'][] = 'by-viewer'; @@ -1699,12 +1559,14 @@ function template_preprocess_comment(&$variables) { * * @param $variables * An associative array containing: - * - node: The comment node. + * - entity: The comment entity. * * @ingroup themeable */ function theme_comment_post_forbidden($variables) { - $node = $variables['node']; + $entity = $variables['entity']; + $field_name = $variables['field_name']; + $instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()); global $user; // Since this is expensive to compute, we cache it so that a page with many @@ -1722,11 +1584,12 @@ function theme_comment_post_forbidden($variables) { if ($authenticated_post_comments) { // We cannot use drupal_get_destination() because these links // sometimes appear on /node and taxonomy listing pages. - if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_SEPARATE_PAGE) { - $destination = array('destination' => "comment/reply/$node->nid#comment-form"); + if ($instance['settings']['comment']['comment_form_location'] == COMMENT_FORM_SEPARATE_PAGE) { + $destination = array('destination' => 'comment/reply/' . $entity->entityType() . '/' . $entity->id() . '/' . $field_name .'#comment-form'); } else { - $destination = array('destination' => "node/$node->nid#comment-form"); + $uri = $entity->uri(); + $destination = array('destination' => $uri['path'] . "#comment-form"); } if (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { @@ -1748,8 +1611,8 @@ function theme_comment_post_forbidden($variables) { */ function template_preprocess_comment_wrapper(&$variables) { // Provide contextual information. - $variables['node'] = $variables['content']['#node']; - $variables['display_mode'] = variable_get('comment_default_mode_' . $variables['node']->type, COMMENT_MODE_THREADED); + $variables['entity'] = $variables['content']['#entity']; + $variables['display_mode'] = $variables['content']['#display_mode']; // The comment form is optional and may not exist. $variables['content'] += array('comment_form' => array()); } @@ -1966,12 +1829,16 @@ function comment_ranking() { 'title' => t('Number of comments'), 'join' => array( 'type' => 'LEFT', - 'table' => 'node_comment_statistics', - 'alias' => 'node_comment_statistics', - 'on' => 'node_comment_statistics.nid = i.sid', + 'table' => 'comment_entity_statistics', + 'alias' => 'ces', + // Default to comment field as this is the most common use case for + // nodes. + 'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_name = 'comment'", ), // Inverse law that maps the highest reply count on the site to 1 and 0 to 0. - 'score' => '2.0 - 2.0 / (1.0 + node_comment_statistics.comment_count * CAST(:scale AS DECIMAL))', + 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * CAST(:scale AS DECIMAL))', + // @todo convert this to config() or state(). + 'arguments' => array(':scale' => variable_get('node_cron_comments_scale', 0)), ), ); @@ -2025,8 +1892,16 @@ function comment_rdf_mapping() { function comment_file_download_access($field, EntityInterface $entity, File $file) { if ($entity->entityType() == 'comment') { if (user_access('access comments') && $entity->status == COMMENT_PUBLISHED || user_access('administer comments')) { - $node = node_load($entity->nid); - return node_access('view', $node); + // @todo replace this with entity access controls once generic access controller lands, + // @see http://drupal.org/node/1696660 + $function = $entity->entity_type . '_access'; + if (function_exists($function)) { + $entity = entity_load($entity->entity_type, $entity->entity_id); + return $function('view', $entity); + } + // Return NULL as there is no entity access callback for this entity + // type, we rely on other modules intervening. + return; } return FALSE; } @@ -2040,7 +1915,7 @@ function comment_library_info() { 'title' => 'Comment', 'version' => VERSION, 'js' => array( - drupal_get_path('module', 'comment') . '/comment-node-form.js' => array(), + drupal_get_path('module', 'comment') . '/comment-entity-form.js' => array(), ), 'dependencies' => array( array('system', 'jquery'), @@ -2051,3 +1926,141 @@ function comment_library_info() { return $libraries; } + +/** + * Utility function to return an array of comment fields. + * + * @return array + * An array of comment field definitions, keyed by field name. Each field has + * an additional property, 'bundles', which is an array of all the bundles to + * which this field belongs, keyed by entity type. + * + * @see field_info_fields(). + */ +function comment_get_comment_fields() { + return array_filter(field_info_fields(), function ($value) { + if ($value['type'] == 'comment') { + return TRUE; + } + }); +} + +/** + * Decide on the type of marker to be shown for a comment. + */ +function comment_mark(Comment $comment) { + global $user; + $cache = &drupal_static(__FUNCTION__, array()); + $cid = $comment->entity_id . '__' . $comment->entity_type; + + if (!$user->uid) { + return MARK_READ; + } + if (!isset($cache[$cid])) { + $function = $comment->entity_type . '_last_viewed'; + if (function_exists($function)) { + $cache[$cid] = $function($comment->entity_id); + } + else { + // @todo - decide how to handle last viewed for other entities. For now + // assume REQUEST_TIME. + $cache[$cid] = REQUEST_TIME; + } + } + if ($cache[$cid] == 0 && $comment->changed > COMMENT_NEW_LIMIT) { + return MARK_NEW; + } + elseif ($comment->changed > $cache[$cid] && $comment->changed > COMMENT_NEW_LIMIT) { + return MARK_UPDATED; + } + return MARK_READ; +} + +/** + * Utility method to add the default comment field to an entity. + * + * Attaches a comment field named 'comment' to the given entity type and bundle. + * Largely replicates the default behaviour in Drupal 7 and earlier. + * + * @param string $entity_type + * The entity type to attach the default comment field to. + * @param string $bundle + * The bundle to attach the default comment field instance to. + */ +function comment_add_default_comment_field($entity_type, $bundle) { + // Make sure field doesn't already exist. + if (!field_info_field('comment')) { + // Add a default comment field for existing node comments. + $field = array( + 'cardinality' => '1', + 'field_name' => 'comment', + 'module' => 'comment', + 'settings' => array(), + 'translatable' => '0', + 'type' => 'comment', + ); + // Create the field. + field_create_field($field); + } + // Make sure the instance doesn't already exist. + if (!field_info_instance($entity_type, 'comment', $bundle)) { + $instance = array( + 'bundle' => $bundle, + 'default_value' => COMMENT_ENTITY_HIDDEN, + 'deleted' => '0', + 'description' => '', + 'display' => array( + 'default' => array( + 'label' => 'hidden', + 'module' => 'comment', + 'settings' => array(), + 'type' => 'comment_default', + 'weight' => '1', + ), + 'rss' => array( + 'type' => 'hidden', + 'label' => 'hidden', + ), + 'teaser' => array( + 'type' => 'hidden', + 'label' => 'hidden', + ) + ), + 'entity_type' => $entity_type, + 'field_name' => 'comment', + 'label' => 'Comment settings', + 'required' => 1, + 'settings' => array( + 'comment' => array( + 'comment' => COMMENT_ENTITY_OPEN, + 'comment_default_mode' => COMMENT_MODE_THREADED, + 'comment_default_per_page' => 50, + 'comment_anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT, + 'comment_subject_field' => 1, + 'comment_form_location' => COMMENT_FORM_BELOW, + 'comment_preview' => DRUPAL_OPTIONAL + ) + ), + 'widget' => array( + 'active' => 0, + 'module' => 'comment', + 'settings' => array(), + 'type' => 'comment_default', + 'weight' => '50', + ), + ); + field_create_instance($instance); + } + _comment_body_field_create($entity_type, $bundle, 'comment'); +} + +/** + * Implements hook_field_insert(). + */ +function comment_field_create_instance($instance) { + $field = field_info_field($instance['field_name']); + if ($field['type'] == 'comment') { + _comment_body_field_create($instance['entity_type'], $instance['bundle'], $instance['field_name']); + cache()->delete('comment_entity_info'); + } +} diff --git a/core/modules/comment/comment.pages.inc b/core/modules/comment/comment.pages.inc index 3d1ecb9..ed622e7 100644 --- a/core/modules/comment/comment.pages.inc +++ b/core/modules/comment/comment.pages.inc @@ -5,7 +5,7 @@ * User page callbacks for the Comment module. */ -use Drupal\node\Node; +use Drupal\core\Entity\Entity; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -14,40 +14,46 @@ * * There are several cases that have to be handled, including: * - replies to comments - * - replies to nodes - * - attempts to reply to nodes that can no longer accept comments + * - replies to entities + * - attempts to reply to entities that can no longer accept comments * - respecting access permissions ('access comments', 'post comments', etc.) * - * The node or comment that is being replied to must appear above the comment + * The entity or comment that is being replied to must appear above the comment * form to provide the user context while authoring the comment. * - * @param Drupal\node\Node $node - * Every comment belongs to a node. This is that node. + * @param Drupal\Core\Entity\EntityInterface $entity + * Every comment belongs to an entity. This is that entity. + * @param string $field_name + * The field_name to which the commment belongs. * @param $pid * (optional) Some comments are replies to other comments. In those cases, * $pid is the parent comment's comment ID. Defaults to NULL. * * @return array * An associative array containing: - * - An array for rendering the node or parent comment. - * - comment_node: If the comment is a reply to the node. + * - An array for rendering the entity or parent comment. + * - comment_entity: If the comment is a reply to the entity. * - comment_parent: If the comment is a reply to another comment. * - comment_form: The comment form as a renderable array. */ -function comment_reply(Node $node, $pid = NULL) { +function comment_reply(Drupal\Core\Entity\EntityInterface $entity, $field_name, $pid = NULL) { + $instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()); // Set the breadcrumb trail. - drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->label(), 'node/' . $node->nid))); + // @todo - test behaviour when entities don't have uris. + $uri = $entity->uri(); + drupal_set_breadcrumb(array(l(t('Home'), NULL), l($entity->label(), $uri['path'], $uri['options']))); $op = isset($_POST['op']) ? $_POST['op'] : ''; $build = array(); // The user is previewing a comment prior to submitting it. if ($op == t('Preview')) { if (user_access('post comments')) { - $build['comment_form'] = comment_add($node, $pid); + $build['comment_form'] = comment_add($entity, $field_name, $pid); } else { drupal_set_message(t('You are not authorized to post comments.'), 'error'); - drupal_goto("node/$node->nid"); + $uri = $entity->uri(); + drupal_goto($uri['path']); } } else { @@ -59,43 +65,54 @@ function comment_reply(Node $node, $pid = NULL) { if ($comment->status == COMMENT_PUBLISHED) { // If that comment exists, make sure that the current comment and the // parent comment both belong to the same parent node. - if ($comment->nid != $node->nid) { - // Attempting to reply to a comment not belonging to the current nid. + if ($comment->entity_id != $entity->id() || + $comment->field_name != $field_name || + $comment->entity_type != $entity->entityType()) { + // Attempting to reply to a comment not belonging to the current entity. drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); - drupal_goto("node/$node->nid"); + drupal_goto($uri['path']); } // Display the parent comment - $comment->node_type = 'comment_node_' . $node->type; + $comment->entity_type = $entity->entityType(); + $comment->field_name = $field_name; field_attach_load('comment', array($comment->cid => $comment)); $comment->name = $comment->uid ? $comment->registered_name : $comment->name; $build['comment_parent'] = comment_view($comment); } else { drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); - drupal_goto("node/$node->nid"); + drupal_goto($uri['path']); } } else { drupal_set_message(t('You are not authorized to view comments.'), 'error'); - drupal_goto("node/$node->nid"); + drupal_goto($uri['path']); } } - // This is the case where the comment is in response to a node. Display the node. + // This is the case where the comment is in response to a entity. Display the entity. + // @todo replace this with entity access controls once generic access controller lands, + // @see http://drupal.org/node/1696660 elseif (user_access('access content')) { - $build['comment_node'] = node_view($node); + // We make sure the field value isn't set so we don't end up with a redirect loop. + $original = $entity->{$field_name}; + unset($entity->{$field_name}); + $build['comment_entity'] = entity_view($entity, 'full'); + $entity->{$field_name} = $original; } // Should we show the reply box? - if ($node->comment != COMMENT_NODE_OPEN) { + $items = field_get_items($entity->entityType(), $entity, $field_name); + $status = reset($items); + if (isset($status['comment']) && $status['comment'] != COMMENT_ENTITY_OPEN) { drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error'); - drupal_goto("node/$node->nid"); + drupal_goto($uri['path']); } elseif (user_access('post comments')) { - $build['comment_form'] = comment_add($node, $pid); + $build['comment_form'] = comment_add($entity, $field_name, $pid); } else { drupal_set_message(t('You are not authorized to post comments.'), 'error'); - drupal_goto("node/$node->nid"); + drupal_goto($uri['path']); } } @@ -124,7 +141,11 @@ function comment_approve($cid) { comment_save($comment); drupal_set_message(t('Comment approved.')); - drupal_goto('node/' . $comment->nid); + $entity = entity_load($comment->entity_type, $comment->entity_id); + if ($entity) { + $uri = $entity->uri(); + drupal_goto($uri['path']); + } } throw new NotFoundHttpException(); } diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index c77cb67..594f0b0 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -16,13 +16,14 @@ function comment_token_info() { ); // Comment-related tokens for nodes - $node['comment-count'] = array( + // @todo make this work per field. + $entity['comment-count'] = array( 'name' => t("Comment count"), - 'description' => t("The number of comments posted on a node."), + 'description' => t("The number of comments posted on an entity."), ); - $node['comment-count-new'] = array( + $entity['comment-count-new'] = array( 'name' => t("New comment count"), - 'description' => t("The number of comments posted on a node since the reader last viewed it."), + 'description' => t("The number of comments posted on an entity since the reader last viewed it."), ); // Core comment tokens @@ -79,9 +80,15 @@ function comment_token_info() { 'description' => t("The comment's parent, if comment threading is active."), 'type' => 'comment', ); + $comment['entity'] = array( + 'name' => t("Entity"), + 'description' => t("The entity the comment was posted to."), + 'type' => 'entity', + ); + // Provide for backwards compatability as no upgrade path available. $comment['node'] = array( 'name' => t("Node"), - 'description' => t("The node the comment was posted to."), + 'description' => t("The Node the comment was posted to."), 'type' => 'node', ); $comment['author'] = array( @@ -93,7 +100,7 @@ function comment_token_info() { return array( 'types' => array('comment' => $type), 'tokens' => array( - 'node' => $node, + 'entity' => $entity, 'comment' => $comment, ), ); @@ -193,17 +200,33 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = $replacements[$original] = format_date($comment->changed, 'medium', '', NULL, $langcode); break; - case 'node': - $node = node_load($comment->nid); - $title = $node->label(); + case 'entity': + $entity = entity_load($comment->entity_type, $comment->entity_id); + $title = $entity->label(); $replacements[$original] = $sanitize ? filter_xss($title) : $title; break; + + case 'node': + if ($comment->entity_type== 'node') { + $entity = entity_load($comment->entity_type, $comment->entity_id); + $title = $entity->label(); + $replacements[$original] = $sanitize ? filter_xss($title) : $title; + } + else { + $replacements[$original] = NULL; + } + break; } } // Chained token relationships. - if ($node_tokens = token_find_with_prefix($tokens, 'node')) { - $node = node_load($comment->nid); + if ($entity_tokens = token_find_with_prefix($tokens, 'entity')) { + $entity = entity_load($comment->entity_type, $comment->entity_id); + $replacements += token_generate($comment->entity_type, $entity_tokens, array($comment->entity_type => $entity), $options); + } + + if (($node_tokens = token_find_with_prefix($tokens, 'node')) && $comment->entity_type == 'node') { + $node = node_load($comment->entity_id); $replacements += token_generate('node', $node_tokens, array('node' => $node), $options); } @@ -223,17 +246,21 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = $replacements += token_generate('user', $author_tokens, array('user' => $account), $options); } } - elseif ($type == 'node' & !empty($data['node'])) { - $node = $data['node']; + elseif ($type == 'entity' & !empty($data['entity'])) { + $entity = $data['entity']; foreach ($tokens as $name => $original) { switch($name) { case 'comment-count': - $replacements[$original] = $node->comment_count; + $count = 0; + foreach ($entity->comment_statistics as $field_name => $statistics) { + $count += $statistics->comment_count; + } + $replacements[$original] = $count; break; case 'comment-count-new': - $replacements[$original] = comment_num_new($node->nid); + $replacements[$original] = comment_num_new($entity->id(), $entity->entityType()); break; } } diff --git a/core/modules/comment/lib/Drupal/comment/Comment.php b/core/modules/comment/lib/Drupal/comment/Comment.php index ef6c16a..98926cf 100644 --- a/core/modules/comment/lib/Drupal/comment/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Comment.php @@ -30,6 +30,27 @@ class Comment extends Entity implements ContentEntityInterface { public $uuid; /** + * The entity ID to which this comment is attached. + * + * @var integer + */ + public $entity_id; + + /** + * The entity type to which this comment is attached. + * + * @var string + */ + public $entity_type; + + /** + * The field to which this comment is attached. + * + * @var string + */ + public $field_name = 'comment'; + + /** * The parent comment ID if this is a reply to a comment. * * @var integer @@ -91,6 +112,13 @@ class Comment extends Entity implements ContentEntityInterface { public $homepage; /** + * The entity which this comment is attached. + * + * @var Drupal\Core\Entity\EntityInterface + */ + protected $entity; + + /** * Implements Drupal\Core\Entity\EntityInterface::id(). */ public function id() { @@ -101,6 +129,6 @@ public function id() { * Implements Drupal\Core\Entity\EntityInterface::bundle(). */ public function bundle() { - return $this->node_type; + return $this->field_name; } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index cead0cd..06cc19f 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -21,14 +21,15 @@ class CommentFormController extends EntityFormController { public function form(array $form, array &$form_state, EntityInterface $comment) { global $user; - $node = node_load($comment->nid); - $form_state['comment']['node'] = $node; + $entity = entity_load($comment->entity_type, $comment->entity_id); + $instance = field_info_instance($comment->entity_type, $comment->field_name, $entity->bundle()); + $form_state['comment']['entity'] = $entity; - // Use #comment-form as unique jump target, regardless of node type. + // Use #comment-form as unique jump target, regardless of entity type. $form['#id'] = drupal_html_id('comment_form'); - $form['#theme'] = array('comment_form__node_' . $node->type, 'comment_form'); + $form['#theme'] = array('comment_form__' . $comment->entity_type . '__' . $entity->bundle() . '__' . $comment->field_name, 'comment_form'); - $anonymous_contact = variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT); + $anonymous_contact = $instance['settings']['comment']['comment_anonymous']; $is_admin = (!empty($comment->cid) && user_access('administer comments')); if (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) { @@ -37,9 +38,9 @@ public function form(array $form, array &$form_state, EntityInterface $comment) } // If not replying to a comment, use our dedicated page callback for new - // comments on nodes. + // comments on entities. if (empty($comment->cid) && empty($comment->pid)) { - $form['#action'] = url('comment/reply/' . $comment->nid); + $form['#action'] = url('comment/reply/' . $comment->entity_type . '/' . $comment->entity_id . '/' . $comment->field_name); } if (isset($form_state['comment_preview'])) { @@ -158,7 +159,7 @@ public function form(array $form, array &$form_state, EntityInterface $comment) '#title' => t('Subject'), '#maxlength' => 64, '#default_value' => $comment->subject, - '#access' => variable_get('comment_subject_field_' . $node->type, 1) == 1, + '#access' => $instance['settings']['comment']['comment_subject_field'], ); // Used for conditional validation of author fields. @@ -168,13 +169,12 @@ public function form(array $form, array &$form_state, EntityInterface $comment) ); // Add internal comment properties. - foreach (array('cid', 'pid', 'nid', 'uid') as $key) { + foreach (array('cid', 'pid', 'entity_id', 'entity_type', 'field_name', 'uid') as $key) { $form[$key] = array('#type' => 'value', '#value' => $comment->$key); } - $form['node_type'] = array('#type' => 'value', '#value' => 'comment_node_' . $node->type); - // Make the comment inherit the current content language unless specifically - // set. + // Make the comment inherit the entity language unless specifically set. + $comment_langcode = $comment->langcode; if ($comment->isNew()) { $language_content = language(LANGUAGE_TYPE_CONTENT); $comment->langcode = $language_content->langcode; @@ -185,9 +185,6 @@ public function form(array $form, array &$form_state, EntityInterface $comment) '#value' => $comment->langcode, ); - // Attach fields. - $comment->node_type = 'comment_node_' . $node->type; - return parent::form($form, $form_state, $comment); } @@ -197,8 +194,9 @@ public function form(array $form, array &$form_state, EntityInterface $comment) protected function actions(array $form, array &$form_state) { $element = parent::actions($form, $form_state); $comment = $this->getEntity($form_state); - $node = $form_state['comment']['node']; - $preview_mode = variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL); + $entity = $form_state['comment']['entity']; + $instance = field_info_instance($comment->entity_type, $comment->field_name, $entity->bundle()); + $preview_mode = $instance['settings']['comment']['comment_preview']; // No delete action on the comment form. unset($element['delete']); @@ -206,7 +204,6 @@ protected function actions(array $form, array &$form_state) { // Only show the save button if comment previews are optional or if we are // already previewing the submission. $element['submit']['#access'] = ($comment->cid && user_access('administer comments')) || $preview_mode != DRUPAL_REQUIRED || isset($form_state['comment_preview']); - $element['preview'] = array( '#type' => 'submit', '#value' => t('Preview'), @@ -332,10 +329,15 @@ public function preview(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityFormController::save(). */ public function save(array $form, array &$form_state) { - $node = node_load($form_state['values']['nid']); + $entity = entity_load($form_state['values']['entity_type'], $form_state['values']['entity_id']); $comment = $this->getEntity($form_state); + $field_name = $form_state['values']['field_name']; + $instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()); + $items = field_get_items($entity->entityType(), $entity, $field_name); + $status = reset($items); + $uri = $entity->uri(); - if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) { + if (user_access('post comments') && (user_access('administer comments') || $status['comment'] == COMMENT_ENTITY_OPEN)) { // Save the anonymous user information to a cookie for reuse. if (user_is_anonymous()) { user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))); @@ -358,18 +360,18 @@ public function save(array $form, array &$form_state) { } $query = array(); // Find the current display page for this comment. - $page = comment_get_display_page($comment->cid, $node->type); + $page = comment_get_display_page($comment->cid, $instance); if ($page > 0) { $query['page'] = $page; } - // Redirect to the newly posted comment. - $redirect = array('node/' . $node->nid, array('query' => $query, 'fragment' => 'comment-' . $comment->cid)); + // Redirect to the newly posted comment. @todo up to here. + $redirect = array($uri['path'], array('query' => $query, 'fragment' => 'comment-' . $comment->cid) + $uri['options']); } else { watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject), WATCHDOG_WARNING); drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject)), 'error'); - // Redirect the user to the node they are commenting on. - $redirect = 'node/' . $node->nid; + // Redirect the user to the entity they are commenting on. + $redirect = $uri['path']; } $form_state['redirect'] = $redirect; // Clear the block and page caches so that anonymous users see the comment diff --git a/core/modules/comment/lib/Drupal/comment/CommentRenderController.php b/core/modules/comment/lib/Drupal/comment/CommentRenderController.php index c614f8e..b7c7765 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentRenderController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentRenderController.php @@ -19,7 +19,7 @@ class CommentRenderController extends EntityRenderController { * Overrides Drupal\Core\Entity\EntityRenderController::buildContent(). * * In addition to modifying the content key on entities, this implementation - * will also set the node key which all comments carry. + * will also set the comment entity key which all comments carry. */ public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL) { $return = array(); @@ -30,12 +30,12 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la parent::buildContent($entities, $view_mode, $langcode); foreach ($entities as $entity) { - $node = node_load($entity->nid); - if (!$node) { - throw new \InvalidArgumentException(t('Invalid node for comment.')); + $comment_entity = entity_load($entity->entity_type, $entity->entity_id); + if (!$comment_entity) { + throw new \InvalidArgumentException(t('Invalid entity for comment.')); } - $entity->content['#node'] = $node; - $entity->content['#theme'] = 'comment__node_' . $node->bundle(); + $entity->content['#entity'] = $entity; + $entity->content['#theme'] = 'comment__' . $entity->entity_type . '__' . $comment_entity->bundle() . '__' . $entity->field_name; $entity->content['links'] = array( '#theme' => 'links__comment', '#pre_render' => array('drupal_pre_render_links'), @@ -44,8 +44,8 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la if (empty($entity->in_preview)) { $entity->content['links'][$this->entityType] = array( '#theme' => 'links__comment__comment', - // The "node" property is specified to be present, so no need to check. - '#links' => comment_links($entity, $node), + // The "entity" property is specified to be present, so no need to check. + '#links' => comment_links($entity, $comment_entity, $entity->field_name), '#attributes' => array('class' => array('links', 'inline')), ); } @@ -59,8 +59,10 @@ protected function alterBuild(array &$build, EntityInterface $comment, $view_mod parent::alterBuild($build, $comment, $view_mode, $langcode); if (empty($comment->in_preview)) { $prefix = ''; + $comment_entity = entity_load($comment->entity_type, $comment->entity_id); + $instance = field_info_instance($comment_entity->entityType(), $comment->field_name, $comment_entity->bundle()); $is_threaded = isset($comment->divs) - && variable_get('comment_default_mode_' . $comment->bundle(), COMMENT_MODE_THREADED) == COMMENT_MODE_THREADED; + && $instance['settings']['comment']['comment_default_mode'] == COMMENT_MODE_THREADED; // Add 'new' anchor if needed. if (!empty($comment->first_new)) { diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index 4d447a5..4ab6db4 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -29,9 +29,7 @@ class CommentStorageController extends DatabaseStorageController { */ protected function buildQuery($ids, $revision_id = FALSE) { $query = parent::buildQuery($ids, $revision_id); - // Specify additional fields from the user and node tables. - $query->innerJoin('node', 'n', 'base.nid = n.nid'); - $query->addField('n', 'type', 'node_type'); + // Specify additional fields from the user table. $query->innerJoin('users', 'u', 'base.uid = u.uid'); $query->addField('u', 'name', 'registered_name'); $query->fields('u', array('uid', 'signature', 'signature_format', 'picture')); @@ -45,8 +43,7 @@ protected function attachLoad(&$comments, $load_revision = FALSE) { // Set up standard comment properties. foreach ($comments as $key => $comment) { $comment->name = $comment->uid ? $comment->registered_name : $comment->name; - $comment->new = node_mark($comment->nid, $comment->changed); - $comment->node_type = 'comment_node_' . $comment->node_type; + $comment->new = comment_mark($comment); $comments[$key] = $comment; } parent::attachLoad($comments, $load_revision); @@ -64,11 +61,6 @@ protected function preSave(EntityInterface $comment) { if (!isset($comment->status)) { $comment->status = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED; } - // Make sure we have a proper bundle name. - if (!isset($comment->node_type)) { - $node = node_load($comment->nid); - $comment->node_type = 'comment_node_' . $node->type; - } if (!$comment->cid) { // Add the comment to database. This next section builds the thread field. // Also see the documentation for comment_view(). @@ -85,7 +77,13 @@ protected function preSave(EntityInterface $comment) { if ($comment->pid == 0) { // This is a comment with no parent comment (depth 0): we start // by retrieving the maximum thread level. - $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $comment->nid))->fetchField(); + $query = db_select('comment', 'c') + ->condition('entity_id', $comment->entity_id) + ->condition('field_name', $comment->field_name) + ->condition('entity_type', $comment->entity_type); + $query->addExpression('MAX(thread)', 'thread'); + $max = $query->execute() + ->fetchField(); // Strip the "/" from the end of the thread. $max = rtrim($max, '/'); // We need to get the value at the correct depth. @@ -103,10 +101,14 @@ protected function preSave(EntityInterface $comment) { $parent->thread = (string) rtrim((string) $parent->thread, '/'); $prefix = $parent->thread . '.'; // Get the max value in *this* thread. - $max = db_query("SELECT MAX(thread) FROM {comment} WHERE thread LIKE :thread AND nid = :nid", array( - ':thread' => $parent->thread . '.%', - ':nid' => $comment->nid, - ))->fetchField(); + $query = db_select('comment', 'c') + ->condition('entity_id', $comment->entity_id) + ->condition('field_name', $comment->field_name) + ->condition('entity_type', $comment->entity_type) + ->condition('thread', $parent->thread . '.%', 'LIKE'); + $query->addExpression('MAX(thread)', 'thread'); + $max = $query->execute() + ->fetchField(); if ($max == '') { // First child of this parent. As the other two cases do an @@ -128,7 +130,7 @@ protected function preSave(EntityInterface $comment) { // has the lock, just move to the next integer. do { $thread = $prefix . comment_int_to_alphadecimal(++$n) . '/'; - } while (!lock()->acquire("comment:$comment->nid:$thread")); + } while (!lock()->acquire("comment:$comment->entity_id:$comment->entity_type:$thread")); $this->threadLock = $thread; } if (empty($comment->created)) { @@ -153,8 +155,8 @@ protected function preSave(EntityInterface $comment) { */ protected function postSave(EntityInterface $comment, $update) { $this->releaseThreadLock(); - // Update the {node_comment_statistics} table prior to executing the hook. - $this->updateNodeStatistics($comment->nid); + // Update the {comment_entity_statistics} table prior to executing the hook. + $this->updateEntityStatistics($comment); if ($comment->status == COMMENT_PUBLISHED) { module_invoke_all('comment_publish', $comment); } @@ -172,45 +174,56 @@ protected function postDelete($comments) { comment_delete_multiple($child_cids); foreach ($comments as $comment) { - $this->updateNodeStatistics($comment->nid); + $this->updateEntityStatistics($comment); } } /** - * Updates the comment statistics for a given node. + * Updates the comment statistics for a given entity. * - * The {node_comment_statistics} table has the following fields: - * - last_comment_timestamp: The timestamp of the last comment for this node, - * or the node created timestamp if no comments exist for the node. + * The {comment_entity_statistics} table has the following fields: + * - last_comment_timestamp: The timestamp of the last comment for this entity, + * or the entity created timestamp if no comments exist for the entity. * - last_comment_name: The name of the anonymous poster for the last comment. * - last_comment_uid: The user ID of the poster for the last comment for - * this node, or the node author's user ID if no comments exist for the - * node. + * this entity, or the entity author's user ID if no comments exist for the + * entity. * - comment_count: The total number of approved/published comments on this - * node. + * entity. * - * @param $nid - * The node ID. + * @param Drupal\comment\Comment $comment + * The comment being saved. */ - protected function updateNodeStatistics($nid) { + protected function updateEntityStatistics($comment) { + global $user; // Allow bulk updates and inserts to temporarily disable the - // maintenance of the {node_comment_statistics} table. - if (!variable_get('comment_maintain_node_statistics', TRUE)) { + // maintenance of the {comment_entity_statistics} table. + if (!state()->get('comment_maintain_entity_statistics', TRUE)) { return; } - $count = db_query('SELECT COUNT(cid) FROM {comment} WHERE nid = :nid AND status = :status', array( - ':nid' => $nid, - ':status' => COMMENT_PUBLISHED, - ))->fetchField(); + $query = db_select('comment', 'c'); + $query->addExpression('COUNT(cid)'); + $count = $query->condition('c.entity_id', $comment->entity_id) + ->condition('c.entity_type', $comment->entity_type) + ->condition('c.field_name', $comment->field_name) + ->condition('c.status', COMMENT_PUBLISHED) + ->execute() + ->fetchField(); if ($count > 0) { // Comments exist. - $last_reply = db_query_range('SELECT cid, name, changed, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array( - ':nid' => $nid, - ':status' => COMMENT_PUBLISHED, - ))->fetchObject(); - db_update('node_comment_statistics') + $last_reply = db_select('comment', 'c') + ->fields('c', array('cid', 'name', 'changed', 'uid')) + ->condition('c.entity_id', $comment->entity_id) + ->condition('c.entity_type', $comment->entity_type) + ->condition('c.field_name', $comment->field_name) + ->condition('c.status', COMMENT_PUBLISHED) + ->orderBy('c.created', 'DESC') + ->range(0, 1) + ->execute() + ->fetchObject(); + db_update('comment_entity_statistics') ->fields(array( 'cid' => $last_reply->cid, 'comment_count' => $count, @@ -218,21 +231,29 @@ protected function updateNodeStatistics($nid) { 'last_comment_name' => $last_reply->uid ? '' : $last_reply->name, 'last_comment_uid' => $last_reply->uid, )) - ->condition('nid', $nid) + ->condition('entity_id', $comment->entity_id) + ->condition('entity_type', $comment->entity_type) + ->condition('field_name', $comment->field_name) ->execute(); } else { // Comments do not exist. - $node = db_query('SELECT uid, created FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject(); - db_update('node_comment_statistics') + $entity = entity_load($comment->entity_type, $comment->entity_id); + db_update('comment_entity_statistics') ->fields(array( 'cid' => 0, 'comment_count' => 0, - 'last_comment_timestamp' => $node->created, + // Use created date of entity or default to REQUEST_TIME if none + // exists. + 'last_comment_timestamp' => isset($entity->created) ? $entity->created : REQUEST_TIME, 'last_comment_name' => '', - 'last_comment_uid' => $node->uid, + // @todo refactor when http://drupal.org/node/585838 lands. + // Get uid from entity or default to logged in user if none exists. + 'last_comment_uid' => isset($entity->uid) ? $entity->uid : $user->uid, )) - ->condition('nid', $nid) + ->condition('entity_id', $comment->entity_id) + ->condition('entity_type', $comment->entity_type) + ->condition('field_name', $comment->field_name) ->execute(); } } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentAnonymousTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentAnonymousTest.php index 5e46be4..6d10043 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentAnonymousTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentAnonymousTest.php @@ -60,7 +60,7 @@ function testAnonymous() { $this->drupalLogout(); // Post anonymous comment with contact info (optional). - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); $anonymous_comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName()); @@ -74,7 +74,7 @@ function testAnonymous() { 'subject' => $this->randomName(), "comment_body[$langcode][0][value]" => $this->randomName(), ); - $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save')); + $this->drupalPost('comment/reply/node/' . $this->node->nid . '/comment', $edit, t('Save')); $this->assertText(t('The name you used belongs to a registered user.')); // Require contact info. @@ -83,7 +83,7 @@ function testAnonymous() { $this->drupalLogout(); // Try to post comment with contact info (required). - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE); @@ -137,7 +137,7 @@ function testAnonymous() { $this->assertNoLink('Add new comment', 'Link to add comment was found.'); // Attempt to view node-comment form while disallowed. - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $this->assertText('You are not authorized to post comments', 'Error attempting to post comment.'); $this->assertNoFieldByName('subject', '', 'Subject field not found.'); $this->assertNoFieldByName("comment_body[$langcode][0][value]", '', 'Comment field not found.'); @@ -162,7 +162,7 @@ function testAnonymous() { $this->assertFieldByName('subject', '', 'Subject field found.'); $this->assertFieldByName("comment_body[$langcode][0][value]", '', 'Comment field found.'); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $anonymous_comment3->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $anonymous_comment3->id); $this->assertText('You are not authorized to view comments', 'Error attempting to post reply.'); $this->assertNoText($author_name, 'Comment not displayed.'); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php index 5cf0720..4533b3d 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php @@ -45,11 +45,13 @@ function testCommentClasses() { foreach ($permutations as $case) { // Create a new node. - $node = $this->drupalCreateNode(array('type' => 'article', 'uid' => $case['node_uid'])); + $node = $this->drupalCreateNode(array('type' => 'article', 'uid' => $case['node_uid'], 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); // Add a comment. $comment = entity_create('comment', array( - 'nid' => $node->nid, + 'entity_id' => $node->nid, + 'entity_type' => 'node', + 'field_name' => 'comment', 'uid' => $case['comment_uid'], 'status' => $case['comment_status'], 'subject' => $this->randomName(), diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php index ab03d41..4122861 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php @@ -34,15 +34,14 @@ function testCommentDefaultFields() { // Do not make assumptions on default node types created by the test // installation profile, and create our own. $this->drupalCreateContentType(array('type' => 'test_node_type')); + comment_add_default_comment_field('node', 'test_node_type'); - // Check that the 'comment_body' field is present on all comment bundles. - $instances = field_info_instances('comment'); - foreach (node_type_get_types() as $type_name => $info) { - $this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name))); + // Check that the 'comment_body' field is present on the comment bundle. + $instance = field_info_instance('comment', 'comment_body', 'comment'); + $this->assertTrue(!empty($instance), 'The comment_body field is added when a comment bundle is created'); - // Delete the instance along the way. - field_delete_instance($instances['comment_node_' . $type_name]['comment_body']); - } + // Delete the instance. + field_delete_instance($instance); // Check that the 'comment_body' field is deleted. $field = field_info_field('comment_body'); @@ -51,56 +50,14 @@ function testCommentDefaultFields() { // Create a new content type. $type_name = 'test_node_type_2'; $this->drupalCreateContentType(array('type' => $type_name)); + comment_add_default_comment_field('node', $type_name); // Check that the 'comment_body' field exists and has an instance on the // new comment bundle. $field = field_info_field('comment_body'); $this->assertTrue($field, 'The comment_body field exists'); $instances = field_info_instances('comment'); - $this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name))); - } - - /** - * Tests that comment module works when enabled after a content module. - */ - function testCommentEnable() { - // Create a user to do module administration. - $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules')); - $this->drupalLogin($this->admin_user); - - // Disable the comment module. - $edit = array(); - $edit['modules[Core][comment][enable]'] = FALSE; - $this->drupalPost('admin/modules', $edit, t('Save configuration')); - $this->resetAll(); - $this->assertFalse(module_exists('comment'), 'Comment module disabled.'); - - // Enable core content type modules (book, and poll). - $edit = array(); - $edit['modules[Core][book][enable]'] = 'book'; - $edit['modules[Core][poll][enable]'] = 'poll'; - $this->drupalPost('admin/modules', $edit, t('Save configuration')); - - // Now enable the comment module. - $edit = array(); - $edit['modules[Core][comment][enable]'] = 'comment'; - $this->drupalPost('admin/modules', $edit, t('Save configuration')); - $this->resetAll(); - $this->assertTrue(module_exists('comment'), 'Comment module enabled.'); - - // Create nodes of each type. - $book_node = $this->drupalCreateNode(array('type' => 'book')); - $poll_node = $this->drupalCreateNode(array('type' => 'poll', 'active' => 1, 'runtime' => 0, 'choice' => array(array('chtext' => '')))); - - $this->drupalLogout(); - - // Try to post a comment on each node. A failure will be triggered if the - // comment body is missing on one of these forms, due to postComment() - // asserting that the body is actually posted correctly. - $this->web_user = $this->drupalCreateUser(array('access content', 'access comments', 'post comments', 'skip comment approval')); - $this->drupalLogin($this->web_user); - $this->postComment($book_node, $this->randomName(), $this->randomName()); - $this->postComment($poll_node, $this->randomName(), $this->randomName()); + $this->assertTrue(isset($instances['comment']['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name))); } /** @@ -109,8 +66,9 @@ function testCommentEnable() { function testCommentFormat() { // Disable text processing for comments. $this->drupalLogin($this->admin_user); - $edit = array('instance[settings][text_processing]' => 0); - $this->drupalPost('admin/structure/types/manage/article/comment/fields/comment_body', $edit, t('Save settings')); + $instance = field_info_instance('comment', 'comment_body', 'comment'); + $instance['settings']['text_processing'] = 0; + field_update_instance($instance); // Post a comment without an explicit subject. $this->drupalLogin($this->web_user); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php index 1070107..ba8bdc9 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php @@ -90,7 +90,7 @@ function testCommentInterface() { // Reply to comment #2 creating comment #3 with optional preview and no // subject though field enabled. $this->drupalLogin($this->web_user); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $this->assertText($subject_text, 'Individual comment-reply subject found.'); $this->assertText($comment_text, 'Individual comment-reply body found.'); $reply = $this->postComment(NULL, $this->randomName(), '', TRUE); @@ -100,7 +100,7 @@ function testCommentInterface() { $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.00/', $reply_loaded->thread, 'Thread of reply grows correctly.'); // Second reply to comment #3 creating comment #4. - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $this->assertText($subject_text, 'Individual comment-reply subject found.'); $this->assertText($comment_text, 'Individual comment-reply body found.'); $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); @@ -128,27 +128,27 @@ function testCommentInterface() { // Attempt to reply to an unpublished comment. $reply_loaded->status = COMMENT_NOT_PUBLISHED; $reply_loaded->save(); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $reply_loaded->cid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $reply_loaded->cid); $this->assertText(t('The comment you are replying to does not exist.'), 'Replying to an unpublished comment'); // Attempt to post to node with comments disabled. - $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN)); + $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_HIDDEN))))); $this->assertTrue($this->node, 'Article node created.'); - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $this->assertText('This discussion is closed', 'Posting to node with comments disabled'); $this->assertNoField('edit-comment', 'Comment body field found.'); // Attempt to post to node with read-only comments. - $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_CLOSED)); + $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_CLOSED))))); $this->assertTrue($this->node, 'Article node created.'); - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $this->assertText('This discussion is closed', 'Posting to node with comments read-only'); $this->assertNoField('edit-comment', 'Comment body field found.'); // Attempt to post to node with comments enabled (check field names etc). - $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN)); + $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); $this->assertTrue($this->node, 'Article node created.'); - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled'); $this->assertField('edit-comment-body-' . $langcode . '-0-value', 'Comment body field found.'); @@ -178,5 +178,4 @@ function testCommentInterface() { $this->drupalLogin($this->admin_user); $this->setCommentForm(FALSE); } - } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php index a704fb7..9385651 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php @@ -121,7 +121,9 @@ function testCommentLanguage() { // Check that comment language matches the current content language. $cid = db_select('comment', 'c') ->fields('c', array('cid')) - ->condition('nid', $node->nid) + ->condition('entity_id', $node->nid) + ->condition('entity_type', 'node') + ->condition('field_name', 'comment') ->orderBy('cid', 'DESC') ->range(0, 1) ->execute() diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index e1dcbec..78aa527 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -53,7 +53,7 @@ function testCommentLinks() { // test; there is only a difference between open and closed registration. 'user_register' => array(USER_REGISTER_VISITORS, USER_REGISTER_ADMINISTRATORS_ONLY), // @todo Complete test coverage for: - //'comments' => array(COMMENT_NODE_OPEN, COMMENT_NODE_CLOSED, COMMENT_NODE_HIDDEN), + //'comments' => array(COMMENT_ENTITY_OPEN, COMMENT_ENTITY_CLOSED, COMMENT_ENTITY_HIDDEN), //// COMMENT_ANONYMOUS_MUST_CONTACT is irrelevant for this test. //'contact ' => array(COMMENT_ANONYMOUS_MAY_CONTACT, COMMENT_ANONYMOUS_MAYNOT_CONTACT), ); @@ -80,8 +80,8 @@ function testCommentLinks() { * USER_REGISTER_VISITORS. * - contact: COMMENT_ANONYMOUS_MAY_CONTACT or * COMMENT_ANONYMOUS_MAYNOT_CONTACT. - * - comments: COMMENT_NODE_OPEN, COMMENT_NODE_CLOSED, or - * COMMENT_NODE_HIDDEN. + * - comments: COMMENT_ENTITY_OPEN, COMMENT_ENTITY_CLOSED, or + * COMMENT_ENTITY_HIDDEN. * - User permissions: * These are granted or revoked for the user, according to the * 'authenticated' flag above. Pass 0 or 1 as parameter values. See @@ -102,7 +102,7 @@ function setEnvironment(array $info) { 'form' => COMMENT_FORM_BELOW, 'user_register' => USER_REGISTER_VISITORS, 'contact' => COMMENT_ANONYMOUS_MAY_CONTACT, - 'comments' => COMMENT_NODE_OPEN, + 'comments' => COMMENT_ENTITY_OPEN, 'access comments' => 0, 'post comments' => 0, // Enabled by default, because it's irrelevant for this test. @@ -128,8 +128,9 @@ function setEnvironment(array $info) { // $this->postComment() relies on actual user permissions. $comment = entity_create('comment', array( 'cid' => NULL, - 'nid' => $this->node->nid, - 'node_type' => $this->node->type, + 'entity_id' => $this->node->nid, + 'entity_type' => 'node', + 'field_name' => 'comment', 'pid' => 0, 'uid' => 0, 'status' => COMMENT_PUBLISHED, @@ -153,10 +154,10 @@ function setEnvironment(array $info) { } // Change comment settings. - variable_set('comment_form_location_' . $this->node->type, $info['form']); - variable_set('comment_anonymous_' . $this->node->type, $info['contact']); - if ($this->node->comment != $info['comments']) { - $this->node->comment = $info['comments']; + $this->setCommentSettings('comment_form_location', $info['form'], 'Set comment form location'); + $this->setCommentAnonymous($info['contact']); + if ($this->node->comment[LANGUAGE_NOT_SPECIFIED][0]['comment'] != $info['comments']) { + $this->node->comment[LANGUAGE_NOT_SPECIFIED][0]['comment'] = $info['comments']; node_save($this->node); } @@ -180,9 +181,9 @@ function setEnvironment(array $info) { COMMENT_ANONYMOUS_MUST_CONTACT => 'required', ); $t_comments = array( - COMMENT_NODE_OPEN => 'open', - COMMENT_NODE_CLOSED => 'closed', - COMMENT_NODE_HIDDEN => 'hidden', + COMMENT_ENTITY_OPEN => 'open', + COMMENT_ENTITY_CLOSED => 'closed', + COMMENT_ENTITY_HIDDEN => 'hidden', ); $verbose = $info; $verbose['form'] = $t_form[$info['form']]; @@ -281,12 +282,12 @@ function assertCommentLinks(array $info) { // Verify that the "Add new comment" link points to the correct URL // based on the comment form location configuration. if ($info['form'] == COMMENT_FORM_SEPARATE_PAGE) { - $this->assertLinkByHref("comment/reply/$nid#comment-form", 0, 'Comment form link destination is on a separate page.'); + $this->assertLinkByHref("comment/reply/node/$nid/comment#comment-form", 0, 'Comment form link destination is on a separate page.'); $this->assertNoLinkByHref("node/$nid#comment-form"); } else { $this->assertLinkByHref("node/$nid#comment-form", 0, 'Comment form link destination is on node.'); - $this->assertNoLinkByHref("comment/reply/$nid#comment-form"); + $this->assertNoLinkByHref("comment/reply/node/$nid/comment#comment-form"); } } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php index 324012e..9cf58f5 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php @@ -27,7 +27,6 @@ public function testCommentNewCommentsIndicator() { // Test if the right links are displayed when no comment is present for the // node. $this->drupalLogin($this->admin_user); - $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN)); $this->drupalGet('node'); $this->assertNoLink(t('@count comments', array('@count' => 0))); $this->assertNoLink(t('@count new comments', array('@count' => 0))); @@ -37,8 +36,9 @@ public function testCommentNewCommentsIndicator() { // comment settings so use comment_save() to avoid complex setup. $comment = entity_create('comment', array( 'cid' => NULL, - 'nid' => $this->node->nid, - 'node_type' => $this->node->type, + 'entity_id' => $this->node->nid, + 'entity_type' => 'node', + 'field_name' => 'comment', 'pid' => 0, 'uid' => $this->loggedInUser->uid, 'status' => COMMENT_PUBLISHED, diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentNodeAccessTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentNodeAccessTest.php index 37809c6..93c64f5 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentNodeAccessTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentNodeAccessTest.php @@ -75,7 +75,7 @@ function testThreadedCommentView() { $this->assertText($comment_text, 'Individual comment body found.'); // Reply to comment, creating second comment. - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $reply_text = $this->randomName(); $reply_subject = $this->randomName(); $reply = $this->postComment(NULL, $reply_text, $reply_subject, TRUE); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentPagerTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentPagerTest.php index 52f1e32..b3e5bc4 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentPagerTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentPagerTest.php @@ -31,7 +31,7 @@ function testCommentPaging() { $this->setCommentPreview(DRUPAL_DISABLED); // Create a node and three comments. - $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); + $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); $comments = array(); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); @@ -66,7 +66,7 @@ function testCommentPaging() { // Post a reply to the oldest comment and test again. $replies = array(); $oldest_comment = reset($comments); - $this->drupalGet('comment/reply/' . $node->nid . '/' . $oldest_comment->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $oldest_comment->id); $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); $this->setCommentsPerPage(2); @@ -107,26 +107,26 @@ function testCommentOrderingThreading() { $this->setCommentsPerPage(1000); // Create a node and three comments. - $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); + $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); $comments = array(); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the second comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[1]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[1]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the first comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[0]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[0]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the last comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[2]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[2]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the second comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[3]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[3]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // At this point, the comment tree is: @@ -208,22 +208,22 @@ function testCommentNewPageIndicator() { $this->setCommentsPerPage(1); // Create a node and three comments. - $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); + $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); $comments = array(); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the second comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[1]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[1]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the first comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[0]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[0]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the last comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[2]->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $comments[2]->id); $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); // At this point, the comment tree is: @@ -247,7 +247,7 @@ function testCommentNewPageIndicator() { $node = node_load($node->nid); foreach ($expected_pages as $new_replies => $expected_page) { - $returned = comment_new_page_count($node->comment_count, $new_replies, $node); + $returned = comment_new_page_count($node->comment_statistics['comment']->comment_count, $new_replies, $node); $returned_page = is_array($returned) ? $returned['page'] : 0; $this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page))); } @@ -265,7 +265,7 @@ function testCommentNewPageIndicator() { $node = node_load($node->nid); foreach ($expected_pages as $new_replies => $expected_page) { - $returned = comment_new_page_count($node->comment_count, $new_replies, $node); + $returned = comment_new_page_count($node->comment_statistics['comment']->comment_count, $new_replies, $node); $returned_page = is_array($returned) ? $returned['page'] : 0; $this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page))); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentRssTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentRssTest.php index 28e199e..55bee30 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentRssTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentRssTest.php @@ -31,7 +31,7 @@ function testCommentRss() { $this->assertRaw($raw, 'Comments as part of RSS feed.'); // Hide comments from RSS feed and check presence. - $this->node->comment = COMMENT_NODE_HIDDEN; + $this->node->comment = array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_HIDDEN))); node_save($this->node); $this->drupalGet('rss.xml'); $this->assertNoRaw($raw, 'Hidden comments is not a part of RSS feed.'); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentStatisticsTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentStatisticsTest.php index afdcba4..5e67c3e 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentStatisticsTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentStatisticsTest.php @@ -50,10 +50,10 @@ function testCommentNodeCommentStatistics() { // Checks the initial values of node comment statistics with no comment. $node = node_load($this->node->nid); - $this->assertEqual($node->last_comment_timestamp, $this->node->created, 'The initial value of node last_comment_timestamp is the node created date.'); - $this->assertEqual($node->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.'); - $this->assertEqual($node->last_comment_uid, $this->web_user->uid, 'The initial value of node last_comment_uid is the node uid.'); - $this->assertEqual($node->comment_count, 0, 'The initial value of node comment_count is zero.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_timestamp, $this->node->created, 'The initial value of node last_comment_timestamp is the node created date.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_uid, $this->web_user->uid, 'The initial value of node last_comment_uid is the node uid.'); + $this->assertEqual($node->comment_statistics['comment']->comment_count, 0, 'The initial value of node comment_count is zero.'); // Post comment #1 as web_user2. $this->drupalLogin($this->web_user2); @@ -64,9 +64,9 @@ function testCommentNodeCommentStatistics() { // Checks the new values of node comment statistics with comment #1. // The node needs to be reloaded with a node_load_multiple cache reset. $node = node_load($this->node->nid, TRUE); - $this->assertEqual($node->last_comment_name, NULL, 'The value of node last_comment_name is NULL.'); - $this->assertEqual($node->last_comment_uid, $this->web_user2->uid, 'The value of node last_comment_uid is the comment #1 uid.'); - $this->assertEqual($node->comment_count, 1, 'The value of node comment_count is 1.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_name, NULL, 'The value of node last_comment_name is NULL.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_uid, $this->web_user2->uid, 'The value of node last_comment_uid is the comment #1 uid.'); + $this->assertEqual($node->comment_statistics['comment']->comment_count, 1, 'The value of node comment_count is 1.'); // Prepare for anonymous comment submission (comment approval enabled). config('user.settings')->set('register', USER_REGISTER_VISITORS)->save(); @@ -81,7 +81,7 @@ function testCommentNodeCommentStatistics() { $this->drupalLogout(); // Post comment #2 as anonymous (comment approval enabled). - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $anonymous_comment = $this->postComment($this->node, $this->randomName(), '', TRUE); $comment_unpublished_loaded = comment_load($anonymous_comment->id); @@ -89,9 +89,9 @@ function testCommentNodeCommentStatistics() { // ensure they haven't changed since the comment has not been moderated. // The node needs to be reloaded with a node_load_multiple cache reset. $node = node_load($this->node->nid, TRUE); - $this->assertEqual($node->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.'); - $this->assertEqual($node->last_comment_uid, $this->web_user2->uid, 'The value of node last_comment_uid is still the comment #1 uid.'); - $this->assertEqual($node->comment_count, 1, 'The value of node comment_count is still 1.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_uid, $this->web_user2->uid, 'The value of node last_comment_uid is still the comment #1 uid.'); + $this->assertEqual($node->comment_statistics['comment']->comment_count, 1, 'The value of node comment_count is still 1.'); // Prepare for anonymous comment submission (no approval required). $this->drupalLogin($this->admin_user); @@ -103,16 +103,16 @@ function testCommentNodeCommentStatistics() { $this->drupalLogout(); // Post comment #3 as anonymous. - $this->drupalGet('comment/reply/' . $this->node->nid); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment'); $anonymous_comment = $this->postComment($this->node, $this->randomName(), '', array('name' => $this->randomName())); $comment_loaded = comment_load($anonymous_comment->id); // Checks the new values of node comment statistics with comment #3. // The node needs to be reloaded with a node_load_multiple cache reset. $node = node_load($this->node->nid, TRUE); - $this->assertEqual($node->last_comment_name, $comment_loaded->name, 'The value of node last_comment_name is the name of the anonymous user.'); - $this->assertEqual($node->last_comment_uid, 0, 'The value of node last_comment_uid is zero.'); - $this->assertEqual($node->comment_count, 2, 'The value of node comment_count is 2.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_name, $comment_loaded->name, 'The value of node last_comment_name is the name of the anonymous user.'); + $this->assertEqual($node->comment_statistics['comment']->last_comment_uid, 0, 'The value of node last_comment_uid is zero.'); + $this->assertEqual($node->comment_statistics['comment']->comment_count, 2, 'The value of node comment_count is 2.'); } } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php index 37a8b47..6725784 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php @@ -72,8 +72,11 @@ function setUp() { 'access content', )); + // Create comment field on article. + comment_add_default_comment_field('node', 'article'); + // Create a test node authored by the web user. - $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid)); + $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); } /** @@ -94,12 +97,13 @@ function postComment($node, $comment, $subject = '', $contact = NULL) { $edit = array(); $edit['comment_body[' . $langcode . '][0][value]'] = $comment; - $preview_mode = variable_get('comment_preview_article', DRUPAL_OPTIONAL); - $subject_mode = variable_get('comment_subject_field_article', 1); + $instance = field_info_instance('node', 'comment', 'article'); + $preview_mode = $instance['settings']['comment']['comment_preview']; + $subject_mode = $instance['settings']['comment']['comment_subject_field']; // Must get the page before we test for fields. if ($node !== NULL) { - $this->drupalGet('comment/reply/' . $node->nid); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment'); } if ($subject_mode == TRUE) { @@ -264,7 +268,9 @@ function setCommentsPerPage($number) { * Status message to display. */ function setCommentSettings($name, $value, $message) { - variable_set($name . '_article', $value); + $instance = field_info_instance('node', 'comment', 'article'); + $instance['settings']['comment'][$name] = $value; + field_update_instance($instance); // Display status message. $this->pass($message); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentThreadingTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentThreadingTest.php index 7f4edd5..c51a653 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentThreadingTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentThreadingTest.php @@ -34,7 +34,7 @@ function testCommentThreading() { // Create a node. $this->drupalLogin($this->web_user); - $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid)); + $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid, 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); // Post comment #1. $this->drupalLogin($this->web_user); @@ -47,14 +47,14 @@ function testCommentThreading() { // Reply to comment #1 creating comment #2. $this->drupalLogin($this->web_user); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $reply = $this->postComment(NULL, $this->randomName(), '', TRUE); $reply_loaded = comment_load($reply->id); $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #2. Reply found.'); $this->assertEqual($reply_loaded->thread, '01.00/'); // Reply to comment #2 creating comment #3. - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $reply->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $reply->id); $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); $reply_loaded = comment_load($reply->id); $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #3. Second reply found.'); @@ -62,7 +62,7 @@ function testCommentThreading() { // Reply to comment #1 creating comment #4. $this->drupalLogin($this->web_user); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $reply = $this->postComment(NULL, $this->randomName(), '', TRUE); $reply_loaded = comment_load($reply->id); $this->assertTrue($this->commentExists($comment), 'Comment #4. Third reply found.'); @@ -79,14 +79,14 @@ function testCommentThreading() { // Reply to comment #5 creating comment #6. $this->drupalLogin($this->web_user); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $reply = $this->postComment(NULL, $this->randomName(), '', TRUE); $reply_loaded = comment_load($reply->id); $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #6. Reply found.'); $this->assertEqual($reply_loaded->thread, '02.00/'); // Reply to comment #6 creating comment #7. - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $reply->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $reply->id); $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE); $reply_loaded = comment_load($reply->id); $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #7. Second reply found.'); @@ -94,7 +94,7 @@ function testCommentThreading() { // Reply to comment #5 creating comment #8. $this->drupalLogin($this->web_user); - $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id); + $this->drupalGet('comment/reply/node/' . $this->node->nid . '/comment/' . $comment->id); $reply = $this->postComment(NULL, $this->randomName(), '', TRUE); $reply_loaded = comment_load($reply->id); $this->assertTrue($this->commentExists($comment), 'Comment #8. Third reply found.'); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php index 34674e8..98b96e8 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php @@ -35,11 +35,11 @@ function testCommentTokenReplacement() { $this->setCommentSubject(TRUE); // Create a node and a comment. - $node = $this->drupalCreateNode(array('type' => 'article')); + $node = $this->drupalCreateNode(array('type' => 'article', 'comment' => array(LANGUAGE_NOT_SPECIFIED => array(array('comment' => COMMENT_ENTITY_OPEN))))); $parent_comment = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); // Post a reply to the comment. - $this->drupalGet('comment/reply/' . $node->nid . '/' . $parent_comment->id); + $this->drupalGet('comment/reply/node/' . $node->nid . '/comment/' . $parent_comment->id); $child_comment = $this->postComment(NULL, $this->randomName(), $this->randomName()); $comment = comment_load($child_comment->id); $comment->homepage = 'http://example.org/'; @@ -63,7 +63,7 @@ function testCommentTokenReplacement() { $tests['[comment:changed:since]'] = format_interval(REQUEST_TIME - $comment->changed, 2, $language_interface->langcode); $tests['[comment:parent:cid]'] = $comment->pid; $tests['[comment:parent:title]'] = check_plain($parent_comment->subject); - $tests['[comment:node:nid]'] = $comment->nid; + $tests['[comment:node:nid]'] = $comment->entity_id; $tests['[comment:node:title]'] = check_plain($node->title); $tests['[comment:author:uid]'] = $comment->uid; $tests['[comment:author:name]'] = check_plain($this->admin_user->name); @@ -97,11 +97,11 @@ function testCommentTokenReplacement() { // Generate comment tokens for the node (it has 2 comments, both new). $tests = array(); - $tests['[node:comment-count]'] = 2; - $tests['[node:comment-count-new]'] = 2; + $tests['[entity:comment-count]'] = 2; + $tests['[entity:comment-count-new]'] = 2; foreach ($tests as $input => $expected) { - $output = token_replace($input, array('node' => $node), array('langcode' => $language_interface->langcode)); + $output = token_replace($input, array('entity' => $node), array('langcode' => $language_interface->langcode)); $this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input))); } } diff --git a/core/modules/comment/templates/comment-wrapper.tpl.php b/core/modules/comment/templates/comment-wrapper.tpl.php index ac1c27f..5d9149e 100644 --- a/core/modules/comment/templates/comment-wrapper.tpl.php +++ b/core/modules/comment/templates/comment-wrapper.tpl.php @@ -5,7 +5,7 @@ * Provides an HTML container for comments. * * Available variables: - * - $content: The array of content-related elements for the node. Use + * - $content: The array of content-related elements for the entity. Use * render($content) to print them all, or * print a subset such as render($content['comment_form']). * - $attributes: An instance of Attributes class that can be manipulated as an @@ -20,7 +20,7 @@ * the template. * * The following variables are provided for contextual information. - * - $node: Node entity the comments are attached to. + * - $entity: Entity the comments are attached to. * The constants below the variables show the possible values and should be * used for comparison. * - $display_mode @@ -33,7 +33,7 @@ */ ?>
> - type != 'forum'): ?> + entityType() == 'node' && $entity->bundle() != 'forum')): ?>

diff --git a/core/modules/comment/templates/comment.tpl.php b/core/modules/comment/templates/comment.tpl.php index a6caa52..59128c5 100644 --- a/core/modules/comment/templates/comment.tpl.php +++ b/core/modules/comment/templates/comment.tpl.php @@ -30,7 +30,8 @@ * It includes the 'class' information, which includes: * - comment: The current template type; e.g., 'theming hook'. * - by-anonymous: Comment by an unregistered user. - * - by-node-author: Comment by the author of the parent node. + * - by-{entity-type}-author: Comment by the author of the parent entity, + * eg. by-node-author. * - preview: When previewing a new or edited comment. * The following applies only to viewers who are registered users: * - unpublished: An unpublished comment visible only to administrators. @@ -45,7 +46,7 @@ * * These two variables are provided for context: * - $comment: Full comment object. - * - $node: Node entity the comments are attached to. + * - $entity: Entity the comments are attached to. * * @see template_preprocess() * @see template_preprocess_comment() diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index af2daa9..993b546 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -491,7 +491,9 @@ function forum_taxonomy_term_delete(Term $term) { * comment_save() calls hook_comment_publish() for all published comments. */ function forum_comment_publish($comment) { - _forum_update_forum_index($comment->nid); + if ($comment->entity_type == 'node') { + _forum_update_forum_index($comment->entity_id); + } } /** @@ -503,8 +505,8 @@ function forum_comment_publish($comment) { function forum_comment_update($comment) { // comment_save() calls hook_comment_publish() for all published comments, // so we need to handle all other values here. - if (!$comment->status) { - _forum_update_forum_index($comment->nid); + if (!$comment->status && $comment->entity_type == 'node') { + _forum_update_forum_index($comment->entity_id); } } @@ -512,14 +514,18 @@ function forum_comment_update($comment) { * Implements hook_comment_unpublish(). */ function forum_comment_unpublish($comment) { - _forum_update_forum_index($comment->nid); + if ($comment->entity_type == 'node') { + _forum_update_forum_index($comment->entity_id); + } } /** * Implements hook_comment_delete(). */ function forum_comment_delete($comment) { - _forum_update_forum_index($comment->nid); + if ($comment->entity_type == 'node') { + _forum_update_forum_index($comment->entity_id); + } } /** diff --git a/core/modules/node/node.install b/core/modules/node/node.install index a9c2c36..ebe2057 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -79,12 +79,6 @@ function node_schema() { 'not null' => TRUE, 'default' => 0, ), - 'comment' => array( - 'description' => 'Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write).', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ), 'promote' => array( 'description' => 'Boolean indicating whether the node should be displayed on the front page.', 'type' => 'int', @@ -245,12 +239,6 @@ function node_schema() { 'not null' => TRUE, 'default' => 1, ), - 'comment' => array( - 'description' => 'Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ), 'promote' => array( 'description' => 'Boolean indicating whether the node (at the time of this revision) should be displayed on the front page.', 'type' => 'int', diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 782c06f..d22d111 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -429,7 +429,9 @@ function rdf_comment_load($comments) { // isn't needed until rdf_preprocess_comment() is called, but set it here // to optimize performance for websites that implement an entity cache. $comment->rdf_data['date'] = rdf_rdfa_attributes($comment->rdf_mapping['created'], $comment->created); - $comment->rdf_data['nid_uri'] = url('node/' . $comment->nid); + if ($comment->entity_type == 'node') { + $comment->rdf_data['nid_uri'] = url('node/' . $comment->entity_id); + } if ($comment->pid) { $comment->rdf_data['pid_uri'] = url('comment/' . $comment->pid, array('fragment' => 'comment-' . $comment->pid)); } diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 315ae7a..53a5bc7 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -832,7 +832,9 @@ function search_node_update(Node $node) { */ function search_comment_insert($comment) { // Reindex the node when comments are added. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** @@ -840,7 +842,9 @@ function search_comment_insert($comment) { */ function search_comment_update($comment) { // Reindex the node when comments are changed. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** @@ -848,7 +852,9 @@ function search_comment_update($comment) { */ function search_comment_delete($comment) { // Reindex the node when comments are deleted. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** @@ -856,7 +862,9 @@ function search_comment_delete($comment) { */ function search_comment_publish($comment) { // Reindex the node when comments are published. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** @@ -864,7 +872,9 @@ function search_comment_publish($comment) { */ function search_comment_unpublish($comment) { // Reindex the node when comments are unpublished. - search_touch_node($comment->nid); + if ($comment->entity_type == 'node') { + search_touch_node($comment->entity_id); + } } /** diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index 7060a87..c515140 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -228,8 +228,8 @@ function tracker_node_predelete(Node $node, $arg = 0) { function tracker_comment_update($comment) { // comment_save() calls hook_comment_publish() for all published comments // so we need to handle all other values here. - if ($comment->status != COMMENT_PUBLISHED) { - _tracker_remove($comment->nid, $comment->uid, $comment->changed); + if ($comment->status != COMMENT_PUBLISHED && $comment->entity_type == 'node') { + _tracker_remove($comment->entity_id, $comment->uid, $comment->changed); } } @@ -240,21 +240,27 @@ function tracker_comment_update($comment) { * comment_save() calls hook_comment_publish() for all published comments. */ function tracker_comment_publish($comment) { - _tracker_add($comment->nid, $comment->uid, $comment->changed); + if ($comment->entity_type == 'node') { + _tracker_add($comment->entity_id, $comment->uid, $comment->changed); + } } /** * Implements hook_comment_unpublish(). */ function tracker_comment_unpublish($comment) { - _tracker_remove($comment->nid, $comment->uid, $comment->changed); + if ($comment->entity_type == 'node') { + _tracker_remove($comment->entity_id, $comment->uid, $comment->changed); + } } /** * Implements hook_comment_delete(). */ function tracker_comment_delete($comment) { - _tracker_remove($comment->nid, $comment->uid, $comment->changed); + if ($comment->entity_type == 'node') { + _tracker_remove($comment->entity_id, $comment->uid, $comment->changed); + } } /** diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 4eeb8d6..d735f1d 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -236,9 +236,11 @@ function standard_install() { rdf_mapping_save($rdf_mapping); } - // Default "Basic page" to not be promoted and have comments disabled. + // Default "Basic page" to not be promoted. variable_set('node_options_page', array('status')); - variable_set('comment_page', COMMENT_NODE_HIDDEN); + + // Add comment field to article node type. + comment_add_default_comment_field('node', 'article'); // Don't display date and author information for "Basic page" nodes by default. variable_set('node_submitted_page', FALSE); diff --git a/core/themes/bartik/templates/comment-wrapper.tpl.php b/core/themes/bartik/templates/comment-wrapper.tpl.php index 764f91e..78e92c9 100644 --- a/core/themes/bartik/templates/comment-wrapper.tpl.php +++ b/core/themes/bartik/templates/comment-wrapper.tpl.php @@ -33,7 +33,7 @@ */ ?>
> - type != 'forum'): ?> + entityType() == 'node' && $entity->bundle() != 'forum')): ?>

diff --git a/core/themes/bartik/templates/comment.tpl.php b/core/themes/bartik/templates/comment.tpl.php index 04aa8e5..7d2a977 100644 --- a/core/themes/bartik/templates/comment.tpl.php +++ b/core/themes/bartik/templates/comment.tpl.php @@ -30,7 +30,8 @@ * It includes the 'class' information, which includes: * - comment: The current template type; e.g., 'theming hook'. * - by-anonymous: Comment by an unregistered user. - * - by-node-author: Comment by the author of the parent node. + * - by-{entity-type}-author: Comment by the author of the parent entity. + * eg. by-node-author. * - preview: When previewing a new or edited comment. * The following applies only to viewers who are registered users: * - unpublished: An unpublished comment visible only to administrators. @@ -45,7 +46,7 @@ * * These two variables are provided for context: * - $comment: Full comment object. - * - $node: Node entity the comments are attached to. + * - $entity: Entity the comments are attached to. * * @see template_preprocess() * @see template_preprocess_comment()