function template_preprocess_responsive_image_formatter

template_preprocess_responsive_image_formatter(&$variables)

Prepares variables for responsive image formatter templates.

Default template: responsive-image-formatter.html.twig.

Parameters

array $variables: An associative array containing:

  • item: An ImageItem object.
  • item_attributes: An optional associative array of HTML attributes to be placed in the img tag.
  • responsive_image_style_id: A responsive image style.
  • url: An optional \Drupal\Core\Url object.

File

core/modules/responsive_image/responsive_image.module, line 96
Responsive image display formatter for image fields.

Code

function template_preprocess_responsive_image_formatter(&$variables) {
  // Provide fallback to standard image if valid responsive image style is not
  // provided in the responsive image formatter.
  $responsive_image_style = ResponsiveImageStyle::load($variables['responsive_image_style_id']);
  if ($responsive_image_style) {
    $variables['responsive_image'] = array(
      '#type' => 'responsive_image',
      '#responsive_image_style_id' => $variables['responsive_image_style_id'],
    );
  }
  else {
    $variables['responsive_image'] = array(
      '#theme' => 'image',
    );
  }
  $item = $variables['item'];
  $attributes = array();
  // Do not output an empty 'title' attribute.
  if (Unicode::strlen($item->title) != 0) {
    $attributes['title'] = $item->title;
  }
  $attributes['alt'] = $item->alt;
  // Need to check that item_attributes has a value since it can be NULL.
  if ($variables['item_attributes']) {
    $attributes += $variables['item_attributes'];
  }
  if (($entity = $item->entity) && empty($item->uri)) {
    $variables['responsive_image']['#uri'] = $entity->getFileUri();
  }
  else {
    $variables['responsive_image']['#uri'] = $item->uri;
  }

  foreach (array('width', 'height') as $key) {
    $variables['responsive_image']["#$key"] = $item->$key;
  }
  $variables['responsive_image']['#attributes'] = $attributes;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!responsive_image!responsive_image.module/function/template_preprocess_responsive_image_formatter/8.1.x