You are here

protected function BlazyManager::prepareImage in Blazy 7

Prepares the Blazy image as a structured array ready for ::renderer().

Parameters

array $element: The renderable array being modified.

array $build: The array of information containing the required Image or File item object, settings, optional container attributes.

1 call to BlazyManager::prepareImage()
BlazyManager::preRender in src/BlazyManager.php
Builds the Blazy as a structured array ready for ::renderer().

File

src/BlazyManager.php, line 145

Class

BlazyManager
Implements a public facing blazy manager.

Namespace

Drupal\blazy

Code

protected function prepareImage(array &$element, array $build) {
  $item = $build['item'];
  $settings =& $build['settings'];

  // Decides if to use image or video, or both.
  $settings['_api'] = TRUE;
  $settings['ratio'] = $settings['ratio'] ? str_replace(':', '', $settings['ratio']) : FALSE;
  $settings['ratio'] = $settings['background'] && empty($settings['ratio']) ? 'fluid' : $settings['ratio'];
  $settings['use_media'] = $settings['embed_url'] && in_array($settings['type'], [
    'audio',
    'video',
  ]) && empty($settings['lightbox']);
  foreach (BlazyDefault::themeAttributes() as $key) {
    $key = $key . '_attributes';
    $build[$key] = isset($build[$key]) ? $build[$key] : [];
  }

  // Blazy has these 3 attributes, yet provides optional ones far below.
  // Sanitize potential user-defined attributes such as from BlazyFilter.
  // Skip attributes via $item, or by module, as they are not user-defined.
  $attributes =& $build['attributes'];

  // Build thumbnail and optional placeholder based on thumbnail.
  $this
    ->thumbnailAndPlaceholder($attributes, $settings);

  // Prepare image URL and its dimensions.
  Blazy::urlAndDimensions($settings, $item);

  // Build out the media.
  $this
    ->buildMedia($element, $build);

  // Provides extra attributes as needed, excluding url, item, done above.
  foreach ([
    'caption',
    'media',
    'wrapper',
  ] as $key) {
    $element["#{$key}" . '_attributes'] = empty($build[$key . '_attributes']) ? [] : Blazy::sanitize($build[$key . '_attributes']);
  }
  $captions = empty($build['captions']) ? [] : $this
    ->buildCaption($build['captions'], $settings);
  $element['#caption_attributes']['class'][] = $settings['item_id'] . '__caption';

  // Provides data for the renderable elements.
  $element['#captions'] = $captions;
  $element['#attributes'] = $attributes;
  $element['#postscript'] = $build['postscript'];
  $element['#url_attributes'] = $build['url_attributes'];
  $element['#settings'] = $settings;
}