You are here

public function Video::viewElements in Video Embed Field 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/Video.php \Drupal\video_embed_field\Plugin\Field\FieldFormatter\Video::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/Video.php, line 91

Class

Video
Plugin implementation of the video 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 {
      $autoplay = $this->currentUser
        ->hasPermission('never autoplay videos') ? FALSE : $this
        ->getSetting('autoplay');
      $element[$delta] = $provider
        ->renderEmbedCode($this
        ->getSetting('width'), $this
        ->getSetting('height'), $autoplay);
      $element[$delta]['#cache']['contexts'][] = 'user.permissions';
      $element[$delta] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            Html::cleanCssIdentifier(sprintf('video-embed-field-provider-%s', $provider
              ->getPluginId())),
          ],
        ],
        'children' => $element[$delta],
      ];

      // For responsive videos, wrap each field item in it's own container.
      if ($this
        ->getSetting('responsive')) {
        $element[$delta]['#attached']['library'][] = 'video_embed_field/responsive-video';
        $element[$delta]['#attributes']['class'][] = 'video-embed-field-responsive-video';
      }
    }
  }
  return $element;
}