You are here

protected function BlazyManager::prepareBlazy in Blazy 8.2

Prepares the Blazy output 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::prepareBlazy()
BlazyManager::preRenderBlazy in src/BlazyManager.php
Builds the Blazy image as a structured array ready for ::renderer().

File

src/BlazyManager.php, line 106

Class

BlazyManager
Implements a public facing blazy manager.

Namespace

Drupal\blazy

Code

protected function prepareBlazy(array &$element, array $build) {
  $item = $build['item'];
  $settings =& $build['settings'];
  $settings['_api'] = TRUE;
  $pathinfo = pathinfo($settings['uri']);
  $settings['extension'] = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
  $settings['unstyled'] = BlazyUtil::unstyled($settings);
  $settings['_richbox'] = !empty($settings['colorbox']) || !empty($settings['_richbox']);

  // Disable image style if so configured.
  if ($settings['unstyled']) {
    $images = [
      'box',
      'box_media',
      'image',
      'thumbnail',
      'responsive_image',
    ];
    foreach ($images as $image) {
      $settings[$image . '_style'] = '';
    }
  }
  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 must be set before Blazy::urlAndDimensions to provide placeholder.
  $this
    ->thumbnailAndPlaceholder($attributes, $settings);

  // Prepare image URL and its dimensions, including for rich-media content,
  // such as for local video poster image if a poster URI is provided.
  Blazy::urlAndDimensions($settings, $item);

  // Only process (Responsive) image/ video if no rich-media are provided.
  $this
    ->buildContent($element, $build);
  if (empty($build['content'])) {
    $this
      ->buildMedia($element, $build);
  }

  // Multi-breakpoint aspect ratio only applies if lazyloaded.
  // These may be set once at formatter level, or per breakpoint above.
  if (!empty($settings['blazy_data']['dimensions'])) {
    $attributes['data-dimensions'] = Json::encode($settings['blazy_data']['dimensions']);
  }

  // Provides extra attributes as needed, excluding url, item, done above.
  // Was planned to replace sub-module item markups if similarity is found for
  // theme_gridstack_box(), theme_slick_slide(), etc. Likely for Blazy 3.x+.
  foreach ([
    'caption',
    'media',
    'wrapper',
  ] as $key) {
    $element["#{$key}" . '_attributes'] = empty($build[$key . '_attributes']) ? [] : BlazyUtil::sanitize($build[$key . '_attributes']);
  }

  // Provides captions, if so configured.
  if ($build['captions'] && ($captions = $this
    ->buildCaption($build['captions'], $settings))) {
    $element['#captions'] = $captions;
    $element['#caption_attributes']['class'][] = $settings['item_id'] . '__caption';
  }

  // Pass common elements to theme_blazy().
  $element['#attributes'] = $attributes;
  $element['#settings'] = $settings;
  $element['#url_attributes'] = $build['url_attributes'];

  // Preparing Blazy to replace other blazy-related content/ item markups.
  foreach ([
    'content',
    'icon',
    'overlay',
    'preface',
    'postscript',
  ] as $key) {
    $element["#{$key}"] = empty($element["#{$key}"]) ? $build[$key] : NestedArray::mergeDeep($element["#{$key}"], $build[$key]);
  }
}