function template_preprocess_formatter_suite_image_embed in Formatter Suite 8
Prepares variables for image embed template.
This template is used by this module's ImageInlineFormatter by way of the 'formatter_suite_image_embed_formatter' template. The template's job is to compare the image width and height to any limits imposed by the formatter, and then optionally embed the styled image as a data URL instead of as a file URL.
Incoming variables:
- '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.
- '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'.
Outgoing variables include the above plus:
- 'attributes': a copy of 'attributes'.
- 'width': the styled image width.
- 'height': the styled image height.
- 'alt': the image's alternative text.
- 'title': (optional) an image title.
- 'sizes': (optional) a list of sizes for responsive images, if the image could not be embedded as a data URL.
- 'srcset': (optional) a list of responsive images, if the image could note be embedded as a data URL.
- '#uri': (optional) the URI of the original image if it could not be embedded as a data URL.
Compared to the Drupal core template_preprocess_image(), this function checks if a image is within the maximum embed width/height limits and attempts to load the image to create a data URL. If the image cannot be found, a file URL is used.
Default template: formatter-suite-image-embed-style.html.twig.
Parameters
array $variables: Returns an associative array of theme variables.
File
- ./
formatter_suite.module, line 292 - Implements the principal entry points and hooks for the module.
Code
function template_preprocess_formatter_suite_image_embed(array &$variables) {
// Invoke Drupal core to process variables. This sets up variables with
// the image width, height, title, etc.
template_preprocess_image($variables);
// Check that the image is within our embedding bounds.
$maximumEmbedWidth = (int) $variables['maximumEmbedWidth'];
$maximumEmbedHeight = (int) $variables['maximumEmbedHeight'];
$width = (int) $variables['width'];
$height = (int) $variables['height'];
if ($maximumEmbedWidth > 0 && $width > $maximumEmbedWidth || $maximumEmbedHeight > 0 && $height > $maximumEmbedHeight) {
// The styled image is too large for embedding. The parent class has
// already set things up to use a file URL. Leave things that way.
return;
}
// Get the real path to the image.
if (empty($variables['uri']) === FALSE) {
$originalUri = $variables['uri'];
}
elseif (empty($variables['src']) === FALSE) {
$originalUri = $variables['src'];
}
else {
// Cannot find URI for image. Fall back to whatever the original theme
// was going to do.
return;
}
$fileSystem = \Drupal::service('file_system');
$path = $fileSystem
->realpath($originalUri);
if ($path === FALSE || @file_exists($path) === FALSE) {
// The file does not exist. Curious. Fall back to whatever Drupal core
// set up for the template.
return;
}
// Get the file's MIME type. We need this for the data URL.
$mimeType = \Drupal::service('file.mime_type.guesser')
->guess($path);
// Read the file's raw bytes and base64 encode them as a data URL.
$data = file_get_contents($path);
$dataUrl = 'data:' . $mimeType . ';base64,' . base64_encode($data);
// Set the image's source to be the data URL. To prevent this from
// being overwritten with the image's URI, delete that URI from the
// variables. Delete the srcset and sizes too, if present.
$variables['attributes']['src'] = $dataUrl;
unset($variables['uri']);
unset($variables['srcset']);
unset($variables['sizes']);
}