You are here

public function FileAndImageTableFormatter::viewElements in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment_upload/src/Plugin/Field/FieldFormatter/FileAndImageTableFormatter.php \Drupal\social_comment_upload\Plugin\Field\FieldFormatter\FileAndImageTableFormatter::viewElements()
  2. 8.5 modules/social_features/social_comment_upload/src/Plugin/Field/FieldFormatter/FileAndImageTableFormatter.php \Drupal\social_comment_upload\Plugin\Field\FieldFormatter\FileAndImageTableFormatter::viewElements()
  3. 8.6 modules/social_features/social_comment_upload/src/Plugin/Field/FieldFormatter/FileAndImageTableFormatter.php \Drupal\social_comment_upload\Plugin\Field\FieldFormatter\FileAndImageTableFormatter::viewElements()
  4. 8.8 modules/social_features/social_comment_upload/src/Plugin/Field/FieldFormatter/FileAndImageTableFormatter.php \Drupal\social_comment_upload\Plugin\Field\FieldFormatter\FileAndImageTableFormatter::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 ImageFormatter::viewElements

File

modules/social_features/social_comment_upload/src/Plugin/Field/FieldFormatter/FileAndImageTableFormatter.php, line 25

Class

FileAndImageTableFormatter
Plugin implementation of the 'file_image_default' formatter.

Namespace

Drupal\social_comment_upload\Plugin\Field\FieldFormatter

Code

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

  // Grab elements from the ImageFormatter and see if we can attach files?
  $elements = parent::viewElements($items, $langcode);
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    $item = $file->_referringItem;

    // If it's a File we render it as a file_link.
    if (!$this
      ->isImage($file)) {
      $elements[$delta] = [
        '#theme' => 'file_link',
        '#file' => $file,
        '#cache' => [
          'tags' => $file
            ->getCacheTags(),
        ],
      ];

      // Pass field item attributes to the theme function.
      if (isset($item->_attributes)) {
        $elements[$delta] += [
          '#attributes' => [],
        ];
        $elements[$delta]['#attributes'] += $item->_attributes;

        // Unset field item attributes since they have been included in the
        // formatter output and should not be rendered in the field template.
        unset($item->_attributes);
      }
    }
    else {

      // If it's an image we need to add the image width and height
      // so Photoswipe knows what to render.
      $image = $this
        ->getImage($file);
      $elements[$delta]['#item']->height = $image
        ->getHeight();
      $elements[$delta]['#item']->width = $image
        ->getWidth();
      $elements[$delta]['#item']->is_image = TRUE;
    }
  }
  return $elements;
}