You are here

public static function BlazyMedia::wrap in Blazy 8.2

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

Returns a field item/ content to be wrapped by theme_blazy().

@todo make it non-static method.

Parameters

array $field: The source renderable array $field.

Return value

array The renderable array of the media item to be wrapped by theme_blazy().

Overrides BlazyMediaInterface::wrap

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 is not understood by theme_blazy().

File

src/BlazyMedia.php, line 40

Class

BlazyMedia
Provides extra utilities to work with core Media.

Namespace

Drupal\blazy

Code

public static function wrap(array $field = []) {
  $item = $field[0];
  $settings = $field['#settings'];
  $iframe = isset($item['#tag']) && $item['#tag'] == 'iframe';
  $attributes = [];
  if (isset($item['#attributes'])) {
    $attributes =& $item['#attributes'];
  }

  // Update iframe/video dimensions based on configurable image style, if any.
  foreach ([
    'width',
    'height',
  ] as $key) {
    if (!empty($settings[$key])) {
      $attributes[$key] = $settings[$key];
    }
  }

  // Converts iframes into lazyloaded ones.
  // Iframes: Googledocs, SlideShare. Hardcoded: Soundcloud, Spotify.
  if ($iframe && !empty($attributes['src'])) {
    $settings['embed_url'] = $attributes['src'];
    $attributes = NestedArray::mergeDeep($attributes, Blazy::iframeAttributes($settings));
  }
  elseif (isset($item['#files'], $item['#files'][0]['file'])) {
    self::videoItem($item, $settings);
  }

  // Clone relevant keys since field wrapper is no longer in use.
  foreach ([
    'attached',
    'cache',
    'third_party_settings',
  ] as $key) {
    if (!empty($field["#{$key}"])) {
      $item["#{$key}"] = isset($item["#{$key}"]) ? NestedArray::mergeDeep($field["#{$key}"], $item["#{$key}"]) : $field["#{$key}"];
    }
  }

  // Keep original formatter configurations intact here for custom works.
  $item['#settings'] = new BlazySettings(array_filter($settings));
  return $item;
}