781 lines
30 KiB
Plaintext
781 lines
30 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Enables users to comment on published content.
|
|
*
|
|
* When installed, 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\comment\CommentInterface;
|
|
use Drupal\comment\Entity\Comment;
|
|
use Drupal\comment\CommentManagerInterface;
|
|
use Drupal\comment\Entity\CommentType;
|
|
use Drupal\Core\Entity\FieldableEntityInterface;
|
|
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
|
use Drupal\Component\Utility\SafeMarkup;
|
|
use Drupal\Core\Entity\EntityInterface;
|
|
use Drupal\Core\Form\FormStateInterface;
|
|
use Drupal\Core\Routing\RouteMatchInterface;
|
|
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
|
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
|
use Drupal\Core\Field\FieldDefinitionInterface;
|
|
use Drupal\Core\Render\Element;
|
|
use Drupal\Core\Url;
|
|
use Drupal\field\Entity\FieldStorageConfig;
|
|
use Drupal\field\FieldConfigInterface;
|
|
use Drupal\field\FieldStorageConfigInterface;
|
|
use Drupal\file\FileInterface;
|
|
use Drupal\user\EntityOwnerInterface;
|
|
use Drupal\node\NodeInterface;
|
|
use Drupal\user\RoleInterface;
|
|
|
|
/**
|
|
* Anonymous posters cannot enter their contact information.
|
|
*/
|
|
const COMMENT_ANONYMOUS_MAYNOT_CONTACT = 0;
|
|
|
|
/**
|
|
* Anonymous posters may leave their contact information.
|
|
*/
|
|
const COMMENT_ANONYMOUS_MAY_CONTACT = 1;
|
|
|
|
/**
|
|
* Anonymous posters are required to leave their contact information.
|
|
*/
|
|
const COMMENT_ANONYMOUS_MUST_CONTACT = 2;
|
|
|
|
/**
|
|
* The time cutoff for comments marked as read for entity types other node.
|
|
*
|
|
* 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.
|
|
*
|
|
* @todo Remove when https://www.drupal.org/node/1029708 lands.
|
|
*/
|
|
define('COMMENT_NEW_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);
|
|
|
|
/**
|
|
* Implements hook_help().
|
|
*/
|
|
function comment_help($route_name, RouteMatchInterface $route_match) {
|
|
switch ($route_name) {
|
|
case 'help.page.comment':
|
|
$output = '<h3>' . t('About') . '</h3>';
|
|
$output .= '<p>' . t('The Comment module allows users to comment on site content, set commenting defaults and permissions, and moderate comments. For more information, see the <a href="!comment">online documentation for the Comment module</a>.', array('!comment' => 'https://www.drupal.org/documentation/modules/comment')) . '</p>';
|
|
$output .= '<h3>' . t('Uses') . '</h3>';
|
|
$output .= '<dl>';
|
|
$output .= '<dt>' . t('Enabling commenting and configuring defaults') . '</dt>';
|
|
$output .= '<dd>' . t('Comment functionality can be enabled for any entity sub-type (for example, a <a href="!content-type">content type</a>). On the Manage fields page for each entity sub-type, you can enable commenting by adding a Comments field. The entity sub-types each have their own default comment settings configured as: <em>Open</em> to allow new comments, <em>Closed</em> to view existing comments, but prevent new comments, or <em>Hidden</em> to hide existing comments and prevent new comments. For background information about entities, see the <a href="!field">Field module help page</a>.', array('!content-type' => (\Drupal::moduleHandler()->moduleExists('node')) ? \Drupal::url('entity.node_type.collection') : '#', '!field' => \Drupal::url('help.page', array('name' => 'field')))) . '</dd>';
|
|
$output .= '<dt>' . t('Overriding default settings') . '</dt>';
|
|
$output .= '<dd>' . t('When you create an entity item, you can override the default comment settings. Changing the entity sub-type defaults will not affect existing entity items, whether they used the default settings or had overrides.') . '</dd>';
|
|
$output .= '<dt>' . t('Approving and managing comments') . '</dt>';
|
|
$output .= '<dd>' . t('Comments from users who have the <em>Skip comment approval</em> permission are published immediately. All other comments are placed in the <a href="!comment-approval">Unapproved comments</a> queue, until a user who has permission to <em>Administer comments and comment settings</em> publishes or deletes them. Published comments can be bulk managed on the <a href="!admin-comment">Published comments</a> administration page. When a comment has no replies, it remains editable by its author, as long as the author has <em>Edit own comments</em> permission.', array('!comment-approval' => \Drupal::url('comment.admin_approval'), '!admin-comment' => \Drupal::url('comment.admin'))) . '</dd>';
|
|
$output .= '</dl>';
|
|
return $output;
|
|
|
|
case 'entity.comment_type.collection':
|
|
$output = '<p>' . t('This page provides a list of all comment types on the site and allows you to manage the fields, form and display settings for each.') . '</p>';
|
|
return $output;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Entity URI callback.
|
|
*/
|
|
function comment_uri(CommentInterface $comment) {
|
|
return new Url(
|
|
'entity.comment.canonical',
|
|
array(
|
|
'comment' => $comment->id(),
|
|
),
|
|
array('fragment' => 'comment-' . $comment->id())
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_extra_field_info().
|
|
*/
|
|
function comment_entity_extra_field_info() {
|
|
$return = array();
|
|
foreach (CommentType::loadMultiple() as $comment_type) {
|
|
$return['comment'][$comment_type->id()] = array(
|
|
'form' => array(
|
|
'author' => array(
|
|
'label' => t('Author'),
|
|
'description' => t('Author textfield'),
|
|
'weight' => -2,
|
|
),
|
|
),
|
|
);
|
|
$return['comment'][$comment_type->id()]['display']['links'] = array(
|
|
'label' => t('Links'),
|
|
'description' => t('Comment operation links'),
|
|
'weight' => 100,
|
|
'visible' => TRUE,
|
|
);
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function comment_theme() {
|
|
return array(
|
|
'comment' => array(
|
|
'render element' => 'elements',
|
|
),
|
|
'field__comment' => array(
|
|
'base hook' => 'field',
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_create() for 'field_config'.
|
|
*/
|
|
function comment_field_config_create(FieldConfigInterface $field) {
|
|
if ($field->getType() == 'comment' && !$field->isSyncing()) {
|
|
// Assign default values for the field.
|
|
if (!isset($field->default_value)) {
|
|
$field->setDefaultValue(array());
|
|
}
|
|
$field->default_value += array(array());
|
|
$field->default_value[0] += array(
|
|
'status' => CommentItemInterface::OPEN,
|
|
'cid' => 0,
|
|
'last_comment_timestamp' => 0,
|
|
'last_comment_name' => '',
|
|
'last_comment_uid' => 0,
|
|
'comment_count' => 0,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_update() for 'field_config'.
|
|
*/
|
|
function comment_field_config_update(FieldConfigInterface $field) {
|
|
if ($field->getType() == 'comment') {
|
|
// Comment field settings also affects the rendering of *comment* entities,
|
|
// not only the *commented* entities.
|
|
\Drupal::entityManager()->getViewBuilder('comment')->resetCache();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_insert() for 'field_storage_config'.
|
|
*/
|
|
function comment_field_storage_config_insert(FieldStorageConfigInterface $field_storage) {
|
|
if ($field_storage->getType() == 'comment') {
|
|
// Check that the target entity type uses an integer ID.
|
|
$entity_type_id = $field_storage->getTargetEntityTypeId();
|
|
if (!_comment_entity_uses_integer_id($entity_type_id)) {
|
|
throw new \UnexpectedValueException('You cannot attach a comment field to an entity with a non-integer ID field');
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_delete() for 'field_config'.
|
|
*/
|
|
function comment_field_config_delete(FieldConfigInterface $field) {
|
|
if ($field->getType() == 'comment') {
|
|
// Delete all comments that used by the entity bundle.
|
|
$entity_query = \Drupal::entityQuery('comment');
|
|
$entity_query->condition('entity_type', $field->getEntityTypeId());
|
|
$entity_query->condition('field_name', $field->getName());
|
|
$cids = $entity_query->execute();
|
|
entity_delete_multiple('comment', $cids);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_node_links_alter().
|
|
*/
|
|
function comment_node_links_alter(array &$node_links, NodeInterface $node, array &$context) {
|
|
// Comment links are only added to node entity type for backwards
|
|
// compatibility. Should you require comment links for other entity types you
|
|
// can do so by implementing a new field formatter.
|
|
// @todo Make this configurable from the formatter. See
|
|
// https://www.drupal.org/node/1901110.
|
|
|
|
$links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context);
|
|
$node_links += $links;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_view().
|
|
*/
|
|
function comment_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode, $langcode) {
|
|
if ($entity instanceof FieldableEntityInterface && $view_mode == 'rss' && $display->getComponent('links')) {
|
|
/** @var \Drupal\comment\CommentManagerInterface $comment_manager */
|
|
$comment_manager = \Drupal::service('comment.manager');
|
|
$fields = $comment_manager->getFields($entity->getEntityTypeId());
|
|
foreach ($fields as $field_name => $detail) {
|
|
if ($entity->hasField($field_name) && $entity->get($field_name)->status != CommentItemInterface::HIDDEN) {
|
|
// Add a comments RSS element which is a URL to the comments of this
|
|
// entity.
|
|
$options = array(
|
|
'fragment' => 'comments',
|
|
'absolute' => TRUE,
|
|
);
|
|
$entity->rss_elements[] = array(
|
|
'key' => 'comments',
|
|
'value' => $entity->url('canonical', $options),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_view_alter() for node entities.
|
|
*/
|
|
function comment_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) {
|
|
if (\Drupal::moduleHandler()->moduleExists('history')) {
|
|
$build['#attributes']['data-history-node-id'] = $node->id();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generates an array for rendering a comment.
|
|
*
|
|
* @param \Drupal\comment\CommentInterface $comment
|
|
* The comment object.
|
|
* @param $view_mode
|
|
* (optional) View mode; e.g., 'full', 'teaser', etc. Defaults to 'full'.
|
|
* @param $langcode
|
|
* (optional) A language code to use for rendering. Defaults to the global
|
|
* content language of the current request.
|
|
*
|
|
* @return array
|
|
* An array as expected by drupal_render().
|
|
*
|
|
* @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
|
|
* Use \Drupal::entityManager()->getViewBuilder('comment')->view().
|
|
*/
|
|
function comment_view(CommentInterface $comment, $view_mode = 'full', $langcode = NULL) {
|
|
return entity_view($comment, $view_mode, $langcode);
|
|
}
|
|
|
|
/**
|
|
* Constructs render array from an array of loaded comments.
|
|
*
|
|
* @param $comments
|
|
* An array of comments as returned by entity_load_multiple().
|
|
* @param $view_mode
|
|
* View mode; e.g., 'full', 'teaser', etc.
|
|
* @param $langcode
|
|
* (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 array
|
|
* An array in the format expected by drupal_render().
|
|
*
|
|
* @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
|
|
* Use \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple().
|
|
*
|
|
* @see drupal_render()
|
|
*/
|
|
function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) {
|
|
return entity_view_multiple($comments, $view_mode, $langcode);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_FORM_ID_alter() for field_ui_field_storage_add_form.
|
|
*/
|
|
function comment_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state) {
|
|
$route_match = \Drupal::routeMatch();
|
|
if ($form_state->get('entity_type_id') == 'comment' && $route_match->getParameter('commented_entity_type')) {
|
|
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
|
|
}
|
|
if (!_comment_entity_uses_integer_id($form_state->get('entity_type_id'))) {
|
|
// You cannot use comment fields on entity types with non-integer IDs.
|
|
unset($form['add']['new_storage_type']['#options'][t('General')]['comment']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_FORM_ID_alter().
|
|
*/
|
|
function comment_form_field_ui_form_display_overview_form_alter(&$form, FormStateInterface $form_state) {
|
|
$route_match = \Drupal::routeMatch();
|
|
if ($form['#entity_type'] == 'comment' && $route_match->getParameter('commented_entity_type')) {
|
|
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_FORM_ID_alter().
|
|
*/
|
|
function comment_form_field_ui_display_overview_form_alter(&$form, FormStateInterface $form_state) {
|
|
$route_match = \Drupal::routeMatch();
|
|
if ($form['#entity_type'] == 'comment' && $route_match->getParameter('commented_entity_type')) {
|
|
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_FORM_ID_alter() for 'field_storage_config_edit_form'.
|
|
*/
|
|
function comment_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state) {
|
|
if ($form_state->getFormObject()->getEntity()->getType() == 'comment') {
|
|
// We only support posting one comment at the time so it doesn't make sense
|
|
// to let the site builder choose anything else.
|
|
$form['cardinality_container']['cardinality']['#default_value'] = 1;
|
|
$form['cardinality_container']['#access'] = FALSE;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_storage_load().
|
|
*
|
|
* @see \Drupal\comment\Plugin\Field\FieldType\CommentItem::propertyDefinitions()
|
|
*/
|
|
function comment_entity_storage_load($entities, $entity_type) {
|
|
// Comments can only be attached to content entities, so skip others.
|
|
if (!\Drupal::entityManager()->getDefinition($entity_type)->isSubclassOf('Drupal\Core\Entity\FieldableEntityInterface')) {
|
|
return;
|
|
}
|
|
// @todo Investigate in https://www.drupal.org/node/2527866 why we need that.
|
|
if (!\Drupal::hasService('comment.manager') || !\Drupal::service('comment.manager')->getFields($entity_type)) {
|
|
// Do not query database when entity has no comment fields.
|
|
return;
|
|
}
|
|
// Load comment information from the database and update the entity's
|
|
// comment statistics properties, which are defined on each CommentItem field.
|
|
$result = \Drupal::service('comment.statistics')->read($entities, $entity_type);
|
|
foreach ($result as $record) {
|
|
// Skip fields that entity does not have.
|
|
if (!$entities[$record->entity_id]->hasField($record->field_name)) {
|
|
continue;
|
|
}
|
|
$comment_statistics = $entities[$record->entity_id]->get($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_entity_insert().
|
|
*/
|
|
function comment_entity_insert(EntityInterface $entity) {
|
|
// Allow bulk updates and inserts to temporarily disable the
|
|
// maintenance of the {comment_entity_statistics} table.
|
|
if (\Drupal::state()->get('comment.maintain_entity_statistics') &&
|
|
$fields = \Drupal::service('comment.manager')->getFields($entity->getEntityTypeId())) {
|
|
\Drupal::service('comment.statistics')->create($entity, $fields);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_predelete().
|
|
*/
|
|
function comment_entity_predelete(EntityInterface $entity) {
|
|
// Entities can have non-numeric IDs, but {comment} and
|
|
// {comment_entity_statistics} tables have integer columns for entity ID, and
|
|
// PostgreSQL throws exceptions if you attempt query conditions with
|
|
// mismatched types. So, we need to verify that the ID is numeric (even for an
|
|
// entity type that has an integer ID, $entity->id() might be a string
|
|
// containing a number), and then cast it to an integer when querying.
|
|
if ($entity instanceof FieldableEntityInterface && is_numeric($entity->id())) {
|
|
$entity_query = \Drupal::entityQuery('comment');
|
|
$entity_query->condition('entity_id', (int) $entity->id());
|
|
$entity_query->condition('entity_type', $entity->getEntityTypeId());
|
|
$cids = $entity_query->execute();
|
|
entity_delete_multiple('comment', $cids);
|
|
\Drupal::service('comment.statistics')->delete($entity);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determines if an entity type is using an integer-based ID definition.
|
|
*
|
|
* @param string $entity_type_id
|
|
* The ID the represents the entity type.
|
|
*
|
|
* @return bool
|
|
* Returns TRUE if the entity type has an integer-based ID definition and
|
|
* FALSE otherwise.
|
|
*/
|
|
function _comment_entity_uses_integer_id($entity_type_id) {
|
|
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
|
|
$entity_type_id_key = $entity_type->getKey('id');
|
|
if ($entity_type_id_key === FALSE) {
|
|
return FALSE;
|
|
}
|
|
$field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type->id());
|
|
$entity_type_id_definition = $field_definitions[$entity_type_id_key];
|
|
return $entity_type_id_definition->getType() === 'integer';
|
|
}
|
|
|
|
/**
|
|
* Implements hook_node_update_index().
|
|
*/
|
|
function comment_node_update_index(EntityInterface $node, $langcode) {
|
|
$index_comments = &drupal_static(__FUNCTION__);
|
|
|
|
if ($index_comments === NULL) {
|
|
// Do not index in the following three cases:
|
|
// 1. 'Authenticated user' can search content but can't access comments.
|
|
// 2. 'Anonymous user' can search content but can't access comments.
|
|
// 3. Any role can search content but can't access comments and access
|
|
// comments is not granted by the 'authenticated user' role. In this case
|
|
// all users might have both permissions from various roles but it is also
|
|
// possible to set up a user to have only search content and so a user
|
|
// edit could change the security situation so it is not safe to index the
|
|
// comments.
|
|
$index_comments = TRUE;
|
|
$roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple();
|
|
$authenticated_can_access = $roles[RoleInterface::AUTHENTICATED_ID]->hasPermission('access comments');
|
|
foreach ($roles as $rid => $role) {
|
|
if ($role->hasPermission('search content') && !$role->hasPermission('access comments')) {
|
|
if ($rid == RoleInterface::AUTHENTICATED_ID || $rid == RoleInterface::ANONYMOUS_ID || !$authenticated_can_access) {
|
|
$index_comments = FALSE;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$build = array();
|
|
|
|
if ($index_comments) {
|
|
foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $info) {
|
|
// Skip fields that entity does not have.
|
|
if (!$node->hasField($field_name)) {
|
|
continue;
|
|
}
|
|
$field_definition = $node->getFieldDefinition($field_name);
|
|
$mode = $field_definition->getSetting('default_mode');
|
|
$comments_per_page = $field_definition->getSetting('per_page');
|
|
if ($node->get($field_name)->status) {
|
|
$comments = \Drupal::entityManager()->getStorage('comment')
|
|
->loadThread($node, $field_name, $mode, $comments_per_page);
|
|
if ($comments) {
|
|
$build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return \Drupal::service('renderer')->renderPlain($build);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_cron().
|
|
*/
|
|
function comment_cron() {
|
|
// Store the maximum possible comments per thread (used for node search
|
|
// ranking by reply count).
|
|
\Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, \Drupal::service('comment.statistics')->getMaximumCount('node')));
|
|
}
|
|
|
|
/**
|
|
* Implements hook_node_search_result().
|
|
*
|
|
* Formats a comment count string and returns it, for display with search
|
|
* results.
|
|
*/
|
|
function comment_node_search_result(EntityInterface $node) {
|
|
$comment_fields = \Drupal::service('comment.manager')->getFields('node');
|
|
$comments = 0;
|
|
$open = FALSE;
|
|
foreach ($comment_fields as $field_name => $info) {
|
|
// Skip fields that entity does not have.
|
|
if (!$node->hasField($field_name)) {
|
|
continue;
|
|
}
|
|
// Do not make a string if comments are hidden.
|
|
$status = $node->get($field_name)->status;
|
|
if (\Drupal::currentUser()->hasPermission('access comments') && $status != CommentItemInterface::HIDDEN) {
|
|
if ($status == CommentItemInterface::OPEN) {
|
|
// At least one comment field is open.
|
|
$open = TRUE;
|
|
}
|
|
$comments += $node->get($field_name)->comment_count;
|
|
}
|
|
}
|
|
// Do not make a string if there are no comment fields, or no comments exist
|
|
// or all comment fields are hidden.
|
|
if ($comments > 0 || $open) {
|
|
return array('comment' => \Drupal::translation()->formatPlural($comments, '1 comment', '@count comments'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_user_cancel().
|
|
*/
|
|
function comment_user_cancel($edit, $account, $method) {
|
|
switch ($method) {
|
|
case 'user_cancel_block_unpublish':
|
|
$comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id()));
|
|
foreach ($comments as $comment) {
|
|
$comment->setPublished(CommentInterface::NOT_PUBLISHED);
|
|
$comment->save();
|
|
}
|
|
break;
|
|
|
|
case 'user_cancel_reassign':
|
|
/** @var \Drupal\comment\CommentInterface[] $comments */
|
|
$comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id()));
|
|
foreach ($comments as $comment) {
|
|
$comment->setOwnerId(0);
|
|
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
|
|
$comment->save();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_predelete() for user entities.
|
|
*/
|
|
function comment_user_predelete($account) {
|
|
$entity_query = \Drupal::entityQuery('comment');
|
|
$entity_query->condition('uid', $account->id());
|
|
$cids = $entity_query->execute();
|
|
entity_delete_multiple('comment', $cids);
|
|
}
|
|
|
|
/**
|
|
* Generates a comment preview.
|
|
*
|
|
* @param \Drupal\comment\CommentInterface $comment
|
|
* The comment entity to preview.
|
|
* @param Drupal\Core\Form\FormStateInterface $form_state
|
|
* The current state of the form.
|
|
*
|
|
* @return array
|
|
* An array as expected by drupal_render().
|
|
*/
|
|
function comment_preview(CommentInterface $comment, FormStateInterface $form_state) {
|
|
$preview_build = array();
|
|
$entity = $comment->getCommentedEntity();
|
|
|
|
if (!$form_state->getErrors()) {
|
|
// Attach the user and time information.
|
|
$author_name = $comment->getAuthorName();
|
|
if (!empty($author_name)) {
|
|
$account = user_load_by_name($author_name);
|
|
}
|
|
elseif (\Drupal::currentUser()->isAuthenticated() && empty($comment->is_anonymous)) {
|
|
$account = \Drupal::currentUser();
|
|
}
|
|
|
|
if (!empty($account) && $account->isAuthenticated()) {
|
|
$comment->setOwner($account);
|
|
$comment->setAuthorName(SafeMarkup::checkPlain($account->getUsername()));
|
|
}
|
|
elseif (empty($author_name)) {
|
|
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
|
|
}
|
|
|
|
$created_time = !is_null($comment->getCreatedTime()) ? $comment->getCreatedTime() : REQUEST_TIME;
|
|
$comment->setCreatedTime($created_time);
|
|
$comment->changed->value = REQUEST_TIME;
|
|
$comment->in_preview = TRUE;
|
|
$comment_build = comment_view($comment);
|
|
$comment_build['#weight'] = -100;
|
|
|
|
$preview_build['comment_preview'] = $comment_build;
|
|
}
|
|
|
|
if ($comment->hasParentComment()) {
|
|
$build = array();
|
|
$parent = $comment->getParentComment();
|
|
if ($parent && $parent->isPublished()) {
|
|
$build = comment_view($parent);
|
|
}
|
|
}
|
|
else {
|
|
// The comment field output includes rendering the parent entity of the
|
|
// thread to which the comment is a reply. The rendered entity output
|
|
// includes the comment reply form, which contains the comment preview and
|
|
// therefore the rendered parent entity. This results in an infinite loop of
|
|
// parent entity output rendering the comment form and the comment form
|
|
// rendering the parent entity. To prevent this infinite loop we temporarily
|
|
// set the value of the comment field on a clone of the entity to hidden
|
|
// before calling entity_view(). That way when the output of the commented
|
|
// entity is rendered, it excludes the comment field output.
|
|
$field_name = $comment->getFieldName();
|
|
$entity = clone $entity;
|
|
$entity->$field_name->status = CommentItemInterface::HIDDEN;
|
|
$build = entity_view($entity, 'full');
|
|
}
|
|
|
|
$preview_build['comment_output_below'] = $build;
|
|
$preview_build['comment_output_below']['#weight'] = 100;
|
|
|
|
return $preview_build;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_HOOK() for block templates.
|
|
*/
|
|
function comment_preprocess_block(&$variables) {
|
|
if ($variables['configuration']['provider'] == 'comment') {
|
|
$variables['attributes']['role'] = 'navigation';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prepares variables for comment templates.
|
|
*
|
|
* Default template: comment.html.twig.
|
|
*
|
|
* @param array $variables
|
|
* An associative array containing:
|
|
* - elements: An associative array containing the comment and entity objects.
|
|
* Array keys: #comment, #commented_entity.
|
|
*/
|
|
function template_preprocess_comment(&$variables) {
|
|
/** @var \Drupal\comment\CommentInterface $comment */
|
|
$comment = $variables['elements']['#comment'];
|
|
$commented_entity = $comment->getCommentedEntity();
|
|
$variables['comment'] = $comment;
|
|
$variables['commented_entity'] = $commented_entity;
|
|
$variables['threaded'] = $variables['elements']['#comment_threaded'];
|
|
|
|
$account = $comment->getOwner();
|
|
$username = array(
|
|
'#theme' => 'username',
|
|
'#account' => $account,
|
|
);
|
|
$variables['author'] = drupal_render($username);
|
|
$variables['author_id'] = $comment->getOwnerId();
|
|
$variables['new_indicator_timestamp'] = $comment->getChangedTime();
|
|
$variables['created'] = format_date($comment->getCreatedTime());
|
|
// Avoid calling format_date() twice on the same timestamp.
|
|
if ($comment->getChangedTime() == $comment->getCreatedTime()) {
|
|
$variables['changed'] = $variables['created'];
|
|
}
|
|
else {
|
|
$variables['changed'] = format_date($comment->getChangedTime());
|
|
}
|
|
|
|
if (theme_get_setting('features.comment_user_picture')) {
|
|
// To change user picture settings (e.g., image style), edit the 'compact'
|
|
// view mode on the User entity.
|
|
$variables['user_picture'] = user_view($account, 'compact');
|
|
}
|
|
else {
|
|
$variables['user_picture'] = array();
|
|
}
|
|
|
|
if (isset($comment->in_preview)) {
|
|
$variables['title'] = \Drupal::l($comment->getSubject(), new Url('<front>'));
|
|
$variables['permalink'] = \Drupal::l(t('Permalink'), new Url('<front>'));
|
|
}
|
|
else {
|
|
$uri = $comment->urlInfo();
|
|
$attributes = $uri->getOption('attributes') ?: array();
|
|
$attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
|
|
$uri->setOption('attributes', $attributes);
|
|
$variables['title'] = \Drupal::l($comment->getSubject(), $uri);
|
|
|
|
$variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink());
|
|
}
|
|
|
|
$variables['submitted'] = t('Submitted by @username on @datetime', array('@username' => $variables['author'], '@datetime' => $variables['created']));
|
|
|
|
if ($comment->hasParentComment()) {
|
|
// Fetch and store the parent comment information for use in templates.
|
|
$comment_parent = $comment->getParentComment();
|
|
$account_parent = $comment_parent->getOwner();
|
|
$variables['parent_comment'] = $comment_parent;
|
|
$username = array(
|
|
'#theme' => 'username',
|
|
'#account' => $account_parent,
|
|
);
|
|
$variables['parent_author'] = drupal_render($username);
|
|
$variables['parent_created'] = format_date($comment_parent->getCreatedTime());
|
|
// Avoid calling format_date() twice on the same timestamp.
|
|
if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) {
|
|
$variables['parent_changed'] = $variables['parent_created'];
|
|
}
|
|
else {
|
|
$variables['parent_changed'] = format_date($comment_parent->getChangedTime());
|
|
}
|
|
$permalink_uri_parent = $comment_parent->permalink();
|
|
$attributes = $permalink_uri_parent->getOption('attributes') ?: array();
|
|
$attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
|
|
$permalink_uri_parent->setOption('attributes', $attributes);
|
|
$variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent);
|
|
$variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent);
|
|
$variables['parent'] = t('In reply to !parent_title by !parent_username',
|
|
array('!parent_username' => $variables['parent_author'], '!parent_title' => $variables['parent_title']));
|
|
}
|
|
else {
|
|
$variables['parent_comment'] = '';
|
|
$variables['parent_author'] = '';
|
|
$variables['parent_created'] = '';
|
|
$variables['parent_changed'] = '';
|
|
$variables['parent_title'] = '';
|
|
$variables['parent_permalink'] = '';
|
|
$variables['parent'] = '';
|
|
}
|
|
|
|
// Helpful $content variable for templates.
|
|
foreach (Element::children($variables['elements']) as $key) {
|
|
$variables['content'][$key] = $variables['elements'][$key];
|
|
}
|
|
|
|
// Set status to a string representation of comment->status.
|
|
if (isset($comment->in_preview)) {
|
|
$variables['status'] = 'preview';
|
|
}
|
|
else {
|
|
$variables['status'] = $comment->isPublished() ? 'published' : 'unpublished';
|
|
}
|
|
|
|
// Add comment author user ID. Necessary for the comment-by-viewer library.
|
|
$variables['attributes']['data-comment-user-id'] = $comment->getOwnerId();
|
|
}
|
|
|
|
/**
|
|
* Prepares variables for comment field templates.
|
|
*
|
|
* Default template: field--comment.html.twig.
|
|
*
|
|
* @param array $variables
|
|
* An associative array containing:
|
|
* - element: An associative array containing render arrays for the list of
|
|
* comments, and the comment form. Array keys: comments, comment_form.
|
|
*
|
|
* @todo Rename to template_preprocess_field__comment() once
|
|
* https://www.drupal.org/node/939462 is resolved.
|
|
*/
|
|
function comment_preprocess_field(&$variables) {
|
|
$element = $variables['element'];
|
|
if ($element['#field_type'] == 'comment') {
|
|
// Provide contextual information.
|
|
$variables['comment_display_mode'] = $element[0]['#comment_display_mode'];
|
|
$variables['comment_type'] = $element[0]['#comment_type'];
|
|
|
|
// Append additional attributes (eg. RDFa) from the first field item.
|
|
$variables['attributes'] += $variables['items'][0]['attributes']->storage();
|
|
|
|
// Create separate variables for the comments and comment form.
|
|
$variables['comments'] = $element[0]['comments'];
|
|
$variables['comment_form'] = $element[0]['comment_form'];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ranking().
|
|
*/
|
|
function comment_ranking() {
|
|
return \Drupal::service('comment.statistics')->getRankingInfo();
|
|
}
|