You are here

function template_preprocess_formatter_suite_general_image_formatter in Formatter Suite 8

Prepares variables for general image formatter template.

This template is used by this module's GeneralImageFormatter to add captions and link attributes to images. Processing swaps out the image module's 'image_formatter' theme for this module's 'formatter_suite_general_image_formatter'. This template preprocessor then sets up the image in a way nearly identical to the parent class, but with URL attributes.

Incoming variables:

  • 'item': an ImageItem object.
  • 'item_attributes': (optional) an associative array of html attributes to be placed in the <img> tag.
  • 'image_style': (optional) the name of an image style.
  • 'url': (optional) a \Drupal\Core\Url object for linking the image to another entity or file.
  • 'url_attributes': (optional) attributes for the image link, if any.

Outgoing variables include the above items, plus:

  • 'image': a theme-ready image description.

    • '#theme': the name of the theme to use.
    • '#attributes': a copy of 'item_attributes'.
    • '#width': the original image width.
    • '#height': the original image height.
    • '#alt': the image's alternative text.
    • '#style_name': (optional) the name of an image style.
    • '#title': (optional) an image title.
    • '#uri': the URI of the item or file.
    • '#maximumEmbedWidth': (optional) a copy of 'maximumEmbedWidth'.
    • '#maximumEmbedHeight': (optional) a copy of 'maximumEmbedHeight'.

The theme name will be one of:

  • 'image' if the image does not have a style.
  • 'image_style' if the image does have a style.

Note that the 'width' and 'height' are for the original image, not for the styled image.

Compared to the base class's template_preprocess_image_formatter(), this function just adds URL attributes.

Default template: formatter-suite-general-image-formatter.html.twig.

Parameters

array $variables: Returns an associative array of theme variables.

File

./formatter_suite.module, line 527
Implements the principal entry points and hooks for the module.

Code

function template_preprocess_formatter_suite_general_image_formatter(array &$variables) {

  //
  // Mimic image module.
  // -------------------
  // Repeat the image module's template_preprocess_image_formatter() function.
  if (empty($variables['image_style']) === FALSE) {

    // An image style is defined.
    $variables['image'] = [
      '#theme' => 'image_style',
      '#style_name' => $variables['image_style'],
    ];
  }
  else {

    // No image style is defined. Use the stock Drupal core template.
    $variables['image'] = [
      '#theme' => 'image',
    ];
  }

  // Copy down the item's attributes.
  $variables['image']['#attributes'] = $variables['item_attributes'];

  // Get the item being formatted.
  $item = $variables['item'];

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

  // Get the URI.
  if (($entity = $item->entity) !== NULL && empty($item->uri) === TRUE) {
    $variables['image']['#uri'] = $entity
      ->getFileUri();
  }
  else {
    $variables['image']['#uri'] = $item->uri;
  }

  // Add basic image attributes.
  foreach ([
    'width',
    'height',
    'alt',
  ] as $key) {
    $variables['image']["#{$key}"] = $item->{$key};
  }
}