11

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?

4
  • 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, 2015 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, 2015 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, 2015 at 19:49
  • Found a couple of good resources: Twig best practices - preprocess functions and templates and Theme system overview. Aug 15, 2015 at 19:51

2 Answers 2

8

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) {
  template_preprocess_image_formatter($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']);
}
4

For a theme whose machine name is atvdirect, create the atvdirect.theme file in the directory containing the .info.yml file for the theme, and add the following code.

function atvdirect_preprocess_page(&$variables) {
  $variables['logopath'] = '/'.drupal_get_path('theme','atvdirect') .'/logo.png';
}

Now you can use {{ logopath }} in the page.html.twig template file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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