function template_preprocess_image_formatter

Same name in other branches
  1. 8.9.x core/modules/image/image.field.inc \template_preprocess_image_formatter()
  2. 10 core/modules/image/image.field.inc \template_preprocess_image_formatter()
  3. 11.x core/modules/image/image.field.inc \template_preprocess_image_formatter()

Prepares variables for image formatter templates.

Default template: 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.
  • image_style: An optional image style.
  • url: An optional \Drupal\Core\Url object.

File

core/modules/image/image.field.inc, line 44

Code

function template_preprocess_image_formatter(&$variables) {
    if ($variables['image_style']) {
        $variables['image'] = [
            '#theme' => 'image_style',
            '#style_name' => $variables['image_style'],
        ];
    }
    else {
        $variables['image'] = [
            '#theme' => 'image',
        ];
    }
    $variables['image']['#attributes'] = $variables['item_attributes'];
    $item = $variables['item'];
    // Do not output an empty 'title' attribute.
    if (!is_null($item->title) && mb_strlen($item->title) != 0) {
        $variables['image']['#title'] = $item->title;
    }
    if (($entity = $item->entity) && empty($item->uri)) {
        $variables['image']['#uri'] = $entity->getFileUri();
    }
    else {
        $variables['image']['#uri'] = $item->uri;
    }
    foreach ([
        'width',
        'height',
        'alt',
    ] as $key) {
        $variables['image']["#{$key}"] = $item->{$key};
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.