Drupal - How do I add a variable for a Twig template in my preprocess function?

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']);
}

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

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

Tags:

Theming

8