You are here

public function RemoteImageWithMetadataFormatter::viewElements in Remote image 8

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 RemoteImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/RemoteImageWithMetadataFormatter.php, line 28
Contains \Drupal\remote_image\Plugin\Field\FieldFormatter\RemoteImageWithMetadataFormatter.

Class

RemoteImageWithMetadataFormatter
'remote_image' formatter which also displays the metadata like width/height.

Namespace

Drupal\remote_image\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  foreach ($items as $delta => $item) {
    $image = $elements[$delta];
    $element = [
      'image' => [
        '#prefix' => '<div class="field__label">' . $this
          ->t('Image') . '</div>',
        $image,
      ],
      'width' => [
        '#prefix' => '<div class="field__label">' . $this
          ->t('Width') . '</div>',
        '#markup' => $image['#width'],
      ],
      'height' => [
        '#prefix' => '<div class="field__label">' . $this
          ->t('Height') . '</div>',
        '#markup' => $image['#height'],
      ],
      'title' => [
        '#prefix' => '<div class="field__label">' . $this
          ->t('Title') . '</div>',
        '#markup' => $image['#title'],
      ],
    ];
    if (isset($image['#alt'])) {
      $element['alt'] = [
        '#prefix' => '<div class="field__label">' . $this
          ->t('Alt') . '</div>',
        '#markup' => $image['#alt'],
      ];
    }
    $elements[$delta] = $element;
  }
  return $elements;
}