You are here

public function Colorbox::viewElements in Video Embed Field 8.2

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

Class

Colorbox
Plugin implementation of the thumbnail field formatter.

Namespace

Drupal\video_embed_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $thumbnails = $this->thumbnailFormatter
    ->viewElements($items, $langcode);
  $videos = $this->videoFormatter
    ->viewElements($items, $langcode);
  foreach ($items as $delta => $item) {

    // Support responsive videos within the colorbox modal.
    if ($this
      ->getSetting('responsive')) {
      $videos[$delta]['#attributes']['class'][] = 'video-embed-field-responsive-modal';
      $videos[$delta]['#attributes']['style'] = sprintf('width:%dpx;', $this
        ->getSetting('modal_max_width'));
    }
    $element[$delta] = [
      '#type' => 'container',
      '#attributes' => [
        'data-video-embed-field-modal' => (string) $this->renderer
          ->render($videos[$delta]),
        'class' => [
          'video-embed-field-launch-modal',
        ],
      ],
      '#attached' => [
        'library' => [
          'video_embed_field/colorbox',
          'video_embed_field/responsive-video',
        ],
      ],
      // Ensure the cache context from the video formatter which was rendered
      // early still exists in the renderable array for this formatter.
      '#cache' => [
        'contexts' => [
          'user.permissions',
        ],
      ],
      'children' => $thumbnails[$delta],
    ];
  }
  $this->colorboxAttachment
    ->attach($element);
  return $element;
}