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-05-20 09:29:40 +00:00
use Drupal\comment\Entity\Comment;
2014-07-08 22:30:11 +00:00
use Drupal\comment\CommentManagerInterface;
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;
2014-06-13 02:33:48 +00:00
use Drupal\Component\Utility\String;
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;
2014-09-02 09:36:57 +00:00
use Drupal\Core\Entity\Entity\EntityViewDisplay;
2013-12-12 23:34:44 +00:00
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
2014-05-15 17:26:18 +00:00
use Drupal\Core\Field\FieldDefinitionInterface;
2014-03-31 17:37:55 +00:00
use Drupal\Core\Render\Element;
2014-03-28 23:07:00 +00:00
use Drupal\Core\Url;
2014-07-18 18:56:27 +00:00
use Drupal\field\Entity\FieldStorageConfig;
2014-09-20 02:45:52 +00:00
use Drupal\field\FieldConfigInterface;
2014-07-18 18:56:27 +00:00
use Drupal\field\FieldStorageConfigInterface;
2013-09-27 15:34:47 +00:00
use Drupal\file\FileInterface;
2014-02-03 16:02:31 +00:00
use Drupal\user\EntityOwnerInterface;
2014-02-28 10:06:00 +00:00
use Drupal\node\NodeInterface;
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.
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.
*/
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.
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.
*
* @todo Remove when http://drupal.org/node/1029708 lands.
*/
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>';
2014-06-06 18:41:03 +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>.', array('!comment' => 'https://drupal.org/documentation/modules/comment')) . '</p>';
2009-11-26 06:44:50 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2014-06-06 18:41:03 +00:00
$output .= '<dt>' . t('Enabling commenting and configuring defaults') . '</dt>';
2015-01-19 09:37:11 +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>). 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::url('entity.node_type.collection'), '!field' => \Drupal::url('help.page', array('name' => 'field')))) . '</dd>';
2014-06-06 18:41:03 +00:00
$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>';
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',
2014-03-28 23:07:00 +00:00
array(
2014-01-31 15:08:43 +00:00
'comment' => $comment->id(),
),
2014-03-28 23:07:00 +00:00
array('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() {
2010-01-30 04:14:17 +00:00
$return = array();
2014-06-13 09:34:04 +00:00
foreach (CommentType::loadMultiple() as $comment_type) {
2014-06-14 09:03:25 +00:00
$return['comment'][$comment_type->id()] = array(
2014-06-13 09:34:04 +00:00
'form' => array(
'author' => array(
'label' => t('Author'),
'description' => t('Author textfield'),
'weight' => -2,
2010-01-30 04:14:17 +00:00
),
2014-06-13 09:34:04 +00:00
),
);
2014-09-05 16:29:12 +00:00
$return['comment'][$comment_type->id()]['display']['links'] = array(
'label' => t('Links'),
'description' => t('Comment operation links'),
'weight' => 100,
'visible' => TRUE,
);
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() {
return array(
'comment' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2007-04-06 13:27:23 +00:00
),
2014-07-21 16:15:37 +00:00
'field__comment' => array(
'base hook' => 'field',
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.
if (!isset($field->default_value)) {
$field->default_value = array();
2014-01-24 18:45:20 +00:00
}
2014-09-20 02:45:52 +00:00
$field->default_value += array(array());
$field->default_value[0] += array(
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,
2014-01-24 18:45:20 +00:00
);
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.
\Drupal::entityManager()->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();
entity_delete_multiple('comment', $cids);
2010-12-18 01:50:16 +00:00
}
2010-01-07 05:23:52 +00:00
}
2013-12-20 12:07:20 +00:00
/**
2014-05-02 21:43:59 +00:00
* Implements hook_entity_build_defaults_alter().
2015-02-27 13:18:52 +00:00
*
* @todo Remove this hook implementation in https://www.drupal.org/node/2433599
2013-12-20 12:07:20 +00:00
*/
2014-05-02 21:43:59 +00:00
function comment_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
// Get the corresponding display settings.
$display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
2013-12-20 12:07:20 +00:00
// Add the comment page number to the cache key if render caching is enabled.
2015-02-27 13:18:52 +00:00
if (isset($build['#cache']) && isset($build['#cache']['keys'])) {
2014-02-24 11:42:13 +00:00
foreach ($entity->getFieldDefinitions() as $field_name => $definition) {
2014-05-02 21:43:59 +00:00
if ($definition->getType() === 'comment' && ($display_options = $display->getComponent($field_name))) {
2013-12-20 12:07:20 +00:00
$pager_id = $display_options['settings']['pager_id'];
2015-02-27 13:18:52 +00:00
$build['#cache']['contexts'][] = 'pager:' . $pager_id;
2013-12-20 12:07:20 +00:00
}
}
}
2014-05-02 21:43:59 +00:00
return $build;
2013-12-20 12:07:20 +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
*/
2014-02-28 10:06:00 +00:00
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
// http://drupal.org/node/1901110
2014-08-19 09:52:51 +00:00
$links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context);
$node_links += $links;
2004-07-08 19:58:10 +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
* Calculates the indentation level of each comment in a comment thread.
*
* This function loops over an array representing a comment thread. For each
* comment, the function calculates the indentation level and saves it in the
* 'divs' property of the comment object.
2009-07-28 10:09:25 +00:00
*
* @param array $comments
2012-01-03 04:36:15 +00:00
* An array of comment objects, keyed by comment ID.
2009-07-28 10:09:25 +00:00
*/
function comment_prepare_thread(&$comments) {
// A counter that helps track how indented we are.
$divs = 0;
2013-09-27 15:34:47 +00:00
foreach ($comments as $key => &$comment) {
2009-07-28 10:09:25 +00:00
// The $divs element instructs #prefix whether to add an indent div or
// close existing divs (a negative value).
2014-02-16 07:20:53 +00:00
$comment->depth = count(explode('.', $comment->getThread())) - 1;
2009-07-28 10:09:25 +00:00
if ($comment->depth > $divs) {
$comment->divs = 1;
$divs++;
}
else {
$comment->divs = $comment->depth - $divs;
while ($comment->depth < $divs) {
$divs--;
}
}
}
// The final comment must close up some hanging divs
$comments[$key]->divs_final = $divs;
}
/**
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.
2009-12-26 16:50:09 +00:00
* @param $view_mode
2013-09-27 15:34:47 +00:00
* (optional) View mode, e.g. 'full', 'teaser'... Defaults to 'full'.
2010-10-03 01:15:34 +00:00
* @param $langcode
* (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
2009-07-28 10:09:25 +00:00
* An array as expected by drupal_render().
*/
2013-09-27 15:34:47 +00:00
function comment_view(CommentInterface $comment, $view_mode = 'full', $langcode = NULL) {
2012-10-14 06:40:03 +00:00
return entity_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
*
* @param $comments
2013-10-09 05:43:02 +00:00
* An array of comments as returned by entity_load_multiple().
2009-12-26 16:50:09 +00:00
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
2010-10-03 01:15:34 +00:00
* @param $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
2009-07-28 10:09:25 +00:00
* An array in the format expected by drupal_render().
2012-01-03 04:36:15 +00:00
*
* @see drupal_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) {
return entity_view_multiple($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'))) {
2014-06-08 16:14:00 +00:00
// You cannot use comment fields on entity types with non-integer IDs.
2015-03-06 11:40:26 +00:00
unset($form['add']['new_storage_type']['#options'][t('General')]['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
/**
* Implements hook_form_FORM_ID_alter().
*/
2014-08-04 11:21:15 +00:00
function comment_form_field_ui_field_storage_edit_form_alter(&$form, FormStateInterface $form_state) {
2013-12-09 23:19:58 +00:00
if ($form['#field']->getType() == 'comment') {
2013-09-27 15:34:47 +00:00
// We only support posting one comment at the time so it doesn't make sense
// to let the site builder choose anything else.
2015-01-12 12:17:08 +00:00
$form['field_storage']['cardinality_container']['cardinality']['#default_value'] = 1;
$form['field_storage']['cardinality_container']['#access'] = FALSE;
2008-10-06 12:55:56 +00:00
}
}
2006-01-05 14:35:55 +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.
2014-10-01 14:11:14 +00:00
if (!\Drupal::entityManager()->getDefinition($entity_type)->isSubclassOf('Drupal\Core\Entity\FieldableEntityInterface')) {
2014-04-22 10:14:42 +00:00
return;
}
2013-09-27 15:34:47 +00:00
if (!\Drupal::service('comment.manager')->getFields($entity_type)) {
// 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();
2014-03-12 18:11:16 +00:00
entity_delete_multiple('comment', $cids);
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) {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$entity_type_id_key = $entity_type->getKey('id');
2014-06-18 20:10:59 +00:00
if ($entity_type_id_key === FALSE) {
return FALSE;
}
2014-06-08 16:14:00 +00:00
$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';
}
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
*/
2013-03-10 19:05:24 +00:00
function comment_node_update_index(EntityInterface $node, $langcode) {
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;
2014-03-27 11:54:40 +00:00
$roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple();
2013-06-24 16:38:32 +00:00
$authenticated_can_access = $roles[DRUPAL_AUTHENTICATED_RID]->hasPermission('access comments');
foreach ($roles as $rid => $role) {
if ($role->hasPermission('search content') && !$role->hasPermission('access comments')) {
if ($rid == DRUPAL_AUTHENTICATED_RID || $rid == DRUPAL_ANONYMOUS_RID || !$authenticated_can_access) {
$index_comments = FALSE;
break;
}
2010-06-08 06:34:36 +00:00
}
}
}
Issue #1825952 by Fabianx, joelpittet, bdragon, heddn, chx, xjm, pwolanin, mikey_p, ti2m, bfr, dags, cilefen, scor, mgifford: Turn on twig autoescape by default
2014-07-18 09:05:22 +00:00
$build = array();
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) {
$comments = \Drupal::entityManager()->getStorage('comment')
->loadThread($node, $field_name, $mode, $comments_per_page);
if ($comments) {
comment_prepare_thread($comments);
$build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
}
2013-09-27 15:34:47 +00:00
}
2010-06-08 06:34:36 +00:00
}
2008-10-06 12:55:56 +00:00
}
Issue #1825952 by Fabianx, joelpittet, bdragon, heddn, chx, xjm, pwolanin, mikey_p, ti2m, bfr, dags, cilefen, scor, mgifford: Turn on twig autoescape by default
2014-07-18 09:05:22 +00:00
return drupal_render($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) {
2015-01-10 13:56:47 +00:00
return array('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
*/
2009-01-22 12:46:07 +00:00
function comment_user_cancel($edit, $account, $method) {
2009-01-08 08:42:13 +00:00
switch ($method) {
case 'user_cancel_block_unpublish':
2013-07-11 17:29:02 +00:00
$comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id()));
2010-06-02 06:20:11 +00:00
foreach ($comments as $comment) {
2014-03-30 12:55:20 +00:00
$comment->setPublished(CommentInterface::NOT_PUBLISHED);
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 */
2013-07-11 17:29:02 +00:00
$comments = entity_load_multiple_by_properties('comment', array('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);
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();
2011-12-05 12:12:15 +00:00
entity_delete_multiple('comment', $cids);
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
* An array as expected by drupal_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) {
2012-08-16 11:30:43 +00:00
$preview_build = array();
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()) {
2007-03-01 19:53:04 +00:00
// Attach the user and time information.
2014-02-16 07:20:53 +00:00
$author_name = $comment->getAuthorName();
if (!empty($author_name)) {
$account = user_load_by_name($author_name);
2007-03-01 19:53:04 +00:00
}
2013-12-11 06:58:02 +00:00
elseif (\Drupal::currentUser()->isAuthenticated() && empty($comment->is_anonymous)) {
$account = \Drupal::currentUser();
2007-03-01 19:53:04 +00:00
}
2008-05-14 13:12:41 +00:00
2013-10-15 04:56:14 +00:00
if (!empty($account) && $account->isAuthenticated()) {
2014-02-03 16:02:31 +00:00
$comment->setOwner($account);
2014-06-13 02:33:48 +00:00
$comment->setAuthorName(String::checkPlain($account->getUsername()));
2007-03-30 07:45:20 +00:00
}
2014-02-16 07:20:53 +00:00
elseif (empty($author_name)) {
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
2007-03-01 19:53:04 +00:00
}
2008-05-14 13:12:41 +00:00
2014-02-16 07:20:53 +00:00
$created_time = !is_null($comment->getCreatedTime()) ? $comment->getCreatedTime() : REQUEST_TIME;
$comment->setCreatedTime($created_time);
2013-01-14 10:59:47 +00:00
$comment->changed->value = REQUEST_TIME;
2009-07-28 10:09:25 +00:00
$comment->in_preview = TRUE;
2012-10-14 06:40:03 +00:00
$comment_build = 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()) {
2009-07-31 19:44:21 +00:00
$build = array();
2014-02-16 07:20:53 +00:00
$parent = $comment->getParentComment();
if ($parent && $parent->isPublished()) {
2013-09-27 15:34:47 +00:00
$build = 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;
2013-09-27 15:34:47 +00:00
$build = entity_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
}
}
2013-01-14 10:59:47 +00:00
/**
* Prepares a user account object for rendering comment authors.
*
* This helper handles anonymous authors in addition to registered comment
* authors.
*
2013-09-27 15:34:47 +00:00
* @param \Drupal\comment\CommentInterface $comment
* The comment to which the author replied.
*
2013-12-22 21:11:36 +00:00
* @return \Drupal\user\UserInterface
2013-01-14 10:59:47 +00:00
* A user account, for use with theme_username() or the user_picture template.
*/
2013-09-27 15:34:47 +00:00
function comment_prepare_author(CommentInterface $comment) {
2014-05-02 21:43:59 +00:00
// The account has been pre-loaded by CommentViewBuilder::buildComponents().
2014-02-03 16:02:31 +00:00
$account = $comment->getOwner();
2013-07-05 18:43:36 +00:00
if (empty($account->uid->value)) {
2014-04-16 20:57:51 +00:00
// @todo Avoid creating a new entity by just creating a new instance
// directly, see https://drupal.org/node/1867228.
2014-02-16 07:20:53 +00:00
$account = entity_create('user', array('uid' => 0, 'name' => $comment->getAuthorName(), 'homepage' => $comment->getHomepage()));
2014-04-16 20:57:51 +00:00
// The anonymous user is not a new account, do not treat it as one.
$account->enforceIsNew(FALSE);
2013-01-14 10:59:47 +00:00
}
2013-07-29 07:52:14 +00:00
return $account;
2013-01-14 10:59:47 +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) {
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;
2012-12-11 19:00:31 +00:00
2013-01-14 10:59:47 +00:00
$account = comment_prepare_author($comment);
2013-06-29 13:43:09 +00:00
$username = array(
'#theme' => 'username',
'#account' => $account,
);
$variables['author'] = drupal_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();
$variables['created'] = format_date($comment->getCreatedTime());
2012-12-11 19:00:31 +00:00
// Avoid calling format_date() 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 {
2014-02-16 07:20:53 +00:00
$variables['changed'] = format_date($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')) {
2012-11-26 10:38:45 +00:00
// To change user picture settings (e.g., image style), edit the 'compact'
// view mode on the User entity.
2013-01-14 10:59:47 +00:00
$variables['user_picture'] = user_view($account, 'compact');
2012-11-26 10:38:45 +00:00
}
else {
$variables['user_picture'] = array();
}
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-06-08 21:25:15 +00:00
if (isset($variables['elements']['signature'])) {
2014-08-29 23:33:20 +00:00
$variables['signature'] = $variables['elements']['signature'];
2014-06-08 21:25:15 +00:00
unset($variables['elements']['signature']);
2013-01-14 10:59:47 +00:00
}
else {
$variables['signature'] = '';
}
2014-01-31 15:08:43 +00:00
if (isset($comment->in_preview)) {
2014-09-29 13:41:29 +00:00
$variables['title'] = \Drupal::l($comment->getSubject(), new Url('<front>'));
$variables['permalink'] = \Drupal::l(t('Permalink'), new Url('<front>'));
2014-01-31 15:08:43 +00:00
}
else {
$uri = $comment->urlInfo();
2014-03-28 23:07:00 +00:00
$attributes = $uri->getOption('attributes') ?: array();
$attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
$uri->setOption('attributes', $attributes);
2014-09-29 13:41:29 +00:00
$variables['title'] = \Drupal::l($comment->getSubject(), $uri);
2010-04-06 19:49:03 +00:00
2014-09-29 13:41:29 +00:00
$variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink());
2014-01-31 15:08:43 +00:00
}
2010-09-23 19:47:31 +00:00
2015-02-28 11:41:52 +00:00
$variables['submitted'] = t('Submitted by @username on @datetime', array('@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();
2013-05-13 19:27:58 +00:00
$account_parent = comment_prepare_author($comment_parent);
2012-10-27 19:41:47 +00:00
$variables['parent_comment'] = $comment_parent;
2013-06-29 13:43:09 +00:00
$username = array(
'#theme' => 'username',
'#account' => $account_parent,
);
$variables['parent_author'] = drupal_render($username);
2014-02-16 07:20:53 +00:00
$variables['parent_created'] = format_date($comment_parent->getCreatedTime());
2012-12-11 19:00:31 +00:00
// Avoid calling format_date() twice on the 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 {
2014-02-16 07:20:53 +00:00
$variables['parent_changed'] = format_date($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();
2014-03-28 23:07:00 +00:00
$attributes = $permalink_uri_parent->getOption('attributes') ?: array();
$attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
$permalink_uri_parent->setOption('attributes', $attributes);
2014-09-29 13:41:29 +00:00
$variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent);
$variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent);
2012-10-27 19:41:47 +00:00
$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'] = '';
}
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();
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
}