You are here

public static function Blazy::preprocessBlazy in Blazy 8.2

Prepares variables for blazy.html.twig templates.

Overrides BlazyInterface::preprocessBlazy

3 calls to Blazy::preprocessBlazy()
BlazyManagerTest::testPreprocessBlazy in tests/src/Kernel/BlazyManagerTest.php
Tests building Blazy attributes.
BlazyUnitTest::testPreprocessBlazy in tests/src/Unit/BlazyUnitTest.php
Tests \Drupal\blazy\Blazy::preprocessBlazy.
template_preprocess_blazy in ./blazy.module
Prepares variables for blazy.html.twig templates.

File

src/Blazy.php, line 28

Class

Blazy
Provides common blazy utility static methods.

Namespace

Drupal\blazy

Code

public static function preprocessBlazy(array &$variables) {
  $element = $variables['element'];
  foreach (BlazyDefault::themeProperties() as $key) {
    $variables[$key] = isset($element["#{$key}"]) ? $element["#{$key}"] : [];
  }

  // Provides optional attributes, see BlazyFilter.
  foreach (BlazyDefault::themeAttributes() as $key) {
    $key = $key . '_attributes';
    $variables[$key] = empty($element["#{$key}"]) ? [] : new Attribute($element["#{$key}"]);
  }

  // Provides sensible default html settings to shutup notices when lacking.
  $settings =& $variables['settings'];
  $settings += BlazyDefault::itemSettings();

  // Do not proceed if no URI is provided.
  if (empty($settings['uri'])) {
    return;
  }

  // URL and dimensions are built out at BlazyManager::preRenderBlazy().
  // Still provides a failsafe for direct call to theme_blazy().
  if (empty($settings['_api'])) {
    self::urlAndDimensions($settings, $variables['item']);
  }

  // Allows rich Media entities stored within `content` to take over.
  if (empty($variables['content'])) {
    self::buildMedia($variables);
  }

  // Aspect ratio to fix layout reflow with lazyloaded images responsively.
  // This is outside 'lazy' to allow non-lazyloaded iframe/content use it too.
  $settings['ratio'] = empty($settings['width']) ? '' : $settings['ratio'];
  if ($settings['ratio']) {
    self::aspectRatioAttributes($variables['attributes'], $settings);
  }

  // Makes a little order here due to twig ignoring the preset priority.
  $attributes =& $variables['attributes'];
  $classes = empty($attributes['class']) ? [] : $attributes['class'];
  $attributes['class'] = array_merge([
    'media',
    'media--blazy',
  ], $classes);
}