How do I add a variable for a Twig template in my preprocess function? I'm using the following code to preprocess the variables for my template:

function template_preprocess_imagegallery_format(&amp;$vars) {
  template_preprocess_image_formatter($vars);
  $vars['image']['#theme'] = 'igimage';
  $vars['image']['#thumbnail'] = ImageStyle::load('thumbnail')-&gt;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?
I finally solved the problem, it turns out the problem was that I tried to add the variables in the wrong preprocessor. Editing the #theme works there, but to add variables I needed to preprocess the theme I set there:

function template_preprocess_imagegallery_format(&amp;$vars) {
  template_preprocess_image_formatter($vars);
  $vars['image']['#theme'] = 'igimage';
}

function template_preprocess_igimage(&amp;$vars) {
    $vars['thumbnail'] = ImageStyle::load('thumbnail')-&gt;buildUrl($vars['uri']);
    $vars['fullimage'] = file_create_url($vars['uri']);
}