You are here

public function VideoEmbedField::viewElements in Magnific Popup 8.2

Same name and namespace in other branches
  1. 8 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 150

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) {
    $element[$delta] = [
      '#type' => 'container',
      '#attributes' => [
        'data-mfp-src-markup' => (string) $this->renderer
          ->renderPlain($videos[$delta]),
        'class' => [
          'mfp-video-embed-popup',
        ],
      ],
      '#attached' => [
        'library' => [
          'magnific_popup/magnific_popup',
          'magnific_popup/video_embed_field',
        ],
      ],
    ];
    $display_item = $gallery_type !== 'first_item' || $delta === 0;
    if ($display_item) {
      $element[$delta]['#type'] = 'link';
      $element[$delta]['#title'] = $this->renderer
        ->render($thumbnails[$delta]);
      $element[$delta]['#url'] = Url::fromURI($videos[$delta]['children']['#url']);
    }
  }
  return $element;
}