2002-02-16 13:38:00 +00:00
<?php
2000-12-14 14:13:37 +00:00
2004-07-10 07:58:47 +00:00
/**
* @file
2004-08-21 06:42:38 +00:00
* Enables users to comment on published content.
2004-07-10 07:58:47 +00:00
*
2014-11-27 16:09:30 +00:00
* When installed, the Comment module creates a field that facilitates a
2013-09-27 15:34:47 +00:00
* 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.
2004-07-10 07:58:47 +00:00
*/
2013-09-27 15:34:47 +00:00
use Drupal\comment\CommentInterface;
2014-06-13 09:34:04 +00:00
use Drupal\comment\Entity\CommentType;
2014-10-01 14:11:14 +00:00
use Drupal\Core\Entity\FieldableEntityInterface;
2014-03-10 12:25:21 +00:00
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
2016-07-19 13:47:30 +00:00
use Drupal\Core\Entity\Entity\EntityViewMode;
2014-03-10 12:25:21 +00:00
use Drupal\Core\Entity\EntityInterface;
2014-07-31 00:50:42 +00:00
use Drupal\Core\Form\FormStateInterface;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2013-12-12 23:34:44 +00:00
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
2014-03-31 17:37:55 +00:00
use Drupal\Core\Render\Element;
2019-07-24 15:48:42 +00:00
use Drupal\Core\Link;
2014-03-28 23:07:00 +00:00
use Drupal\Core\Url;
2014-09-20 02:45:52 +00:00
use Drupal\field\FieldConfigInterface;
2014-07-18 18:56:27 +00:00
use Drupal\field\FieldStorageConfigInterface;
2014-02-28 10:06:00 +00:00
use Drupal\node\NodeInterface;
2015-03-14 21:50:47 +00:00
use Drupal\user\RoleInterface;
2018-09-24 07:55:20 +00:00
use Drupal\user\UserInterface;
2012-04-26 16:44:37 +00:00
2005-11-13 09:05:38 +00:00
/**
2008-05-14 13:12:41 +00:00
* Anonymous posters cannot enter their contact information.
Issue #2807785 by claudiu.cristea, OwilliwO, mpdonadio, cllamas, klausi, xjm, Berdir, Wim Leers, Mile23, daffie, Prashant.c, dawehner, sneha_surve: Move global constants from *.module files into interfaces
2017-02-12 22:50:41 +00:00
*
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
* Use \Drupal\comment\CommentInterface::ANONYMOUS_MAYNOT_CONTACT instead.
2017-10-10 07:30:23 +00:00
*
* @see https://www.drupal.org/node/2831620
2005-11-13 09:05:38 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_ANONYMOUS_MAYNOT_CONTACT = 0;
2006-12-12 06:14:21 +00:00
/**
* Anonymous posters may leave their contact information.
Issue #2807785 by claudiu.cristea, OwilliwO, mpdonadio, cllamas, klausi, xjm, Berdir, Wim Leers, Mile23, daffie, Prashant.c, dawehner, sneha_surve: Move global constants from *.module files into interfaces
2017-02-12 22:50:41 +00:00
*
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
* Use \Drupal\comment\CommentInterface::ANONYMOUS_MAY_CONTACT instead.
2017-10-10 07:30:23 +00:00
*
* @see https://www.drupal.org/node/2831620
2006-12-12 06:14:21 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_ANONYMOUS_MAY_CONTACT = 1;
2006-12-12 06:14:21 +00:00
/**
2008-05-14 13:12:41 +00:00
* Anonymous posters are required to leave their contact information.
Issue #2807785 by claudiu.cristea, OwilliwO, mpdonadio, cllamas, klausi, xjm, Berdir, Wim Leers, Mile23, daffie, Prashant.c, dawehner, sneha_surve: Move global constants from *.module files into interfaces
2017-02-12 22:50:41 +00:00
*
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
* Use \Drupal\comment\CommentInterface::ANONYMOUS_MUST_CONTACT instead.
2017-10-10 07:30:23 +00:00
*
* @see https://www.drupal.org/node/2831620
2006-12-12 06:14:21 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_ANONYMOUS_MUST_CONTACT = 2;
2005-11-13 09:05:38 +00:00
2013-09-27 15:34:47 +00:00
/**
* 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.
*
2015-05-18 21:08:10 +00:00
* @todo Remove when https://www.drupal.org/node/1029708 lands.
2013-09-27 15:34:47 +00:00
*/
define('COMMENT_NEW_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);
2012-05-03 05:49:41 +00:00
2004-06-04 18:50:29 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-06-04 18:50:29 +00:00
*/
2014-06-30 03:33:08 +00:00
function comment_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.comment':
2009-11-26 06:44:50 +00:00
$output = '<h3>' . t('About') . '</h3>';
2017-03-04 01:20:24 +00:00
$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>.', [':comment' => 'https://www.drupal.org/documentation/modules/comment']) . '</p>';
2009-11-26 06:44:50 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2015-09-10 06:21:49 +00:00
$output .= '<dt>' . t('Enabling commenting') . '</dt>';
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t('Comment functionality can be enabled for any entity sub-type (for example, a <a href=":content-type">content type</a>) by adding a <em>Comments</em> field on its <em>Manage fields page</em>. Adding or removing commenting for an entity through the user interface requires the <a href=":field_ui">Field UI</a> module to be enabled, even though the commenting functionality works without it. For more information on fields and entities, see the <a href=":field">Field module help page</a>.', [':content-type' => (\Drupal::moduleHandler()->moduleExists('node')) ? Url::fromRoute('entity.node_type.collection')->toString() : '#', ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
2015-09-10 06:21:49 +00:00
$output .= '<dt>' . t('Configuring commenting settings') . '</dt>';
2019-05-24 06:19:41 +00:00
$output .= '<dd>' . t('Commenting settings can be configured by editing the <em>Comments</em> field on the <em>Manage fields page</em> of an entity type if the <em>Field UI module</em> is enabled. Configuration includes the label of the comments field, the number of comments to be displayed, and whether they are shown in threaded list. Commenting can be 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. Changing this configuration for an entity type will not change existing entity items.') . '</dd>';
2014-06-06 18:41:03 +00:00
$output .= '<dt>' . t('Overriding default settings') . '</dt>';
2015-09-10 06:21:49 +00:00
$output .= '<dd>' . t('Users with the appropriate permissions can override the default commenting settings of an entity type when they create an item of that type.') . '</dd>';
2015-10-05 00:41:41 +00:00
$output .= '<dt>' . t('Adding comment types') . '</dt>';
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t('Additional <em>comment types</em> can be created per entity sub-type and added on the <a href=":field">Comment types page</a>. If there are multiple comment types available you can select the appropriate one after adding a <em>Comments field</em>.', [':field' => Url::fromRoute('entity.comment_type.collection')->toString()]) . '</dd>';
2014-06-06 18:41:03 +00:00
$output .= '<dt>' . t('Approving and managing comments') . '</dt>';
2019-04-16 05:38:27 +00:00
$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.', [':comment-approval' => Url::fromRoute('comment.admin_approval')->toString(), ':admin-comment' => Url::fromRoute('comment.admin')->toString()]) . '</dd>';
2009-11-26 06:44:50 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2013-09-27 15:34:47 +00:00
2015-01-19 09:37:11 +00:00
case 'entity.comment_type.collection':
2014-06-13 09:34:04 +00:00
$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>';
2013-09-27 15:34:47 +00:00
return $output;
2007-07-05 08:48:58 +00:00
}
2002-02-22 19:44:22 +00:00
}
2010-01-13 06:26:49 +00:00
/**
2012-09-02 04:50:06 +00:00
* Entity URI callback.
2010-01-13 06:26:49 +00:00
*/
2013-12-22 21:11:36 +00:00
function comment_uri(CommentInterface $comment) {
2014-03-28 23:07:00 +00:00
return new Url(
2014-08-04 17:19:44 +00:00
'entity.comment.canonical',
2017-03-04 01:20:24 +00:00
[
2014-01-31 15:08:43 +00:00
'comment' => $comment->id(),
2017-03-04 01:20:24 +00:00
],
['fragment' => 'comment-' . $comment->id()]
2010-02-11 15:52:13 +00:00
);
2010-01-13 06:26:49 +00:00
}
2010-01-30 04:14:17 +00:00
/**
2014-03-28 10:30:44 +00:00
* Implements hook_entity_extra_field_info().
2010-01-30 04:14:17 +00:00
*/
2014-03-28 10:30:44 +00:00
function comment_entity_extra_field_info() {
2017-03-04 01:20:24 +00:00
$return = [];
2014-06-13 09:34:04 +00:00
foreach (CommentType::loadMultiple() as $comment_type) {
2017-03-04 01:20:24 +00:00
$return['comment'][$comment_type->id()] = [
'form' => [
'author' => [
2014-06-13 09:34:04 +00:00
'label' => t('Author'),
'description' => t('Author textfield'),
'weight' => -2,
2017-03-04 01:20:24 +00:00
],
],
];
$return['comment'][$comment_type->id()]['display']['links'] = [
2014-09-05 16:29:12 +00:00
'label' => t('Links'),
'description' => t('Comment operation links'),
'weight' => 100,
'visible' => TRUE,
2017-03-04 01:20:24 +00:00
];
2010-01-30 04:14:17 +00:00
}
return $return;
}
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function comment_theme() {
2017-03-04 01:20:24 +00:00
return [
'comment' => [
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2017-03-04 01:20:24 +00:00
],
'field__comment' => [
2014-07-21 16:15:37 +00:00
'base hook' => 'field',
2017-03-04 01:20:24 +00:00
],
];
2007-04-06 13:27:23 +00:00
}
2014-01-24 18:45:20 +00:00
/**
2014-09-20 02:45:52 +00:00
* Implements hook_ENTITY_TYPE_create() for 'field_config'.
2014-01-24 18:45:20 +00:00
*/
2014-09-20 02:45:52 +00:00
function comment_field_config_create(FieldConfigInterface $field) {
if ($field->getType() == 'comment' && !$field->isSyncing()) {
// Assign default values for the field.
2015-09-10 09:28:11 +00:00
$default_value = $field->getDefaultValueLiteral();
2017-03-04 01:20:24 +00:00
$default_value += [[]];
$default_value[0] += [
2014-03-10 12:25:21 +00:00
'status' => CommentItemInterface::OPEN,
2013-09-27 15:34:47 +00:00
'cid' => 0,
'last_comment_timestamp' => 0,
'last_comment_name' => '',
'last_comment_uid' => 0,
'comment_count' => 0,
2017-03-04 01:20:24 +00:00
];
2015-09-10 09:28:11 +00:00
$field->setDefaultValue($default_value);
2013-09-27 15:34:47 +00:00
}
2009-08-11 15:50:56 +00:00
}
2009-07-31 19:44:21 +00:00
2009-08-11 15:50:56 +00:00
/**
2014-09-20 02:45:52 +00:00
* Implements hook_ENTITY_TYPE_update() for 'field_config'.
2009-08-11 15:50:56 +00:00
*/
2014-09-20 02:45:52 +00:00
function comment_field_config_update(FieldConfigInterface $field) {
if ($field->getType() == 'comment') {
2013-12-20 12:07:20 +00:00
// Comment field settings also affects the rendering of *comment* entities,
// not only the *commented* entities.
2019-05-24 06:44:36 +00:00
\Drupal::entityTypeManager()->getViewBuilder('comment')->resetCache();
2009-08-11 15:50:56 +00:00
}
}
2009-07-31 19:44:21 +00:00
2014-05-27 08:34:19 +00:00
/**
2014-07-18 18:56:27 +00:00
* Implements hook_ENTITY_TYPE_insert() for 'field_storage_config'.
2014-05-27 08:34:19 +00:00
*/
2014-07-18 18:56:27 +00:00
function comment_field_storage_config_insert(FieldStorageConfigInterface $field_storage) {
if ($field_storage->getType() == 'comment') {
2014-06-08 16:14:00 +00:00
// Check that the target entity type uses an integer ID.
2014-07-18 18:56:27 +00:00
$entity_type_id = $field_storage->getTargetEntityTypeId();
2014-06-08 16:14:00 +00:00
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');
}
2014-05-27 08:34:19 +00:00
}
}
2013-09-27 15:34:47 +00:00
/**
2014-09-20 02:45:52 +00:00
* Implements hook_ENTITY_TYPE_delete() for 'field_config'.
2013-09-27 15:34:47 +00:00
*/
2014-09-20 02:45:52 +00:00
function comment_field_config_delete(FieldConfigInterface $field) {
if ($field->getType() == 'comment') {
2013-09-27 15:34:47 +00:00
// Delete all comments that used by the entity bundle.
2014-07-07 09:45:21 +00:00
$entity_query = \Drupal::entityQuery('comment');
2014-09-20 02:45:52 +00:00
$entity_query->condition('entity_type', $field->getEntityTypeId());
$entity_query->condition('field_name', $field->getName());
2014-07-07 09:45:21 +00:00
$cids = $entity_query->execute();
2019-05-02 03:00:25 +00:00
$comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
$comments = $comment_storage->loadMultiple($cids);
$comment_storage->delete($comments);
2010-12-18 01:50:16 +00:00
}
2010-01-07 05:23:52 +00:00
}
2004-07-08 19:58:10 +00:00
/**
2014-02-28 10:06:00 +00:00
* Implements hook_node_links_alter().
2004-07-08 19:58:10 +00:00
*/
2016-02-03 08:42:10 +00:00
function comment_node_links_alter(array &$links, NodeInterface $node, array &$context) {
2014-02-28 10:06:00 +00:00
// 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.
2015-05-18 21:08:10 +00:00
// @todo Make this configurable from the formatter. See
// https://www.drupal.org/node/1901110.
2014-02-28 10:06:00 +00:00
2016-02-03 08:42:10 +00:00
$comment_links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context);
$links += $comment_links;
2004-07-08 19:58:10 +00:00
}
2015-06-08 14:48:29 +00:00
/**
* Implements hook_entity_view().
*/
2015-10-05 23:33:05 +00:00
function comment_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
2015-06-08 14:48:29 +00:00
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.
2017-03-04 01:20:24 +00:00
$options = [
2015-06-08 14:48:29 +00:00
'fragment' => 'comments',
'absolute' => TRUE,
2017-03-04 01:20:24 +00:00
];
$entity->rss_elements[] = [
2015-06-08 14:48:29 +00:00
'key' => 'comments',
2018-12-23 13:30:20 +00:00
'value' => $entity->toUrl('canonical', $options)->toString(),
2017-03-04 01:20:24 +00:00
];
2015-06-08 14:48:29 +00:00
}
}
}
}
2013-09-11 13:27:11 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_view_alter() for node entities.
2013-09-11 13:27:11 +00:00
*/
2014-05-02 21:43:59 +00:00
function comment_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) {
2013-09-16 03:58:06 +00:00
if (\Drupal::moduleHandler()->moduleExists('history')) {
2013-09-11 13:27:11 +00:00
$build['#attributes']['data-history-node-id'] = $node->id();
}
}
2009-07-28 10:09:25 +00:00
/**
2012-01-03 04:36:15 +00:00
* Generates an array for rendering a comment.
2009-07-28 10:09:25 +00:00
*
2013-09-27 15:34:47 +00:00
* @param \Drupal\comment\CommentInterface $comment
2012-01-03 04:36:15 +00:00
* The comment object.
Issue #1808478 by Mile23, prhavile, naveenvalecha, llbbl, hctom, nikunjkotecha, Lars Toomre, deepakaryan1988, mrjmd, jgeryk, mlevasseur: Add missing type hinting to Comment module docblocks
2016-02-24 05:23:02 +00:00
* @param string $view_mode
2015-12-03 19:04:31 +00:00
* (optional) View mode; for instance, 'full', 'teaser', etc. Defaults to
* 'full'.
Issue #1808478 by Mile23, prhavile, naveenvalecha, llbbl, hctom, nikunjkotecha, Lars Toomre, deepakaryan1988, mrjmd, jgeryk, mlevasseur: Add missing type hinting to Comment module docblocks
2016-02-24 05:23:02 +00:00
* @param string $langcode
2010-10-03 01:15:34 +00:00
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
2009-07-28 10:09:25 +00:00
*
2013-12-22 21:11:36 +00:00
* @return array
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* An array as expected by \Drupal\Core\Render\RendererInterface::render().
2015-07-06 11:19:15 +00:00
*
* @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
2019-04-29 21:35:23 +00:00
* Use \Drupal::entityTypeManager()->getViewBuilder('comment')->view().
*
* @see https://www.drupal.org/node/3033656
2009-07-28 10:09:25 +00:00
*/
2013-09-27 15:34:47 +00:00
function comment_view(CommentInterface $comment, $view_mode = 'full', $langcode = NULL) {
2019-04-29 21:35:23 +00:00
@trigger_error("comment_view() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->view() instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED);
return \Drupal::entityTypeManager()
->getViewBuilder('comment')
->view($comment, $view_mode, $langcode);
2009-07-28 10:09:25 +00:00
}
/**
2012-01-03 04:36:15 +00:00
* Constructs render array from an array of loaded comments.
2009-07-28 10:09:25 +00:00
*
Issue #1808478 by Mile23, prhavile, naveenvalecha, llbbl, hctom, nikunjkotecha, Lars Toomre, deepakaryan1988, mrjmd, jgeryk, mlevasseur: Add missing type hinting to Comment module docblocks
2016-02-24 05:23:02 +00:00
* @param \Drupal\comment\CommentInterface[] $comments
2013-10-09 05:43:02 +00:00
* An array of comments as returned by entity_load_multiple().
Issue #1808478 by Mile23, prhavile, naveenvalecha, llbbl, hctom, nikunjkotecha, Lars Toomre, deepakaryan1988, mrjmd, jgeryk, mlevasseur: Add missing type hinting to Comment module docblocks
2016-02-24 05:23:02 +00:00
* @param string $view_mode
2015-12-03 19:04:31 +00:00
* (optional) View mode; for instance, 'full', 'teaser', etc. Defaults to
* 'full'.
Issue #1808478 by Mile23, prhavile, naveenvalecha, llbbl, hctom, nikunjkotecha, Lars Toomre, deepakaryan1988, mrjmd, jgeryk, mlevasseur: Add missing type hinting to Comment module docblocks
2016-02-24 05:23:02 +00:00
* @param string $langcode
2013-09-27 15:34:47 +00:00
* (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.
2010-10-03 01:15:34 +00:00
*
2013-12-22 21:11:36 +00:00
* @return array
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* An array in the format expected by
* \Drupal\Core\Render\RendererInterface::render().
2012-01-03 04:36:15 +00:00
*
2015-07-06 11:19:15 +00:00
* @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
2019-04-29 21:35:23 +00:00
* Use \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple().
2015-07-06 11:19:15 +00:00
*
2019-04-29 21:35:23 +00:00
* @see https://www.drupal.org/node/3033656
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* @see \Drupal\Core\Render\RendererInterface::render()
2009-07-28 10:09:25 +00:00
*/
2012-10-14 06:40:03 +00:00
function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) {
2019-04-29 21:35:23 +00:00
@trigger_error("comment_view_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple() instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED);
return \Drupal::entityTypeManager()
->getViewBuilder('comment')
->viewMultiple($comments, $view_mode, $langcode);
2009-07-28 10:09:25 +00:00
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
Issue #1963340 by amateescu, dags, andypost, agentrickard, mgifford, yoroy, pguillard, jibran, YesCT, xjm, LewisNyman, swentel, Hydra, yched, tim.plunkett, rteijeiro, ainz, Xano, Bojhan, Berdir: Change field UI so that adding a field is a separate task
2014-12-14 22:57:59 +00:00
* Implements hook_form_FORM_ID_alter() for field_ui_field_storage_add_form.
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
Issue #1963340 by amateescu, dags, andypost, agentrickard, mgifford, yoroy, pguillard, jibran, YesCT, xjm, LewisNyman, swentel, Hydra, yched, tim.plunkett, rteijeiro, ainz, Xano, Bojhan, Berdir: Change field UI so that adding a field is a separate task
2014-12-14 22:57:59 +00:00
function comment_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state) {
2015-01-09 15:14:23 +00:00
$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'));
2013-09-27 15:34:47 +00:00
}
Issue #1963340 by amateescu, dags, andypost, agentrickard, mgifford, yoroy, pguillard, jibran, YesCT, xjm, LewisNyman, swentel, Hydra, yched, tim.plunkett, rteijeiro, ainz, Xano, Bojhan, Berdir: Change field UI so that adding a field is a separate task
2014-12-14 22:57:59 +00:00
if (!_comment_entity_uses_integer_id($form_state->get('entity_type_id'))) {
2015-09-13 11:25:37 +00:00
$optgroup = (string) t('General');
2014-06-08 16:14:00 +00:00
// You cannot use comment fields on entity types with non-integer IDs.
2015-09-13 11:25:37 +00:00
unset($form['add']['new_storage_type']['#options'][$optgroup]['comment']);
2014-06-08 16:14:00 +00:00
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
2009-01-22 03:11:54 +00:00
/**
2013-09-27 15:34:47 +00:00
* Implements hook_form_FORM_ID_alter().
2009-01-22 03:11:54 +00:00
*/
2014-08-04 11:21:15 +00:00
function comment_form_field_ui_form_display_overview_form_alter(&$form, FormStateInterface $form_state) {
2015-01-09 15:14:23 +00:00
$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'));
2005-11-25 10:11:59 +00:00
}
}
2004-07-08 19:58:10 +00:00
/**
2013-09-27 15:34:47 +00:00
* Implements hook_form_FORM_ID_alter().
2004-07-08 19:58:10 +00:00
*/
2014-08-04 11:21:15 +00:00
function comment_form_field_ui_display_overview_form_alter(&$form, FormStateInterface $form_state) {
2015-01-09 15:14:23 +00:00
$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'));
2008-12-05 22:18:46 +00:00
}
2013-09-27 15:34:47 +00:00
}
2008-12-05 22:18:46 +00:00
2013-09-27 15:34:47 +00:00
/**
Issue #597236 by Berdir, catch, msonnabaum, Xano, Wim Leers, jhedstrom, amateescu, corvus_ch, swentel, moshe weitzman, Gábor Hojtsy, riccardoR, killes@www.drop.org, et al: Add entity caching to core.
2014-07-18 10:53:52 +00:00
* Implements hook_entity_storage_load().
2013-09-27 15:34:47 +00:00
*
2014-02-24 11:42:13 +00:00
* @see \Drupal\comment\Plugin\Field\FieldType\CommentItem::propertyDefinitions()
2008-10-06 12:55:56 +00:00
*/
Issue #597236 by Berdir, catch, msonnabaum, Xano, Wim Leers, jhedstrom, amateescu, corvus_ch, swentel, moshe weitzman, Gábor Hojtsy, riccardoR, killes@www.drop.org, et al: Add entity caching to core.
2014-07-18 10:53:52 +00:00
function comment_entity_storage_load($entities, $entity_type) {
2014-04-22 10:14:42 +00:00
// Comments can only be attached to content entities, so skip others.
2019-05-24 06:44:36 +00:00
if (!\Drupal::entityTypeManager()->getDefinition($entity_type)->entityClassImplements(FieldableEntityInterface::class)) {
2014-04-22 10:14:42 +00:00
return;
}
2017-03-20 21:53:37 +00:00
if (!\Drupal::service('comment.manager')->getFields($entity_type)) {
2013-09-27 15:34:47 +00:00
// Do not query database when entity has no comment fields.
return;
}
2014-03-25 18:41:33 +00:00
// 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);
2013-09-27 15:34:47 +00:00
foreach ($result as $record) {
2013-12-11 10:59:20 +00:00
// Skip fields that entity does not have.
2014-06-13 09:34:04 +00:00
if (!$entities[$record->entity_id]->hasField($record->field_name)) {
2013-12-11 10:59:20 +00:00
continue;
}
2014-06-13 09:34:04 +00:00
$comment_statistics = $entities[$record->entity_id]->get($record->field_name);
2013-09-27 15:34:47 +00:00
$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;
2008-10-06 12:55:56 +00:00
}
}
2005-01-24 21:20:16 +00:00
2008-10-06 12:55:56 +00:00
/**
2013-09-27 15:34:47 +00:00
* Implements hook_entity_insert().
2008-10-06 12:55:56 +00:00
*/
2013-09-27 15:34:47 +00:00
function comment_entity_insert(EntityInterface $entity) {
2010-04-16 13:56:45 +00:00
// Allow bulk updates and inserts to temporarily disable the
2013-09-27 15:34:47 +00:00
// maintenance of the {comment_entity_statistics} table.
if (\Drupal::state()->get('comment.maintain_entity_statistics') &&
2014-01-30 12:06:58 +00:00
$fields = \Drupal::service('comment.manager')->getFields($entity->getEntityTypeId())) {
2014-03-25 18:41:33 +00:00
\Drupal::service('comment.statistics')->create($entity, $fields);
2010-04-16 13:56:45 +00:00
}
2008-10-06 12:55:56 +00:00
}
2006-08-31 21:58:36 +00:00
2008-10-06 12:55:56 +00:00
/**
2013-09-27 15:34:47 +00:00
* Implements hook_entity_predelete().
2008-10-06 12:55:56 +00:00
*/
2013-09-27 15:34:47 +00:00
function comment_entity_predelete(EntityInterface $entity) {
2014-03-12 18:11:16 +00:00
// 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.
2014-10-01 14:11:14 +00:00
if ($entity instanceof FieldableEntityInterface && is_numeric($entity->id())) {
2014-06-24 19:46:19 +00:00
$entity_query = \Drupal::entityQuery('comment');
$entity_query->condition('entity_id', (int) $entity->id());
$entity_query->condition('entity_type', $entity->getEntityTypeId());
$cids = $entity_query->execute();
2019-05-02 03:00:25 +00:00
$comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
$comments = $comment_storage->loadMultiple($cids);
$comment_storage->delete($comments);
2014-03-25 18:41:33 +00:00
\Drupal::service('comment.statistics')->delete($entity);
2014-03-12 18:11:16 +00:00
}
2008-10-06 12:55:56 +00:00
}
2006-08-31 21:58:36 +00:00
2014-06-08 16:14:00 +00:00
/**
* 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) {
2019-05-24 06:44:36 +00:00
$entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
2014-06-08 16:14:00 +00:00
$entity_type_id_key = $entity_type->getKey('id');
2014-06-18 20:10:59 +00:00
if ($entity_type_id_key === FALSE) {
return FALSE;
}
2019-04-26 07:09:28 +00:00
$field_definitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type->id());
2014-06-08 16:14:00 +00:00
$entity_type_id_definition = $field_definitions[$entity_type_id_key];
return $entity_type_id_definition->getType() === 'integer';
}
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_update_index().
2008-10-06 12:55:56 +00:00
*/
2015-10-05 23:33:05 +00:00
function comment_node_update_index(EntityInterface $node) {
2010-06-08 06:34:36 +00:00
$index_comments = &drupal_static(__FUNCTION__);
if ($index_comments === NULL) {
2013-06-24 16:38:32 +00:00
// 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.
2010-06-08 06:34:36 +00:00
$index_comments = TRUE;
2019-05-24 06:44:36 +00:00
$roles = \Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple();
2015-03-14 21:50:47 +00:00
$authenticated_can_access = $roles[RoleInterface::AUTHENTICATED_ID]->hasPermission('access comments');
2013-06-24 16:38:32 +00:00
foreach ($roles as $rid => $role) {
if ($role->hasPermission('search content') && !$role->hasPermission('access comments')) {
2015-03-14 21:50:47 +00:00
if ($rid == RoleInterface::AUTHENTICATED_ID || $rid == RoleInterface::ANONYMOUS_ID || !$authenticated_can_access) {
2013-06-24 16:38:32 +00:00
$index_comments = FALSE;
break;
}
2010-06-08 06:34:36 +00:00
}
}
}
2017-03-04 01:20:24 +00:00
$build = [];
2013-09-27 15:34:47 +00:00
2010-06-08 06:34:36 +00:00
if ($index_comments) {
2013-09-27 15:34:47 +00:00
foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $info) {
// Skip fields that entity does not have.
2013-10-18 06:16:26 +00:00
if (!$node->hasField($field_name)) {
2013-09-27 15:34:47 +00:00
continue;
}
2014-05-27 08:34:19 +00:00
$field_definition = $node->getFieldDefinition($field_name);
$mode = $field_definition->getSetting('default_mode');
$comments_per_page = $field_definition->getSetting('per_page');
2014-07-18 22:30:49 +00:00
if ($node->get($field_name)->status) {
2019-05-24 06:44:36 +00:00
$comments = \Drupal::entityTypeManager()->getStorage('comment')
2014-07-18 22:30:49 +00:00
->loadThread($node, $field_name, $mode, $comments_per_page);
if ($comments) {
2019-05-24 06:44:36 +00:00
$build[] = \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple($comments);
2014-07-18 22:30:49 +00:00
}
2013-09-27 15:34:47 +00:00
}
2010-06-08 06:34:36 +00:00
}
2008-10-06 12:55:56 +00:00
}
2015-06-24 21:31:20 +00:00
return \Drupal::service('renderer')->renderPlain($build);
2008-10-06 12:55:56 +00:00
}
2005-01-24 21:20:16 +00:00
2008-12-31 12:02:24 +00:00
/**
2014-02-12 10:56:06 +00:00
* Implements hook_cron().
2008-12-31 12:02:24 +00:00
*/
2014-02-12 10:56:06 +00:00
function comment_cron() {
// Store the maximum possible comments per thread (used for node search
// ranking by reply count).
2014-03-25 18:41:33 +00:00
\Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, \Drupal::service('comment.statistics')->getMaximumCount('node')));
2008-12-31 12:02:24 +00:00
}
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_search_result().
2010-04-26 14:26:46 +00:00
*
* Formats a comment count string and returns it, for display with search
* results.
2008-10-06 12:55:56 +00:00
*/
2013-03-10 19:05:24 +00:00
function comment_node_search_result(EntityInterface $node) {
2013-09-27 15:34:47 +00:00
$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.
2013-10-18 06:16:26 +00:00
if (!$node->hasField($field_name)) {
2013-09-27 15:34:47 +00:00
continue;
}
// Do not make a string if comments are hidden.
$status = $node->get($field_name)->status;
2014-03-10 12:25:21 +00:00
if (\Drupal::currentUser()->hasPermission('access comments') && $status != CommentItemInterface::HIDDEN) {
if ($status == CommentItemInterface::OPEN) {
2013-09-27 15:34:47 +00:00
// At least one comment field is open.
$open = TRUE;
}
$comments += $node->get($field_name)->comment_count;
2010-04-26 14:26:46 +00:00
}
2009-05-25 15:43:38 +00:00
}
2013-09-27 15:34:47 +00:00
// 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) {
2017-03-04 01:20:24 +00:00
return ['comment' => \Drupal::translation()->formatPlural($comments, '1 comment', '@count comments')];
2013-09-27 15:34:47 +00:00
}
2008-10-06 12:55:56 +00:00
}
2005-09-19 19:13:35 +00:00
2004-07-08 19:58:10 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_cancel().
2004-07-08 19:58:10 +00:00
*/
2018-09-24 07:55:20 +00:00
function comment_user_cancel($edit, UserInterface $account, $method) {
2009-01-08 08:42:13 +00:00
switch ($method) {
case 'user_cancel_block_unpublish':
2019-04-30 07:41:05 +00:00
$comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['uid' => $account->id()]);
2010-06-02 06:20:11 +00:00
foreach ($comments as $comment) {
2018-05-05 21:36:51 +00:00
$comment->setUnpublished();
2013-06-11 16:38:12 +00:00
$comment->save();
2010-06-02 06:20:11 +00:00
}
2009-01-08 08:42:13 +00:00
break;
case 'user_cancel_reassign':
2014-02-03 16:02:31 +00:00
/** @var \Drupal\comment\CommentInterface[] $comments */
2019-04-30 07:41:05 +00:00
$comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['uid' => $account->id()]);
2010-06-02 06:20:11 +00:00
foreach ($comments as $comment) {
2014-02-03 16:02:31 +00:00
$comment->setOwnerId(0);
2015-04-19 18:41:59 +00:00
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
2013-06-11 16:38:12 +00:00
$comment->save();
2010-06-02 06:20:11 +00:00
}
2009-01-08 08:42:13 +00:00
break;
}
2004-07-08 19:58:10 +00:00
}
2010-03-12 15:56:30 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_predelete() for user entities.
2010-03-12 15:56:30 +00:00
*/
2011-12-17 12:43:37 +00:00
function comment_user_predelete($account) {
2014-06-24 19:42:56 +00:00
$entity_query = \Drupal::entityQuery('comment');
$entity_query->condition('uid', $account->id());
$cids = $entity_query->execute();
2019-05-02 03:00:25 +00:00
$comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
$comments = $comment_storage->loadMultiple($cids);
$comment_storage->delete($comments);
2009-07-31 19:44:21 +00:00
}
2009-07-21 21:48:24 +00:00
/**
2012-01-03 04:36:15 +00:00
* Generates a comment preview.
*
2013-09-27 15:34:47 +00:00
* @param \Drupal\comment\CommentInterface $comment
2013-12-22 21:11:36 +00:00
* The comment entity to preview.
2014-07-31 00:50:42 +00:00
* @param Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
2013-12-22 21:11:36 +00:00
*
* @return array
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* An array as expected by \Drupal\Core\Render\RendererInterface::render().
2009-07-21 21:48:24 +00:00
*/
2014-07-31 00:50:42 +00:00
function comment_preview(CommentInterface $comment, FormStateInterface $form_state) {
2017-03-04 01:20:24 +00:00
$preview_build = [];
2014-02-16 07:20:53 +00:00
$entity = $comment->getCommentedEntity();
2009-07-21 21:48:24 +00:00
2014-09-08 16:01:19 +00:00
if (!$form_state->getErrors()) {
2009-07-28 10:09:25 +00:00
$comment->in_preview = TRUE;
2016-01-19 14:22:33 +00:00
$comment_build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($comment);
2010-07-29 00:50:56 +00:00
$comment_build['#weight'] = -100;
2008-05-14 13:12:41 +00:00
2012-08-16 11:30:43 +00:00
$preview_build['comment_preview'] = $comment_build;
2009-07-28 10:09:25 +00:00
}
2005-11-21 08:32:31 +00:00
2014-02-16 07:20:53 +00:00
if ($comment->hasParentComment()) {
2017-03-04 01:20:24 +00:00
$build = [];
2014-02-16 07:20:53 +00:00
$parent = $comment->getParentComment();
if ($parent && $parent->isPublished()) {
2016-01-19 14:22:33 +00:00
$build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($parent);
2009-07-31 19:44:21 +00:00
}
2005-11-21 08:32:31 +00:00
}
else {
2013-09-27 15:34:47 +00:00
// 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
2014-05-02 21:43:59 +00:00
// set the value of the comment field on a clone of the entity to hidden
2013-09-27 15:34:47 +00:00
// before calling entity_view(). That way when the output of the commented
2014-05-02 21:43:59 +00:00
// entity is rendered, it excludes the comment field output.
2014-02-16 07:20:53 +00:00
$field_name = $comment->getFieldName();
2014-05-02 21:43:59 +00:00
$entity = clone $entity;
$entity->$field_name->status = CommentItemInterface::HIDDEN;
2019-04-29 21:35:23 +00:00
$build = \Drupal::entityTypeManager()
->getViewBuilder($entity->getEntityTypeId())
->view($entity, 'full');
2005-11-21 08:32:31 +00:00
}
2012-08-16 11:30:43 +00:00
$preview_build['comment_output_below'] = $build;
$preview_build['comment_output_below']['#weight'] = 100;
2011-05-14 02:16:34 +00:00
2012-08-16 11:30:43 +00:00
return $preview_build;
2005-11-21 08:32:31 +00:00
}
2012-01-10 03:22:24 +00:00
/**
2013-10-03 20:55:34 +00:00
* Implements hook_preprocess_HOOK() for block templates.
2012-01-10 03:22:24 +00:00
*/
function comment_preprocess_block(&$variables) {
2014-04-01 21:14:13 +00:00
if ($variables['configuration']['provider'] == 'comment') {
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'navigation';
2012-01-10 03:22:24 +00:00
}
}
2007-04-27 07:42:54 +00:00
/**
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
* Prepares variables for comment templates.
2007-07-20 08:51:13 +00:00
*
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
* Default template: comment.html.twig.
*
* @param array $variables
* An associative array containing:
2013-09-27 15:34:47 +00:00
* - elements: An associative array containing the comment and entity objects.
* Array keys: #comment, #commented_entity.
2007-04-27 07:42:54 +00:00
*/
function template_preprocess_comment(&$variables) {
2019-01-01 19:15:24 +00:00
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
2014-02-03 16:02:31 +00:00
/** @var \Drupal\comment\CommentInterface $comment */
2009-07-28 10:09:25 +00:00
$comment = $variables['elements']['#comment'];
2014-02-16 07:20:53 +00:00
$commented_entity = $comment->getCommentedEntity();
2011-12-05 12:06:35 +00:00
$variables['comment'] = $comment;
2013-09-27 15:34:47 +00:00
$variables['commented_entity'] = $commented_entity;
2015-06-02 15:13:44 +00:00
$variables['threaded'] = $variables['elements']['#comment_threaded'];
2012-12-11 19:00:31 +00:00
2015-04-07 11:39:10 +00:00
$account = $comment->getOwner();
2017-03-04 01:20:24 +00:00
$username = [
2013-06-29 13:43:09 +00:00
'#theme' => 'username',
'#account' => $account,
2017-03-04 01:20:24 +00:00
];
Issue #2704871 by pepegarciag, Mile23, Manuel Garcia, ieguskiza, snehi, gaurav.pahuja, dimaro, rajeshwari10, gargsuchi, pashupathi nath gajawada, penyaskito, xjm, ZeiP, Wim Leers, alexpott: Replace usages of deprecated method drupal_render()
2017-07-03 15:35:13 +00:00
$variables['author'] = \Drupal::service('renderer')->render($username);
2014-11-01 14:22:09 +00:00
$variables['author_id'] = $comment->getOwnerId();
2014-02-16 07:20:53 +00:00
$variables['new_indicator_timestamp'] = $comment->getChangedTime();
2019-01-01 19:15:24 +00:00
$variables['created'] = $date_formatter->format($comment->getCreatedTime());
// Avoid calling DateFormatterInterface::format() twice on the same timestamp.
2014-02-16 07:20:53 +00:00
if ($comment->getChangedTime() == $comment->getCreatedTime()) {
2012-12-11 19:00:31 +00:00
$variables['changed'] = $variables['created'];
}
else {
2019-01-01 19:15:24 +00:00
$variables['changed'] = $date_formatter->format($comment->getChangedTime());
2012-12-11 19:00:31 +00:00
}
2011-12-05 12:06:35 +00:00
2013-05-07 09:47:19 +00:00
if (theme_get_setting('features.comment_user_picture')) {
2015-12-03 19:04:31 +00:00
// To change user picture settings (for instance, image style), edit the
// 'compact' view mode on the User entity.
2019-04-29 21:35:23 +00:00
$variables['user_picture'] = \Drupal::entityTypeManager()
->getViewBuilder('user')
->view($account, 'compact');
2012-11-26 10:38:45 +00:00
}
else {
2017-03-04 01:20:24 +00:00
$variables['user_picture'] = [];
2012-11-26 10:38:45 +00:00
}
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
2014-01-31 15:08:43 +00:00
if (isset($comment->in_preview)) {
2019-07-24 15:48:42 +00:00
$variables['title'] = Link::fromTextAndUrl($comment->getSubject(), Url::fromRoute('<front>'))->toString();
$variables['permalink'] = Link::fromTextAndUrl(t('Permalink'), Url::fromRoute('<front>'))->toString();
2014-01-31 15:08:43 +00:00
}
else {
2016-08-30 13:34:08 +00:00
$uri = $comment->permalink();
2017-03-04 01:20:24 +00:00
$attributes = $uri->getOption('attributes') ?: [];
$attributes += ['class' => ['permalink'], 'rel' => 'bookmark'];
2014-03-28 23:07:00 +00:00
$uri->setOption('attributes', $attributes);
2019-07-24 15:48:42 +00:00
$variables['title'] = Link::fromTextAndUrl($comment->getSubject(), $uri)->toString();
2010-04-06 19:49:03 +00:00
2019-07-24 15:48:42 +00:00
$variables['permalink'] = Link::fromTextAndUrl(t('Permalink'), $comment->permalink())->toString();
2014-01-31 15:08:43 +00:00
}
2010-09-23 19:47:31 +00:00
2017-03-04 01:20:24 +00:00
$variables['submitted'] = t('Submitted by @username on @datetime', ['@username' => $variables['author'], '@datetime' => $variables['created']]);
2010-01-10 00:41:28 +00:00
2014-02-16 07:20:53 +00:00
if ($comment->hasParentComment()) {
2012-10-27 19:41:47 +00:00
// Fetch and store the parent comment information for use in templates.
2014-02-16 07:20:53 +00:00
$comment_parent = $comment->getParentComment();
2015-04-07 11:39:10 +00:00
$account_parent = $comment_parent->getOwner();
2012-10-27 19:41:47 +00:00
$variables['parent_comment'] = $comment_parent;
2017-03-04 01:20:24 +00:00
$username = [
2013-06-29 13:43:09 +00:00
'#theme' => 'username',
'#account' => $account_parent,
2017-03-04 01:20:24 +00:00
];
Issue #2704871 by pepegarciag, Mile23, Manuel Garcia, ieguskiza, snehi, gaurav.pahuja, dimaro, rajeshwari10, gargsuchi, pashupathi nath gajawada, penyaskito, xjm, ZeiP, Wim Leers, alexpott: Replace usages of deprecated method drupal_render()
2017-07-03 15:35:13 +00:00
$variables['parent_author'] = \Drupal::service('renderer')->render($username);
2019-01-01 19:15:24 +00:00
$variables['parent_created'] = $date_formatter->format($comment_parent->getCreatedTime());
// Avoid calling DateFormatterInterface::format() twice on same timestamp.
2014-02-16 07:20:53 +00:00
if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) {
2012-12-11 19:00:31 +00:00
$variables['parent_changed'] = $variables['parent_created'];
}
else {
2019-01-01 19:15:24 +00:00
$variables['parent_changed'] = $date_formatter->format($comment_parent->getChangedTime());
2012-12-11 19:00:31 +00:00
}
2013-06-13 08:19:53 +00:00
$permalink_uri_parent = $comment_parent->permalink();
2017-03-04 01:20:24 +00:00
$attributes = $permalink_uri_parent->getOption('attributes') ?: [];
$attributes += ['class' => ['permalink'], 'rel' => 'bookmark'];
2014-03-28 23:07:00 +00:00
$permalink_uri_parent->setOption('attributes', $attributes);
2019-07-24 15:48:42 +00:00
$variables['parent_title'] = Link::fromTextAndUrl($comment_parent->getSubject(), $permalink_uri_parent)->toString();
$variables['parent_permalink'] = Link::fromTextAndUrl(t('Parent permalink'), $permalink_uri_parent)->toString();
2015-09-15 12:36:26 +00:00
$variables['parent'] = t('In reply to @parent_title by @parent_username',
2017-03-04 01:20:24 +00:00
['@parent_username' => $variables['parent_author'], '@parent_title' => $variables['parent_title']]);
2012-10-27 19:41:47 +00:00
}
else {
$variables['parent_comment'] = '';
$variables['parent_author'] = '';
$variables['parent_created'] = '';
$variables['parent_changed'] = '';
$variables['parent_title'] = '';
$variables['parent_permalink'] = '';
$variables['parent'] = '';
}
2009-09-10 12:33:46 +00:00
// Helpful $content variable for templates.
2014-03-31 17:37:55 +00:00
foreach (Element::children($variables['elements']) as $key) {
2009-09-10 12:33:46 +00:00
$variables['content'][$key] = $variables['elements'][$key];
}
2009-10-09 01:00:08 +00:00
2008-05-14 13:12:41 +00:00
// Set status to a string representation of comment->status.
2009-07-28 10:09:25 +00:00
if (isset($comment->in_preview)) {
2012-03-11 12:21:59 +00:00
$variables['status'] = 'preview';
2008-01-04 19:24:24 +00:00
}
else {
2014-02-16 07:20:53 +00:00
$variables['status'] = $comment->isPublished() ? 'published' : 'unpublished';
2008-01-04 19:24:24 +00:00
}
2012-01-31 05:00:17 +00:00
2013-09-11 13:27:11 +00:00
// Add comment author user ID. Necessary for the comment-by-viewer library.
2014-02-03 16:02:31 +00:00
$variables['attributes']['data-comment-user-id'] = $comment->getOwnerId();
2018-12-14 12:50:29 +00:00
// Add anchor for each comment.
$variables['attributes']['id'] = 'comment-' . $comment->id();
2002-09-15 13:00:12 +00:00
}
2006-07-19 06:19:17 +00:00
/**
2014-07-21 16:15:37 +00:00
* Prepares variables for comment field templates.
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
*
2014-07-21 16:15:37 +00:00
* Default template: field--comment.html.twig.
2007-07-20 08:51:13 +00:00
*
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
* @param array $variables
* An associative array containing:
2014-07-21 16:15:37 +00:00
* - element: An associative array containing render arrays for the list of
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
* comments, and the comment form. Array keys: comments, comment_form.
2014-07-21 16:15:37 +00:00
*
* @todo Rename to template_preprocess_field__comment() once
* https://www.drupal.org/node/939462 is resolved.
2006-07-19 06:19:17 +00:00
*/
2014-07-21 16:15:37 +00:00
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.
2014-10-07 12:17:40 +00:00
$variables['attributes'] += $variables['items'][0]['attributes']->storage();
2014-07-21 16:15:37 +00:00
// Create separate variables for the comments and comment form.
$variables['comments'] = $element[0]['comments'];
$variables['comment_form'] = $element[0]['comment_form'];
}
2006-07-19 06:19:17 +00:00
}
2008-05-14 11:10:54 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_ranking().
2008-05-14 11:10:54 +00:00
*/
function comment_ranking() {
2014-03-25 18:41:33 +00:00
return \Drupal::service('comment.statistics')->getRankingInfo();
2009-01-02 21:17:35 +00:00
}
2016-07-19 13:47:30 +00:00
/**
* Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
*/
function comment_entity_view_display_presave(EntityViewDisplayInterface $display) {
// Act only on comment view displays being disabled.
if ($display->isNew() || $display->getTargetEntityTypeId() !== 'comment' || $display->status()) {
return;
}
$storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
if (!$storage->loadUnchanged($display->getOriginalId())->status()) {
return;
}
// Disable the comment field formatter when the used view display is disabled.
foreach ($storage->loadMultiple() as $id => $view_display) {
$changed = FALSE;
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
foreach ($view_display->getComponents() as $field => $component) {
if (isset($component['type']) && ($component['type'] === 'comment_default')) {
if ($component['settings']['view_mode'] === $display->getMode()) {
$view_display->removeComponent($field);
/** @var \Drupal\Core\Entity\EntityViewModeInterface $mode */
$mode = EntityViewMode::load($display->getTargetEntityTypeId() . '.' . $display->getMode());
$arguments = [
'@id' => $view_display->id(),
'@name' => $field,
'@display' => $mode->label(),
'@mode' => $display->getMode(),
];
\Drupal::logger('system')->warning("View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.", $arguments);
$changed = TRUE;
}
}
}
if ($changed) {
$view_display->save();
}
}
}