Skip to main content
edited tags
Source Link
Kevin
  • 28k
  • 12
  • 95
  • 196

I have a custom theme (subtheme of drupal bootstrap theme). We are overriding menu--account.html.twigmenu--account.html.twig. So I copied menu--account.html.twigmenu--account.html.twig from the bootstrap theme template folder into my theme's template folder, and I verified this template is being used because static text changes I added show up on screen.

Now I'm trying to add a variable that the menu--account.html.twigmenu--account.html.twig template can read.

How do I do that? My variable's name is 'nickname'. I tried this:

function mymodule_preprocess_menu__account(&$variables) {
$variables['nickname'] = 'Jeff';
}

 function mymodule_preprocess_menu__account(&$variables)
 {     
     $variables['nickname'] = 'Jeff';     
 }  

The variable is added to the array, and I can see it if I use dpm, but when I reference {{nickname}} in the template it's always null.

I also read I had to define the variable like this:

function candidate_poral_theme_registry_alter(&$theme_registry) { $theme_registry['menu__account']['variables']['nickname'] = 'default'; }

 function candidate_poral_theme_registry_alter(&$theme_registry)
 {
   $theme_registry['menu__account']['variables']['nickname'] = 'default';
 }

But that also doesn't work.

Any assistance would be welcome.

I have a custom theme (subtheme of drupal bootstrap theme). We are overriding menu--account.html.twig. So I copied menu--account.html.twig from the bootstrap theme template folder into my theme's template folder, and I verified this template is being used because static text changes I added show up on screen.

Now I'm trying to add a variable that the menu--account.html.twig template can read.

How do I do that? My variable's name is 'nickname'. I tried this:

function mymodule_preprocess_menu__account(&$variables) {
$variables['nickname'] = 'Jeff';
}

The variable is added to the array, and I can see it if I use dpm, but when I reference {{nickname}} in the template it's always null.

I also read I had to define the variable like this:

function candidate_poral_theme_registry_alter(&$theme_registry) { $theme_registry['menu__account']['variables']['nickname'] = 'default'; }

But that also doesn't work.

Any assistance would be welcome.

I have a custom theme (subtheme of drupal bootstrap theme). We are overriding menu--account.html.twig. So I copied menu--account.html.twig from the bootstrap theme template folder into my theme's template folder, and I verified this template is being used because static text changes I added show up on screen.

Now I'm trying to add a variable that the menu--account.html.twig template can read.

How do I do that? My variable's name is 'nickname'. I tried this:

 function mymodule_preprocess_menu__account(&$variables)
 {     
     $variables['nickname'] = 'Jeff';     
 }  

The variable is added to the array, and I can see it if I use dpm, but when I reference {{nickname}} in the template it's always null.

I also read I had to define the variable like this:

 function candidate_poral_theme_registry_alter(&$theme_registry)
 {
   $theme_registry['menu__account']['variables']['nickname'] = 'default';
 }

But that also doesn't work.

Any assistance would be welcome.

Source Link

How to add a custom variable that can be accessed on bootstrap's menu--account.html.twig?

I have a custom theme (subtheme of drupal bootstrap theme). We are overriding menu--account.html.twig. So I copied menu--account.html.twig from the bootstrap theme template folder into my theme's template folder, and I verified this template is being used because static text changes I added show up on screen.

Now I'm trying to add a variable that the menu--account.html.twig template can read.

How do I do that? My variable's name is 'nickname'. I tried this:

function mymodule_preprocess_menu__account(&$variables) {
$variables['nickname'] = 'Jeff';
}

The variable is added to the array, and I can see it if I use dpm, but when I reference {{nickname}} in the template it's always null.

I also read I had to define the variable like this:

function candidate_poral_theme_registry_alter(&$theme_registry) { $theme_registry['menu__account']['variables']['nickname'] = 'default'; }

But that also doesn't work.

Any assistance would be welcome.