The value passed as #uri
can be either the image URI or an image relative path. template_preprocess_image_style()
passes the value of $variables['uri']
to ImageStyle::buildUrl()
which as first parameter accepts either the image relative path or its URI.
$style = ImageStyle::load($variables['style_name']);
// Determine the dimensions of the styled image.
$dimensions = [
'width' => $variables['width'],
'height' => $variables['height'],
];
$style->transformDimensions($dimensions, $variables['uri']);
$variables['image'] = [
'#theme' => 'image',
'#width' => $dimensions['width'],
'#height' => $dimensions['height'],
'#attributes' => $variables['attributes'],
'#style_name' => $variables['style_name'],
];
// If the current image toolkit supports this file type, prepare the URI for
// the derivative image. If not, just use the original image resized to the
// dimensions specified by the style.
if ($style->supportsUri($variables['uri'])) {
$variables['image']['#uri'] = $style->buildUrl($variables['uri']);
}
To get the module path, both drupal_get_path('module', $module_name)
, which is still implemented even in Drupal 9.3.x, and \Drupal::moduleHandler()->getModule($module_name)->getPath()
return the same value.
Deprecate drupal_get_path() and drupal_get_filename() and replace with ExtensionList::getPath() and ExtensionList::getPathname() and drupal_get_path() and drupal_get_filename() have been deprecated in favor of extension listing services (the associated change record) suggest to use either \Drupal::service('extension.list.module')->getPath($module_name)
or \Drupal::service('extension.path.resolver')->getPath('module', $module_name)
, to get a module path.