sirv-8.x-3.0-beta4/src/SirvPreprocess.php
src/SirvPreprocess.php
<?php
namespace Drupal\sirv;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Functions for preprocessing variables in theme templates.
*/
class SirvPreprocess implements ContainerInjectionInterface {
const SIRV_SCRIPTS_DOMAIN = 'https://scripts.sirv.com';
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The Sirv URL service.
*
* @var \Drupal\sirv\SirvUrlInterface
*/
protected $sirvUrl;
/**
* Constructs a SirvPreprocess object.
*
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The entity type manager.
* @param \Drupal\sirv\SirvUrlInterface $sirv_url
* The Sirv URL service.
*/
public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, ConfigFactoryInterface $config_factory, SirvUrlInterface $sirv_url) {
$this->streamWrapperManager = $stream_wrapper_manager;
$this->configFactory = $config_factory;
$this->sirvUrl = $sirv_url;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('stream_wrapper_manager'),
$container->get('config.factory'),
$container->get('sirv.url')
);
}
/**
* Preprocess variables in the "image_formatter" theme template.
*
* @param array $variables
* The theme template variables.
*/
public function preprocessImageFormatter(array &$variables) {
// If there are no Sirv image settings, do not continue.
if ('image' !== ($variables['sirv']['type'] ?? NULL)) {
return;
}
// If the image does not have a URI, do not continue.
if (!$uri = $variables['image']['#uri'] ?? NULL) {
return;
}
$sirv = $variables['sirv'];
// Determine the scheme from the URI and save it with the Sirv data.
$sirv['original_scheme'] = $this->streamWrapperManager->getScheme($uri);
// Add the Sirv data to the image.
$variables['image']['#sirv'] = $sirv;
}
/**
* Preprocess variables in the "image_style" theme template.
*
* @param array $variables
* The theme template variables.
*/
public function preprocessImageStyle(array &$variables) {
// If there are no Sirv image settings, do not continue.
if ('image' !== ($variables['sirv']['type'] ?? NULL)) {
return;
}
// If the image does not exist, do not continue.
if (!isset($variables['image'])) {
return;
}
// Add the Sirv data to the image.
if (isset($variables['image'])) {
$variables['image']['#sirv'] = $variables['sirv'];
}
}
/**
* Preprocess variables in the "image" theme template.
*
* @param array $variables
* The theme template variables.
*/
public function preprocessImage(array &$variables) {
// If there are no Sirv image settings, do not continue.
if ('image' !== ($variables['sirv']['type'] ?? NULL)) {
return;
}
// If the src attribute is not set, do not continue.
if (!$src = ($variables['attributes']['src'] ?? NULL)) {
return;
}
// Get the site-wide Sirv settings.
$sirv_settings = $this->configFactory->get('sirv.settings')->get();
// Get the image-specific Sirv settings.
$sirv_image_settings = $variables['sirv']['settings'] ?? [];
// Set defaults for any missing Sirv image settings.
$sirv_image_settings += [
'profile' => NULL,
'responsive' => FALSE,
'responsive_scale_crop' => NULL,
'responsive_resize_step' => NULL,
'lazy_load' => FALSE,
'lazy_load_threshold' => NULL,
];
$variables['attributes']['src'] = $this->sirvUrl->getUrlfromUriReference($src, $sirv_image_settings, $variables['sirv']['original_scheme'] ?? NULL);
$attributes = $variables['attributes'];
// Define an empty array for the data-options attribute.
$data_options = [];
// Make sure the "class" attribute exists.
if (!isset($attributes['class'])) {
$attributes['class'] = [];
}
$responsive = $sirv_image_settings['responsive'];
$lazy_load = $sirv_image_settings['lazy_load'];
if (!empty($sirv_settings['domain'])) {
// Add header links to preconnect and prefetch DNS.
$variables['#attached']['html_head_link'][][] = [
'rel' => 'preconnect',
'href' => 'https://' . $sirv_settings['domain'],
'crossorigin' => TRUE,
];
$variables['#attached']['html_head_link'][][] = [
'rel' => 'dns-prefetch',
'href' => 'https://' . $sirv_settings['domain'],
];
}
if ($responsive || $lazy_load) {
// Set the data-src attribute to the Sirv URL and remove the src
// attribute.
if (!empty($variables['url'])) {
$attributes['data-src'] = $variables['url'];
unset($attributes['url']);
}
$attributes['class'][] = 'Sirv';
// Add the Sirv library.
$variables['#attached']['library'][] = 'sirv/sirv';
// Add header links to preconnect and prefetch DNS.
$variables['#attached']['html_head_link'][][] = [
'rel' => 'preconnect',
'href' => self::SIRV_SCRIPTS_DOMAIN,
'crossorigin' => TRUE,
];
$variables['#attached']['html_head_link'][][] = [
'rel' => 'dns-prefetch',
'href' => self::SIRV_SCRIPTS_DOMAIN,
];
}
else {
if (!empty($variables['url'])) {
$attributes['src'] = $variables['url'];
}
}
if ($responsive) {
if (!$lazy_load) {
$data_options['lazy'] = 'false';
}
// Handle the Responsive Scale/Crop setting.
switch ($sirv_image_settings['responsive_scale_crop']) {
case 'contain':
$attributes['class'][] = 'image-fit';
break;
case 'cover':
$attributes['class'][] = 'image-fill';
$data_options['fit'] = 'cover';
break;
case 'crop':
$attributes['class'][] = 'image-fill';
$data_options['fit'] = 'crop';
break;
}
// Handle the Responsive Resize Step setting.
if ($sirv_image_settings['responsive_resize_step']) {
$data_options['resizeStep'] = $sirv_image_settings['responsive_resize_step'];
}
}
if ($lazy_load) {
if (!$responsive) {
$data_options['resize'] = 'false';
}
// Handle the Lazy Load Threshold setting.
if ($sirv_image_settings['lazy_load_threshold']) {
$data_options['threshold'] = $sirv_image_settings['lazy_load_threshold'];
}
}
// Compile the data options into a data-options attribute.
$data_options_attribute = NULL;
$data_options_items = [];
foreach ($data_options as $key => $value) {
$data_options_items[] = "$key:$value";
}
$data_options_attribute = implode(';', $data_options_items);
if ($data_options_attribute) {
$attributes['data-options'] = $data_options_attribute;
}
$variables['attributes'] = $attributes;
foreach (['alt', 'title'] as $key) {
if (isset($variables[$key])) {
// If the property has already been defined in the attributes,
// do not override, including NULL.
if (array_key_exists($key, $variables['attributes'])) {
continue;
}
$variables['attributes'][$key] = $variables[$key];
}
}
}
}
