client_hints-8.x-1.0-alpha3/modules/client_hints_image_formatter/client_hints_image_formatter.module
modules/client_hints_image_formatter/client_hints_image_formatter.module
<?php
/**
* Implements hook_theme_registry_alter().
*/
function client_hints_image_formatter_theme_registry_alter(&$theme_registry) {
$theme_registry['image__client_hints']['path'] = \Drupal::service('module_handler')->getModule('client_hints_image_formatter')->getPath() . '/templates';
$theme_registry['image__client_hints']['template'] = 'image--client-hints';
}
/**
* Implements template_preprocess_image__client_hints().
*/
function client_hints_image_formatter_preprocess_image__client_hints(&$variables) {
// Set processed url as clientHintsSrc data attribute.
$variables['attributes']['data-client-hints-src'] = $variables['uri'];
// Set an appropriately sized transparent svg as placeholder image.
$variables['attributes']['src'] = _client_hints_placeholder_svg($variables['width'], $variables['height']);
// Add Javascript library.
$variables['#attached']['library'][] = 'client_hints/lazy';
}
/**
* Implements template_preprocess_image__client_hints().
*/
function client_hints_image_formatter_preprocess_image_formatter__client_hints(&$variables) {
$variables['image']['#theme'] = 'image__client_hints';
}
