public function CommentPostFormatter::viewElements in Open Social 8.7
Same name and namespace in other branches
- 8.9 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8.2 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8.3 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8.4 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8.5 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8.6 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 8.8 modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 10.3.x modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 10.0.x modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 10.1.x modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
- 10.2.x modules/social_features/social_post/src/Plugin/Field/FieldFormatter/CommentPostFormatter.php \Drupal\social_post\Plugin\Field\FieldFormatter\CommentPostFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides CommentDefaultFormatter::viewElements
File
- modules/
social_features/ social_post/ src/ Plugin/ Field/ FieldFormatter/ CommentPostFormatter.php, line 44
Class
- CommentPostFormatter
- Provides a post comment formatter.
Namespace
Drupal\social_post\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$output = [];
$field_name = $this->fieldDefinition
->getName();
$entity = $items
->getEntity();
$status = $items->status;
$comments_per_page = $this
->getSetting('num_comments');
if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, [
'search_result',
'search_index',
])) {
$comment_settings = $this
->getFieldSettings();
$comment_count = $entity
->get($field_name)->comment_count;
// Only attempt to render comments if the entity has visible comments.
// Unpublished comments are not included in
// $entity->get($field_name)->comment_count, but unpublished comments
// should display if the user is an administrator.
$elements['#cache']['contexts'][] = 'user.permissions';
if ($this->currentUser
->hasPermission('access comments') || $this->currentUser
->hasPermission('administer comments')) {
$output['comments'] = [];
if ($comment_count || $this->currentUser
->hasPermission('administer comments')) {
$mode = $comment_settings['default_mode'];
$comments = $this
->loadThread($entity, $field_name, $mode, $comments_per_page, FALSE);
if ($comments) {
$build = $this->viewBuilder
->viewMultiple($comments);
$output['comments'] += $build;
}
if ($comments_per_page && $comment_count > $comments_per_page) {
$t_args = [
':num_comments' => $comment_count,
];
$more_link = $this
->t('Show all :num_comments comments', $t_args);
// Set link classes to be added to the button.
$more_link_options = [
'attributes' => [
'class' => [
'btn',
'btn-flat',
'brand-text-primary',
],
],
];
// Set path to post node.
$link_url = $entity
->urlInfo('canonical');
// Attach the attributes.
$link_url
->setOptions($more_link_options);
// Build the link.
$more_button = Link::fromTextAndUrl($more_link, $link_url);
$output['more_link'] = $more_button;
}
}
}
// Append comment form if the comments are open and the form is set to
// display below the entity. Do not show the form for the print view mode.
if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') {
// Only show the add comment form if the user has permission.
$elements['#cache']['contexts'][] = 'user';
$add_comment_form = FALSE;
// Check if the post has been posted in a group.
$group_id = $entity->field_recipient_group->target_id;
if ($group_id) {
/** @var \Drupal\group\Entity\Group $group */
$group = entity_load('group', $group_id);
if ($group
->hasPermission('add post entities in group', $this->currentUser) && $this->currentUser
->hasPermission('post comments')) {
$add_comment_form = TRUE;
}
}
elseif ($this->currentUser
->hasPermission('post comments')) {
$add_comment_form = TRUE;
}
if ($add_comment_form) {
$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' => [],
'more_link' => [],
];
}
return $elements;
}