comment.module
File
-
core/
modules/ comment/ comment.module
View source
<?php
/**
* @file
*/
use Drupal\comment\CommentInterface;
use Drupal\comment\Hook\CommentThemeHooks;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* The time cutoff for comments marked as read for entity types other node.
*
* Comments changed before this time are always marked as read.
* Comments changed after this time may be marked new, updated, or read,
* depending on their state for the current user. Defaults to 30 days ago.
*
* @todo Remove when https://www.drupal.org/node/2006632 lands.
*/
define('COMMENT_NEW_LIMIT', (int) $_SERVER['REQUEST_TIME'] - 30 * 24 * 60 * 60);
/**
* Entity URI callback.
*/
function comment_uri(CommentInterface $comment) {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use \\Drupal\\comment\\Entity\\Comment::permalink() instead. See https://www.drupal.org/node/3384294', E_USER_DEPRECATED);
return $comment->permalink();
}
/**
* 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::entityTypeManager()->getDefinition($entity_type_id);
$entity_type_id_key = $entity_type->getKey('id');
if ($entity_type_id_key === FALSE) {
return FALSE;
}
$field_definitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type->id());
$entity_type_id_definition = $field_definitions[$entity_type_id_key];
return $entity_type_id_definition->getType() === 'integer';
}
/**
* Generates a comment preview.
*
* @param \Drupal\comment\CommentInterface $comment
* The comment entity to preview.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* An array as expected by \Drupal\Core\Render\RendererInterface::render().
*/
function comment_preview(CommentInterface $comment, FormStateInterface $form_state) : array {
$preview_build = [];
$entity = $comment->getCommentedEntity();
if (!$form_state->getErrors()) {
$comment->in_preview = TRUE;
$comment_build = \Drupal::entityTypeManager()->getViewBuilder('comment')
->view($comment);
$comment_build['#weight'] = -100;
$preview_build['comment_preview'] = $comment_build;
}
if ($comment->hasParentComment()) {
$build = [];
$parent = $comment->getParentComment();
if ($parent && $parent->isPublished()) {
$build = \Drupal::entityTypeManager()->getViewBuilder('comment')
->view($parent);
}
}
else {
// The comment field output includes rendering the parent entity of the
// thread to which the comment is a reply. The rendered entity output
// includes the comment reply form, which contains the comment preview and
// therefore the rendered parent entity. This results in an infinite loop of
// parent entity output rendering the comment form and the comment form
// rendering the parent entity. To prevent this infinite loop we temporarily
// set the value of the comment field on a clone of the entity to hidden
// before calling the entity view builder. That way when the output of
// the commented entity is rendered, it excludes the comment field output.
$field_name = $comment->getFieldName();
$entity = clone $entity;
$entity->{$field_name}->status = CommentItemInterface::HIDDEN;
$build = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())
->view($entity, 'full');
}
$preview_build['comment_output_below'] = $build;
$preview_build['comment_output_below']['#weight'] = 200;
return $preview_build;
}
/**
* Prepares variables for comment templates.
*
* By default this function performs special preprocessing of some base fields
* so they are available as variables in the template. For example 'subject'
* appears as 'title'. This preprocessing is skipped if:
* - a module makes the field's display configurable via the field UI by means
* of BaseFieldDefinition::setDisplayConfigurable()
* - AND the additional entity type property
* 'enable_base_field_custom_preprocess_skipping' has been set using
* hook_entity_type_build().
*
* Default template: comment.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the comment and entity objects.
* Array keys: #comment, #commented_entity.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_comment(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(CommentThemeHooks::class)->preprocessComment($variables);
}
Functions
| Title | Deprecated | Summary |
|---|---|---|
| comment_preview | Generates a comment preview. | |
| comment_uri | Entity URI callback. | |
| template_preprocess_comment | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for comment templates. |
| _comment_entity_uses_integer_id | Determines if an entity type is using an integer-based ID definition. |
Constants
| Title | Deprecated | Summary |
|---|---|---|
| COMMENT_NEW_LIMIT | The time cutoff for comments marked as read for entity types other node. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.