I'm using the following code to preprocess the variables for my template:
function template_preprocess_imagegallery_format(&$vars) {
template_preprocess_image_formatter($vars);
$vars['image']['#theme'] = 'igimage';
$vars['image']['#thumbnail'] = ImageStyle::load('thumbnail')->buildUrl($vars ['image']['#uri']);
$vars['image']['#fullimage'] = file_create_url($vars ['image']['#uri']);
}
The #uri variable is already present, and if I just modify it everything works fine. But I need to add additional variables here as I need two different styles of the same image. But simply adding those variables to the array doesn't work.
If I dump the available variables in my Twig template with {{ dump(_context|keys) }}
, I don't see the variables I added there. If I try to use them in my template, nothing gets inserted.
I can see that the values are added to the array, when I dump the image variable in the theme that renders the entire field, I see my variables in the array. But they're still not available in the template that renders the image variable itself.
What else do I need to do to add a variable in my preprocess function that I can use in my Twig template?
$vars
and your keys (e.g.['image']
). I'm pretty sure that isn't allowed.template_preprocess_imagegallery_format
but 'template' should be replaced with your theme or module name. You will also need a cache clear.