You are here

public static function BlazyMedia::wrap in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/BlazyMedia.php \Drupal\blazy\BlazyMedia::wrap()

Returns a field to be wrapped by theme_container().

Currently Instagram, and SlideShare are known to use iframe, and thus can be converted into a responsive Blazy with fluid ratio. The rest are returned as is, only wrapped by .media wrapper for consistency with complex interaction like EB.

Parameters

array $field: The source renderable array $field.

Return value

array The new renderable array of the media item wrapped by theme_container().

2 calls to BlazyMedia::wrap()
BlazyFormatterTest::testBlazyMedia in tests/src/Kernel/BlazyFormatterTest.php
Tests the Blazy formatter faked Media integration.
BlazyMedia::build in src/BlazyMedia.php
Builds the media field which cannot be displayed using theme_blazy().

File

src/BlazyMedia.php, line 58

Class

BlazyMedia
Provides extra media utilities without dependencies on Media Entity, etc.

Namespace

Drupal\blazy

Code

public static function wrap(array $field = []) {

  // Media entity is a single being, reasonable to work with multi-value?
  $item = $field[0];
  $settings = isset($field['#settings']) ? $field['#settings'] : [];
  $iframe = isset($item['#tag']) && $item['#tag'] == 'iframe';
  $attributes = [];
  if (isset($item['#attributes'])) {
    $attributes =& $item['#attributes'];
  }

  // Converts iframes into lazyloaded ones.
  if ($iframe && !empty($attributes['src'])) {
    $attributes['data-src'] = $attributes['src'];
    $attributes['class'][] = 'b-lazy media__iframe media__element';
    $attributes['src'] = 'about:blank';
    $attributes['allowfullscreen'] = TRUE;
  }

  // Wraps the media item to allow consistency for EB/SB.
  $build = [
    '#theme' => 'container',
    '#children' => $item,
    '#attributes' => [
      'class' => [
        'media',
      ],
    ],
    '#settings' => $settings,
  ];
  if (!empty($settings['bundle'])) {
    $build['#attributes']['class'][] = 'media--' . str_replace('_', '-', $settings['bundle']);
  }

  // Adds helper for Entity Browser small thumbnail selection.
  if (!empty($settings['thumbnail_style']) && !empty($settings['uri'])) {
    $build['#attributes']['data-thumb'] = ImageStyle::load($settings['thumbnail_style'])
      ->buildUrl($settings['uri']);
  }

  // See comment above for known media entities using iframe.
  if ($iframe) {
    $build['#attributes']['class'][] = 'media--ratio';
    if (!empty($attributes['width']) && !empty($attributes['height'])) {
      $padding_bottom = round($attributes['height'] / $attributes['width'] * 100, 2);
      $build['#attributes']['style'] = 'padding-bottom: ' . $padding_bottom . '%';
    }
  }
  else {
    $build['#attributes']['class'][] = 'media--rendered';
  }

  // Clone relevant keys as field wrapper is no longer in use.
  foreach ([
    'attached',
    'cache',
  ] as $key) {
    if (isset($field["#{$key}"])) {
      $build["#{$key}"] = $field["#{$key}"];
    }
  }
  return $build;
}