I'm using the following code to preprocess the variables for my template:

function template_preprocess_imagegallery_format(&$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?

  • You have a space between the array name $vars and your keys (e.g. ['image']). I'm pretty sure that isn't allowed. Aug 15 '15 at 19:42
  • I removed the spaces, it doesn't actually matter and I have the same result with or without spaces. The spaces are from code I copied from an example. Aug 15 '15 at 19:45
  • Did you rename the function? It shouldn't be template_preprocess_imagegallery_format but 'template' should be replaced with your theme or module name. You will also need a cache clear. Aug 15 '15 at 19:49
  • Found a couple of good resources: Twig best practices - preprocess functions and templates and Theme system overview. Aug 15 '15 at 19:51

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(&$vars) {
  $vars['image']['#theme'] = 'igimage';

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

Theme name : atvdirect

  • create a file atvdirect.theme on root of your theme, aside atvdirect.info.yml
  • add below code in atvdirect.theme file
  • use {{ logopath }} in page.html.twig

    function atvdirect_preprocess_page(&$variables) {
      $variables['logopath'] = '/'.drupal_get_path('theme','atvdirect') .'/logo.png';
  • 1
    Thank you for finally saying what file this should be placed in.
    – Ryan H
    Jan 13 '17 at 20:46
  • Excellent answer, well explained.
    – Pupil
    Apr 17 '17 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.