class CommentViewBuilder
Same name in other branches
- 9 core/modules/comment/src/CommentViewBuilder.php \Drupal\comment\CommentViewBuilder
- 8.9.x core/modules/comment/src/CommentViewBuilder.php \Drupal\comment\CommentViewBuilder
- 11.x core/modules/comment/src/CommentViewBuilder.php \Drupal\comment\CommentViewBuilder
View builder handler for comments.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait
- class \Drupal\Core\Entity\EntityViewBuilder extends \Drupal\Core\Entity\EntityHandlerBase implements \Drupal\Core\Entity\EntityHandlerInterface, \Drupal\Core\Entity\EntityViewBuilderInterface, \Drupal\Core\Security\TrustedCallbackInterface
- class \Drupal\comment\CommentViewBuilder extends \Drupal\Core\Entity\EntityViewBuilder
- class \Drupal\Core\Entity\EntityViewBuilder extends \Drupal\Core\Entity\EntityHandlerBase implements \Drupal\Core\Entity\EntityHandlerInterface, \Drupal\Core\Entity\EntityViewBuilderInterface, \Drupal\Core\Security\TrustedCallbackInterface
Expanded class hierarchy of CommentViewBuilder
File
-
core/
modules/ comment/ src/ CommentViewBuilder.php, line 20
Namespace
Drupal\commentView source
class CommentViewBuilder extends EntityViewBuilder {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new CommentViewBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme registry.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, AccountInterface $current_user, Registry $theme_registry, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container->get('entity.repository'), $container->get('language_manager'), $container->get('current_user'), $container->get('theme.registry'), $container->get('entity_display.repository'), $container->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
/** @var \Drupal\comment\CommentInterface $entity */
// Store a threading field setting to use later in self::buildComponents().
$commented_entity = $entity->getCommentedEntity();
$build['#comment_threaded'] = is_null($commented_entity) || $commented_entity->getFieldDefinition($entity->getFieldName())
->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED;
// If threading is enabled, don't render cache individual comments, but do
// keep the cacheability metadata, so it can bubble up.
if ($build['#comment_threaded']) {
unset($build['#cache']['keys']);
}
return $build;
}
/**
* {@inheritdoc}
*
* In addition to modifying the content key on entities, this implementation
* will also set the comment entity key which all comments carry.
*
* @throws \InvalidArgumentException
* Thrown when a comment is attached to an entity that no longer exists.
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
/** @var \Drupal\comment\CommentInterface[] $entities */
if (empty($entities)) {
return;
}
// Pre-load associated users into cache to leverage multiple loading.
$uids = [];
foreach ($entities as $entity) {
$uids[] = $entity->getOwnerId();
}
$this->entityTypeManager
->getStorage('user')
->loadMultiple(array_unique($uids));
parent::buildComponents($build, $entities, $displays, $view_mode);
// A counter to track the indentation level.
$current_indent = 0;
$attach_history = $this->moduleHandler
->moduleExists('history') && $this->currentUser
->isAuthenticated();
foreach ($entities as $id => $entity) {
if ($build[$id]['#comment_threaded']) {
$comment_indent = count(explode('.', (string) $entity->getThread())) - 1;
if ($comment_indent > $current_indent) {
// Set 1 to indent this comment from the previous one (its parent).
// Set only one extra level of indenting even if the difference in
// depth is higher.
$build[$id]['#comment_indent'] = 1;
$current_indent++;
}
else {
// Set zero if this comment is on the same level as the previous one
// or negative value to point an amount indents to close.
$build[$id]['#comment_indent'] = $comment_indent - $current_indent;
$current_indent = $comment_indent;
}
}
// Commented entities already loaded after self::getBuildDefaults().
$commented_entity = $entity->getCommentedEntity();
// Set defaults if the commented_entity does not exist.
$bundle = $commented_entity ? $commented_entity->bundle() : '';
$is_node = $commented_entity ? $commented_entity->getEntityTypeId() === 'node' : NULL;
$build[$id]['#entity'] = $entity;
$build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $bundle;
$display = $displays[$entity->bundle()];
if ($display->getComponent('links')) {
$build[$id]['links'] = [
'#lazy_builder' => [
'comment.lazy_builders:renderLinks',
[
$entity->id(),
$view_mode,
$entity->language()
->getId(),
!empty($entity->in_preview),
],
],
'#create_placeholder' => TRUE,
];
}
if (!isset($build[$id]['#attached'])) {
$build[$id]['#attached'] = [];
}
$build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer';
if ($attach_history && $is_node) {
$build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator';
// Embed the metadata for the comment "new" indicators on this node.
$build[$id]['history'] = [
'#lazy_builder' => [
'\\Drupal\\history\\HistoryRenderCallback::lazyBuilder',
[
$commented_entity->id(),
],
],
'#create_placeholder' => TRUE,
];
}
}
if ($build[$id]['#comment_threaded']) {
// The final comment must close up some hanging divs.
$build[$id]['#comment_indent_final'] = $current_indent;
}
}
/**
* {@inheritdoc}
*/
protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode) {
parent::alterBuild($build, $comment, $display, $view_mode);
if (empty($comment->in_preview)) {
$prefix = '';
// Add indentation div or close open divs as needed.
if ($build['#comment_threaded']) {
$prefix .= $build['#comment_indent'] <= 0 ? str_repeat('</div>', abs($build['#comment_indent'])) : "\n" . '<div class="indented">';
}
$build['#prefix'] = $prefix;
// Close all open divs.
if (!empty($build['#comment_indent_final'])) {
$build['#suffix'] = str_repeat('</div>', $build['#comment_indent_final']);
}
}
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
CommentViewBuilder::$currentUser | protected | property | The current user. | |||
CommentViewBuilder::$entityTypeManager | protected | property | The entity type manager. | |||
CommentViewBuilder::alterBuild | protected | function | Overrides EntityViewBuilder::alterBuild | |||
CommentViewBuilder::buildComponents | public | function | In addition to modifying the content key on entities, this implementation will also set the comment entity key which all comments carry. |
Overrides EntityViewBuilder::buildComponents | ||
CommentViewBuilder::createInstance | public static | function | Overrides EntityViewBuilder::createInstance | |||
CommentViewBuilder::getBuildDefaults | protected | function | Overrides EntityViewBuilder::getBuildDefaults | |||
CommentViewBuilder::__construct | public | function | Constructs a new CommentViewBuilder. | Overrides EntityViewBuilder::__construct | ||
DependencySerializationTrait::$_entityStorages | protected | property | ||||
DependencySerializationTrait::$_serviceIds | protected | property | ||||
DependencySerializationTrait::__sleep | public | function | 1 | |||
DependencySerializationTrait::__wakeup | public | function | 2 | |||
EntityHandlerBase::$moduleHandler | protected | property | The module handler to invoke hooks on. | 5 | ||
EntityHandlerBase::moduleHandler | protected | function | Gets the module handler. | 5 | ||
EntityHandlerBase::setModuleHandler | public | function | Sets the module handler for this handler. | |||
EntityViewBuilder::$cacheBin | protected | property | The cache bin used to store the render cache. | |||
EntityViewBuilder::$entityDisplayRepository | protected | property | The entity display repository. | |||
EntityViewBuilder::$entityRepository | protected | property | The entity repository service. | |||
EntityViewBuilder::$entityType | protected | property | Information about the entity type. | |||
EntityViewBuilder::$entityTypeId | protected | property | The type of entities for which this view builder is instantiated. | |||
EntityViewBuilder::$languageManager | protected | property | The language manager. | |||
EntityViewBuilder::$singleFieldDisplays | protected | property | The EntityViewDisplay objects created for individual field rendering. | |||
EntityViewBuilder::$themeRegistry | protected | property | The theme registry. | |||
EntityViewBuilder::addContextualLinks | protected | function | Add contextual links. | |||
EntityViewBuilder::build | public | function | Builds an entity's view; augments entity defaults. | |||
EntityViewBuilder::buildMultiple | public | function | Builds multiple entities' views; augments entity defaults. | |||
EntityViewBuilder::getCacheTags | public | function | Overrides EntityViewBuilderInterface::getCacheTags | |||
EntityViewBuilder::getSingleFieldDisplay | protected | function | Gets an EntityViewDisplay for rendering an individual field. | |||
EntityViewBuilder::isViewModeCacheable | protected | function | Determines whether the view mode is cacheable. | |||
EntityViewBuilder::resetCache | public | function | Overrides EntityViewBuilderInterface::resetCache | |||
EntityViewBuilder::trustedCallbacks | public static | function | Overrides TrustedCallbackInterface::trustedCallbacks | 2 | ||
EntityViewBuilder::view | public | function | Overrides EntityViewBuilderInterface::view | 4 | ||
EntityViewBuilder::viewField | public | function | Overrides EntityViewBuilderInterface::viewField | |||
EntityViewBuilder::viewFieldItem | public | function | Overrides EntityViewBuilderInterface::viewFieldItem | |||
EntityViewBuilder::viewMultiple | public | function | Overrides EntityViewBuilderInterface::viewMultiple | 4 | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | ||
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | ||
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | |||
TrustedCallbackInterface::THROW_EXCEPTION | constant | Untrusted callbacks throw exceptions. | ||||
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION | constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||||
TrustedCallbackInterface::TRIGGER_WARNING | Deprecated | constant | Untrusted callbacks trigger E_USER_WARNING errors. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.