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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.