You are here

public function VideoEmbedField::viewElements in Magnific Popup 8

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

Class

VideoEmbedField
Magnific Popup FieldFormatter for Video Embed Field.

Namespace

Drupal\magnific_popup\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $gallery_type = $this
    ->getSetting('gallery_type');
  $thumbnails = $this->thumbnailFormatter
    ->viewElements($items, $langcode);
  $videos = $this->videoFormatter
    ->viewElements($items, $langcode);
  foreach ($items as $delta => $item) {
    if ($gallery_type === 'first_item' && $delta > 0) {
      $element[$delta] = [
        '#type' => 'container',
        '#attributes' => [
          'data-mfp-video-embed' => (string) $this->renderer
            ->renderPlain($videos[$delta]),
          'class' => [
            'mfp-video-embed-popup',
          ],
        ],
        '#attached' => [
          'library' => [
            'magnific_popup/magnific_popup',
            'magnific_popup/video_embed_field',
          ],
        ],
      ];
    }
    else {
      $element[$delta] = [
        '#type' => 'container',
        '#attributes' => [
          'data-mfp-video-embed' => (string) $this->renderer
            ->renderPlain($videos[$delta]),
          'class' => [
            'mfp-video-embed-popup',
          ],
        ],
        '#attached' => [
          'library' => [
            'magnific_popup/magnific_popup',
            'magnific_popup/video_embed_field',
          ],
        ],
        'children' => $thumbnails[$delta],
      ];
    }
  }
  return $element;
}