I am trying to add a variable to render in the menu-account.html.twig file.
Here is my preprocess function in my .theme file.
function HOOK_preprocess_menu__account(&$variables) {
$variables['account_name'] = Drupal::currentUser()->getAccountName();
}
HOOK is replaced with my theme name
And in my twig file I have this snippet:
<div class="welcome-message">
Welcome, {{ account_name }}!
</div>
So I can edit pre-existing variables but I cannot seem to add a new one.
I was reading that new variables cannot be added in a preprocess function and that the HOOK_theme_registry_alter should be used.
Here is what I attempted and by using xDebug I can tell that it is getting looked at when flushing caches.
function HOOK_theme_registry_alter(&$theme_registry) {
$theme_registry['menu']['variables']['account_name'] = [];
}
Does anyone know the steps to get a new variable to be rendered in the menu twig file. This my first time using the theme registry hook so I might be doing something wrong there as well.
Thanks,
EDIT: "menu--account.html.twig" was named correctly in my code but mistyped in my question. Thank you to GiorgosK for pointing this out.