function template_preprocess_formatter_suite_image_embed_formatter in Formatter Suite 8
Prepares variables for image embed formatter template.
This template is used by this module's ImageInlineFormatter to enable embedding images directly on a page by using a data URL. Processing swaps out the image module's 'image_formatter' theme for this module's 'formatter_suite_image_formatter'. This template preprocessor then sets up the image in a way nearly identical to the parent class, but with support for data URL handling.
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.
- 'maximumEmbedWidth': (optional) the maximum width for embedding as a data URL.
- 'maximumEmbedHEight': (optional) the maximum height for embedding as a data URL.
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.
- 'formatter_suite_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 primarily insures that the maximum embed width/height variables are passed onwards to the customized theme templates that actually do the embedding.
Default template: formatter-suite-image-embed-formatter.html.twig.
Parameters
array $variables: Returns an associative array of theme variables.
File
- ./
formatter_suite.module, line 191 - Implements the principal entry points and hooks for the module.
Code
function template_preprocess_formatter_suite_image_embed_formatter(array &$variables) {
// The following code is based upon the image module's
// template_preprocess_image_formatter() function to process the
// image_formatter template.
//
// Changes include redirecting to this module's image embed templates,
// instead of that for the image module, and adding the maximum embed
// dimensions.
if (empty($variables['image_style']) === FALSE) {
// An image style is defined. Set up an image that redirects to
// our image_style template. Forward the name of the style.
$variables['image'] = [
'#theme' => 'formatter_suite_image_embed_style',
'#style_name' => $variables['image_style'],
];
}
else {
// No image style is defined. Use the stock Drupal core template.
$variables['image'] = [
'#theme' => 'formatter_suite_image_embed',
];
}
// Copy down the item's attributes.
$variables['image']['#attributes'] = $variables['item_attributes'];
if (isset($variables['maximumEmbedWidth']) === TRUE) {
$variables['image']['#maximumEmbedWidth'] = $variables['maximumEmbedWidth'];
}
if (isset($variables['maximumEmbedHeight']) === TRUE) {
$variables['image']['#maximumEmbedHeight'] = $variables['maximumEmbedHeight'];
}
// 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};
}
}