I have a custom module that when installed, creates a paragraph type called "Gallery". Gallery has one field called "field_gallery_images", which is a media reference.
I am trying to replace image.html.twig
with image--gallery.html.twig
only for images inside my gallery.
I have managed to get Drupal to recognise and load my custom template from the modules template directory. However, every single image on the site now uses this template. I only want the template to be used for images of my paragraph type (or it's field). All other images should use the default image.html.twig.
Here is gallery.module
:
function gallery_theme($existing, $type, $theme, $path) {
return [
'image__gallery' => [
'template' => 'image--gallery',
'base hook' => 'image',
],
];
}
function gallery_theme_suggestions_image_alter(array &$suggestions, array $variables) {
$suggestions[] = 'image__gallery';
}
There is nothing I can test against inside any of the variables passed into both hooks to target my templates scope.
Am I using the wrong hooks?