You are here

public static function Blazy::imageAttributes in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Blazy.php \Drupal\blazy\Blazy::imageAttributes()
  2. 7 src/Blazy.php \Drupal\blazy\Blazy::imageAttributes()

Modifies $variables to provide optional (Responsive) image attributes.

1 call to Blazy::imageAttributes()
Blazy::buildAttributes in src/Blazy.php
Prepares variables for blazy.html.twig templates.

File

src/Blazy.php, line 177

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

public static function imageAttributes(array &$variables) {
  $item = $variables['item'];
  $image =& $variables['image'];
  $settings = $variables['settings'];
  $attributes =& $variables['item_attributes'];

  // Respects hand-coded image attributes.
  if ($item) {
    if (!isset($attributes['alt'])) {
      $attributes['alt'] = isset($item->alt) ? $item->alt : NULL;
    }

    // Do not output an empty 'title' attribute.
    if (isset($item->title) && mb_strlen($item->title) != 0) {
      $attributes['title'] = $item->title;
    }
  }

  // Only output dimensions for non-svg. Respects hand-coded image attributes.
  // Do not pass it to $attributes to also respect both (Responsive) image.
  // Responsive images with height and width save a lot of calls to
  // image.factory service for every image and breakpoint in
  // _responsive_image_build_source_attributes(). Very necessary for
  // external file system like Amazon S3.
  if (!isset($attributes['width']) && $settings['extension'] != 'svg') {
    $image['#width'] = $settings['width'];
    $image['#height'] = $settings['height'];
  }
  $attributes['class'][] = 'media__image media__element';
  $image['#attributes'] = $attributes;
}