CommentDefaultFormatter.php in Comment Permissions 8
File
src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
View source
<?php
namespace Drupal\comment_perm\Plugin\Field\FieldFormatter;
use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter as CommentDefaultFormatterBase;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment_perm\CommentAccessTrait;
use Drupal\Core\Field\FieldItemListInterface;
class CommentDefaultFormatter extends CommentDefaultFormatterBase {
use CommentAccessTrait;
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$output = [];
$field_name = $this->fieldDefinition
->getName();
$entity = $items
->getEntity();
$status = $items->status;
if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, [
'search_result',
'search_index',
])) {
$comment_settings = $this
->getFieldSettings();
$elements['#cache']['contexts'][] = 'user.permissions';
if ($this
->accessComment($this->currentUser, $comment_settings['comment_type']) || $this
->accessAdministerComment($this->currentUser, $comment_settings['comment_type'])) {
$output['comments'] = [];
if ($entity
->get($field_name)->comment_count || $this
->accessAdministerComment($this->currentUser, $comment_settings['comment_type'])) {
$mode = $comment_settings['default_mode'];
$comments_per_page = $comment_settings['per_page'];
$comments = $this->storage
->loadThread($entity, $field_name, $mode, $comments_per_page, $this
->getSetting('pager_id'));
if ($comments) {
$build = $this->viewBuilder
->viewMultiple($comments, $this
->getSetting('view_mode'));
$build['pager']['#type'] = 'pager';
$build['pager']['#route_name'] = $this->routeMatch
->getRouteObject();
$build['pager']['#route_parameters'] = $this->routeMatch
->getRawParameters()
->all();
if ($this
->getSetting('pager_id')) {
$build['pager']['#element'] = $this
->getSetting('pager_id');
}
$output['comments'] += $build;
}
}
}
if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') {
$elements['#cache']['contexts'][] = 'user.roles';
if ($this
->accessPostComment($this->currentUser, $comment_settings['comment_type'])) {
$output['comment_form'] = [
'#lazy_builder' => [
'comment.lazy_builders:renderForm',
[
$entity
->getEntityTypeId(),
$entity
->id(),
$field_name,
$this
->getFieldSetting('comment_type'),
],
],
'#create_placeholder' => TRUE,
];
}
}
$elements[] = $output + [
'#comment_type' => $this
->getFieldSetting('comment_type'),
'#comment_display_mode' => $this
->getFieldSetting('default_mode'),
'comments' => [],
'comment_form' => [],
];
}
return $elements;
}
}