You are here

public static function Blazy::imageAttributes in Blazy 7

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

Modifies image attributes.

1 call to Blazy::imageAttributes()
BlazyManager::buildMedia in src/BlazyManager.php
Build out (Responsive) image.

File

src/Blazy.php, line 123

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

public static function imageAttributes(array &$attributes, array $settings, $item = NULL) {

  // Unlike D8, we have no free $item->_attributes from RDF, provide one here.
  // With or without rdf enabled, no need to check for module_exists().
  $attributes['typeof'] = [
    'foaf:Image',
  ];

  // Extract field item attributes for the theme function, and unset them
  // from the $item so that the field template does not re-render them.
  if ($item && isset($item->_attributes)) {
    $attributes += $item->_attributes;
    unset($item->_attributes);
  }

  // Respects hand-coded image attributes.
  if ($settings['width'] && !isset($attributes['width'])) {
    $attributes['height'] = $settings['height'];
    $attributes['width'] = $settings['width'];
  }

  // The fallback must run as fallback, also for Picture.
  if ($item) {
    foreach ([
      'width',
      'height',
      'alt',
      'title',
    ] as $key) {
      if (isset($item->{$key})) {

        // Respects hand-coded image attributes, image style, and set once.
        if (array_key_exists($key, $attributes)) {
          continue;
        }

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