You are here

public function SocialLazyCommentDefaultFormatter::viewElements in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/src/Plugin/Field/FieldFormatter/SocialLazyCommentDefaultFormatter.php \Drupal\social_comment\Plugin\Field\FieldFormatter\SocialLazyCommentDefaultFormatter::viewElements()
  2. 10.3.x modules/social_features/social_comment/src/Plugin/Field/FieldFormatter/SocialLazyCommentDefaultFormatter.php \Drupal\social_comment\Plugin\Field\FieldFormatter\SocialLazyCommentDefaultFormatter::viewElements()
  3. 10.1.x modules/social_features/social_comment/src/Plugin/Field/FieldFormatter/SocialLazyCommentDefaultFormatter.php \Drupal\social_comment\Plugin\Field\FieldFormatter\SocialLazyCommentDefaultFormatter::viewElements()
  4. 10.2.x modules/social_features/social_comment/src/Plugin/Field/FieldFormatter/SocialLazyCommentDefaultFormatter.php \Drupal\social_comment\Plugin\Field\FieldFormatter\SocialLazyCommentDefaultFormatter::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_comment/src/Plugin/Field/FieldFormatter/SocialLazyCommentDefaultFormatter.php, line 28

Class

SocialLazyCommentDefaultFormatter
Provides a default comment formatter.

Namespace

Drupal\social_comment\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  // Elements from core comment formatter.
  $elements = parent::viewElements($items, $langcode);

  // Check if comments exists.
  if (!empty($elements[0]['comments'])) {
    $comment_settings = $this
      ->getFieldSettings();

    // Replace comments with lazy builder.
    $elements[0]['comments'] = [
      '#lazy_builder' => [
        'social_comment.lazy_renderer:renderComments',
        [
          $items
            ->getEntity()
            ->getEntityTypeId(),
          $items
            ->getEntity()
            ->id(),
          $comment_settings['default_mode'],
          $items
            ->getName(),
          $comment_settings['per_page'],
          $this
            ->getSetting('pager_id'),
        ],
      ],
      '#create_placeholder' => TRUE,
    ];
  }
  return $elements;
}