0

I have a module setting config variables which I'm trying to display in a twig template. When I use

function mytheme_preprocess_page(&$variables){
    $variables['facebook'] = \Drupal::config('social.settings')->get('facebook', 'No username');
    $variables['twitter'] = \Drupal::config('social.settings')->get('twitter', 'No username');
    $variables['instagram'] = \Drupal::config('social.settings')->get('instagram', 'No username');
    $variables['youtube'] = \Drupal::config('social.settings')->get('youtube', 'No username');              
}

I'm able to display the variables in my page.html.twig template by invoking

{{ facebook }}{{ twitter }}{{ instagram }}{{ youtube }}

However I'd like menu.html.twig to invoke it. Even when adding a preprocess hook for menu like so

function mytheme_preprocess_menu(&$variables){
    $variables['facebook'] = \Drupal::config('social.settings')->get('facebook', 'No username');
    $variables['twitter'] = \Drupal::config('social.settings')->get('twitter', 'No username');
    $variables['instagram'] = \Drupal::config('social.settings')->get('instagram', 'No username');
    $variables['youtube'] = \Drupal::config('social.settings')->get('youtube', 'No username');              
}

menu.html.twig produces nothing when invoking

{{ facebook }}{{ twitter }}{{ instagram }}{{ youtube }}

I know menu.html.twig is being used because I can make changes to it and the changes will appear. I also know that mytheme_preprocess_menu is being called because drupal_set_message works from it. However {{ dump() }} from menu.html.twig is not displaying anything on the page.

8
  • This should work. Have you checked that both the preprocess function and the custom template are used?
    – Eyal
    Jun 8, 2016 at 22:00
  • If you don't use a debugger tool then the die function can be used to make sure the preprocess function is used, and you can insert some reason text in the template to make sure it is used.
    – Eyal
    Jun 8, 2016 at 22:02
  • P.S. you should pass the \Drupal::config(...) result to a variable.
    – Eyal
    Jun 8, 2016 at 22:03
  • I know that the function is being called because I added drupal_set_message to it and it's able to display the variables just fine.
    – Amy
    Jun 9, 2016 at 13:12
  • Did you check that the template file is used?
    – Eyal
    Jun 9, 2016 at 13:18

1 Answer 1

2

The menu theme is declared using the variables key (see doc).

That means that only the keys present in the variables array can be passed to the template.

Any custom variable needs to be first added using hook_theme_registery_alter

4
  • I'm unable to find any reference to hook_theme_alter. Do you mean hook_theme_registry_alter()?
    – Amy
    Jun 9, 2016 at 13:34
  • Yes, my mistake.
    – Eyal
    Jun 9, 2016 at 13:35
  • I'm not having any luck. Do I add the variables to hook_theme_registry_alter in the same way as I did in mytheme_preprocess_menu? Or is there another technique?
    – Amy
    Jun 9, 2016 at 14:00
  • or does the menu not have access to the variables array from twig? I did check in the synchronization settings and the variables do appear there. Also after performing dump() on menu.html.twig only menu variables appear to be available.
    – Amy
    Jun 9, 2016 at 15:14

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.