You are here

public function CommentDefaultFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php \Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter::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 FormatterInterface::viewElements

File

core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php, line 136
Contains \Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter.

Class

CommentDefaultFormatter
Provides a default comment formatter.

Namespace

Drupal\comment\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $output = array();
  $field_name = $this->fieldDefinition
    ->getName();
  $entity = $items
    ->getEntity();
  $status = $items->status;
  if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array(
    'search_result',
    'search_index',
  ))) {
    $comment_settings = $this
      ->getFieldSettings();

    // 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 ($entity
        ->get($field_name)->comment_count || $this->currentUser
        ->hasPermission('administer comments')) {
        $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);
          $build['pager']['#type'] = 'pager';
          if ($this
            ->getSetting('pager_id')) {
            $build['pager']['#element'] = $this
              ->getSetting('pager_id');
          }
          $output['comments'] += $build;
        }
      }
    }

    // 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.roles';
      if ($this->currentUser
        ->hasPermission('post comments')) {
        $output['comment_form'] = [
          '#lazy_builder' => [
            'comment.lazy_builders:renderForm',
            [
              $entity
                ->getEntityTypeId(),
              $entity
                ->id(),
              $field_name,
              $this
                ->getFieldSetting('comment_type'),
            ],
          ],
          '#create_placeholder' => TRUE,
        ];
      }
    }
    $elements[] = $output + array(
      '#comment_type' => $this
        ->getFieldSetting('comment_type'),
      '#comment_display_mode' => $this
        ->getFieldSetting('default_mode'),
      'comments' => array(),
      'comment_form' => array(),
    );
  }
  return $elements;
}