You are here

public function Thumbnail::viewElements in Video Embed Field 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/Thumbnail.php \Drupal\video_embed_field\Plugin\Field\FieldFormatter\Thumbnail::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

src/Plugin/Field/FieldFormatter/Thumbnail.php, line 99

Class

Thumbnail
Plugin implementation of the thumbnail field formatter.

Namespace

Drupal\video_embed_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  foreach ($items as $delta => $item) {
    $provider = $this->providerManager
      ->loadProviderFromInput($item->value);
    if (!$provider) {
      $element[$delta] = [
        '#theme' => 'video_embed_field_missing_provider',
      ];
    }
    else {
      $url = FALSE;
      if ($this
        ->getSetting('link_image_to') == static::LINK_CONTENT) {
        $url = $items
          ->getEntity()
          ->toUrl();
      }
      elseif ($this
        ->getSetting('link_image_to') == static::LINK_PROVIDER) {
        $url = Url::fromUri($item->value);
      }
      $provider
        ->downloadThumbnail();
      $element[$delta] = $provider
        ->renderThumbnail($this
        ->getSetting('image_style'), $url);
    }
  }
  return $element;
}