0

I am developing a theme and trying to send custom vars to a twig menu template (since it pulls data from config) and have a loop to add variables in preprocess:

foreach($attributes as $attribute => $values){
    
    $variables[$element . '_attributes']->SetAttribute(
        $attribute,
        $values
    );
    // Also tried:
    $variables[$element]['#attributes'][$attribute][] = $values;
}

This function passes data to the template $variables as confirmed by kint, but SetAttribute returns a protected class property instead of the render array, while pushing to ['#attributes'] returns the attribute keys correctly but the arrays are empty...

How can I render the Attribute array in a menu?!

1 Answer 1

0

The issue was accessing variables inside of the menu {%macro%}... to pass variables to a menu you need to pass $variables['name'] to the menu before the macro and add a parameter to the macro, i.e.:

// preprocess

$variables['custom_attributes'] = array(
  'class' => ['myclass']
);
//template.html.twig

{{ menus.menu_links(items, attributes, 0, custom_attributes) }}
{% macro menu_links(items, attributes, menu_level, twigvars) %}

{{dump(twigvars.class)}}

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.