You are here

public function CommentNodeFormatter::viewElements in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  2. 8 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  3. 8.2 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  4. 8.3 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  5. 8.4 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  6. 8.5 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  7. 8.6 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  8. 8.7 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  9. 10.3.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  10. 10.0.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  11. 10.1.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::viewElements()
  12. 10.2.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::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_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php, line 45

Class

CommentNodeFormatter
Provides a node comment formatter.

Namespace

Drupal\social_core\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $output = [];
  $field_name = $this->fieldDefinition
    ->getName();
  $entity = $items
    ->getEntity();
  $status = $items->status;
  $access_comments_in_group = FALSE;

  // Exclude entities without the set id.
  if (!empty($entity
    ->id())) {
    $group_contents = GroupContent::loadByEntity($entity);
  }
  if (!empty($group_contents)) {

    // Add cache contexts.
    $elements['#cache']['contexts'][] = 'group.type';
    $elements['#cache']['contexts'][] = 'user.group_permissions';
    $account = \Drupal::currentUser();
    $renderer = \Drupal::service('renderer');
    foreach ($group_contents as $group_content) {
      $group = $group_content
        ->getGroup();
      $membership = $group
        ->getMember($account);
      $renderer
        ->addCacheableDependency($elements, $membership);
      if ($group
        ->hasPermission('access comments', $account)) {
        $access_comments_in_group = TRUE;
      }
    }
  }
  $comments_per_page = $this
    ->getSetting('num_comments');
  if ($access_comments_in_group && $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;
        }
      }

      // Prepare the show all comments link.
      $t_args = [
        ':num_comments' => $comment_count,
      ];

      // Set link classes to be added to the button.
      $more_link_options = [
        'attributes' => [
          'class' => [
            'btn',
            'btn-flat',
            'brand-text-primary',
          ],
        ],
      ];

      // Set path to node.
      $link_url = $entity
        ->urlInfo('canonical');

      // Attach the attributes.
      $link_url
        ->setOptions($more_link_options);
      if ($comment_count == 0) {
        $more_link = $this
          ->t(':num_comments comments', $t_args);
        $output['more_link'] = $more_link;
      }
      elseif ($comment_count == 1) {
        $more_link = $this
          ->t(':num_comments comment', $t_args);
        $output['more_link'] = $more_link;
      }
      else {
        $more_link = $this
          ->t('Show all :num_comments comments', $t_args);
      }

      // Build the link.
      $more_button = Link::fromTextAndUrl($more_link, $link_url);
      $always_show_all_comments = $this
        ->getSetting('always_show_all_comments');
      if ($always_show_all_comments && $comment_count > 1) {
        $output['more_link'] = $more_button;
      }
      elseif ($comments_per_page && $comment_count > $comments_per_page) {
        $output['more_link'] = $more_button;
      }
    }
    $elements[] = $output + [
      '#comment_type' => $this
        ->getFieldSetting('comment_type'),
      '#comment_display_mode' => $this
        ->getFieldSetting('default_mode'),
      'comments' => [],
      'comment_form' => [],
      'more_link' => [],
    ];
  }
  return $elements;
}