You are here

public function VideoEmbedThumbnailFormatter::viewElements in Video 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/VideoEmbedThumbnailFormatter.php \Drupal\video\Plugin\Field\FieldFormatter\VideoEmbedThumbnailFormatter::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/VideoEmbedThumbnailFormatter.php, line 49

Class

VideoEmbedThumbnailFormatter
Plugin implementation of the thumbnail field formatter.

Namespace

Drupal\video\Plugin\Field\FieldFormatter

Code

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

  // load widget settings
  $field_definition = $this->fieldDefinition;
  $entity_form_display = entity_get_form_display($field_definition
    ->getTargetEntityTypeId(), $field_definition
    ->getTargetBundle(), 'default');
  $widget = $entity_form_display
    ->getRenderer($field_definition
    ->getName());
  $widget_settings = $widget
    ->getSettings();
  $element = [];
  foreach ($items as $delta => $item) {
    $file = File::load($item->target_id);
    $metadata = isset($item->data) ? unserialize($item->data) : [];
    $scheme = file_uri_scheme($file
      ->getFileUri());
    $provider = $this->providerManager
      ->loadProviderFromStream($scheme, $file, $metadata, $widget_settings);
    $url = FALSE;
    if ($this
      ->getSetting('link_image_to') == static::LINK_CONTENT) {
      $url = $items
        ->getEntity()
        ->urlInfo();
    }
    elseif ($this
      ->getSetting('link_image_to') == static::LINK_PROVIDER) {
      $url = Url::fromUri(file_create_url($file
        ->getFileUri()));
    }
    $element[$delta] = $provider
      ->renderThumbnail($this
      ->getSetting('image_style'), $url);
  }
  return $element;
}