You are here

protected function BlazyManager::thumbnailAndPlaceholder in Blazy 8.2

Same name and namespace in other branches
  1. 7 src/BlazyManager.php \Drupal\blazy\BlazyManager::thumbnailAndPlaceholder()

Build thumbnails, also to provide placeholder for blur effect.

1 call to BlazyManager::thumbnailAndPlaceholder()
BlazyManager::prepareBlazy in src/BlazyManager.php
Prepares the Blazy output as a structured array ready for ::renderer().

File

src/BlazyManager.php, line 359

Class

BlazyManager
Implements a public facing blazy manager.

Namespace

Drupal\blazy

Code

protected function thumbnailAndPlaceholder(array &$attributes, array &$settings) {
  $settings['placeholder_ui'] = $settings['placeholder'];
  $path = $style = '';

  // With CSS background, IMG may be empty, add thumbnail to the container.
  if (!empty($settings['thumbnail_style'])) {
    $style = $this
      ->entityLoad($settings['thumbnail_style'], 'image_style');
    if ($style) {
      $path = $style
        ->buildUri($settings['uri']);
      $attributes['data-thumb'] = $settings['thumbnail_url'] = BlazyUtil::transformRelative($settings['uri'], $style);
      if (!is_file($path) && BlazyUtil::isValidUri($path)) {
        $style
          ->createDerivative($settings['uri'], $path);
      }
    }
  }

  // Supports unique thumbnail different from main image, such as logo for
  // thumbnail and main image for company profile.
  if (!empty($settings['thumbnail_uri'])) {
    $path = $settings['thumbnail_uri'];
    $attributes['data-thumb'] = $settings['thumbnail_url'] = BlazyUtil::transformRelative($path);
  }

  // Provides image effect if so configured.
  if (!empty($settings['fx'])) {
    $attributes['class'][] = 'media--fx';

    // Ensures at least a hook_alter is always respected. This still allows
    // Blur and hook_alter for Views rewrite issues, unless global UI is set
    // which was already warned about anyway.
    if (empty($settings['placeholder_fx']) && empty($settings['unstyled'])) {
      $this
        ->createPlaceholder($settings, $style, $path);
    }

    // Being a separated .b-blur with .b-lazy, this should work for any lazy.
    $attributes['data-animation'] = $settings['fx'];
  }

  // Mimicks private _responsive_image_image_style_url, #3119527.
  if (empty($settings['image_style']) && !empty($settings['resimage'])) {
    $fallback = $settings['resimage']
      ->getFallbackImageStyle();
    if ($fallback == '_empty image_') {
      $placeholder = empty($settings['width']) ? BlazyInterface::PLACEHOLDER : BlazyUtil::generatePlaceholder($settings['width'], $settings['height']);
      $settings['image_url'] = empty($settings['placeholder']) ? $placeholder : $settings['placeholder'];
    }
    else {
      $settings['image_style'] = $fallback;
    }
  }
}