You are here

public static function Blazy::buildAttributes in Blazy 8

Prepares variables for blazy.html.twig templates.

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

File

src/Blazy.php, line 31

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

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

  // Provides optional attributes.
  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();
  $settings['ratio'] = empty($settings['ratio']) ? '' : str_replace(':', '', $settings['ratio']);
  $settings['use_media'] = $settings['embed_url'] && in_array($settings['type'], [
    'audio',
    'video',
  ]);
  self::buildUrl($settings, $variables['item']);

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

  // Check whether we have responsive image (no svg), or Blazy one.
  $settings['extension'] = pathinfo($settings['uri'])['extension'];
  if (!empty($settings['responsive_image_style_id']) && $settings['extension'] != 'svg') {
    self::buildResponsiveImage($variables);
  }
  else {
    self::buildImage($variables);
  }

  // Prepares a media player, and allows a tiny video preview without iframe.
  if ($settings['use_media'] && empty($settings['_noiframe'])) {
    self::buildIframeAttributes($variables);
  }

  // Image is optional for Video, and Blazy CSS background images.
  if ($variables['image']) {
    self::imageAttributes($variables);
  }
  self::thumbnailAttributes($variables);
}