You are here

public function RemoteImageFormatter::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 FormatterInterface::viewElements

1 call to RemoteImageFormatter::viewElements()
RemoteImageWithMetadataFormatter::viewElements in src/Plugin/Field/FieldFormatter/RemoteImageWithMetadataFormatter.php
Builds a renderable array for a field value.
1 method overrides RemoteImageFormatter::viewElements()
RemoteImageWithMetadataFormatter::viewElements in src/Plugin/Field/FieldFormatter/RemoteImageWithMetadataFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/RemoteImageFormatter.php, line 32
Contains Drupal\remote_image\Plugin\Field\FieldFormatter\RemoteImageFormatter.

Class

RemoteImageFormatter
Plugin implementation of the 'remote_image' formatter.

Namespace

Drupal\remote_image\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  // Add one image per item.
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#theme' => 'image',
      '#uri' => $item->uri,
      '#width' => $item->width,
      '#height' => $item->height,
      '#attributes' => [
        'class' => [
          'remote-image-item',
        ],
      ],
    ];

    // Set the title field.
    if ($this->fieldDefinition
      ->getSetting('title_field') === 1) {
      $elements[$delta]['#title'] = $item->title;
    }

    // Set the alt field.
    if ($this->fieldDefinition
      ->getSetting('alt_field') === 1) {
      $elements[$delta]['#alt'] = $item->title;
    }
  }
  return $elements;
}