Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello there
When using .addClass() twig method within template and enabled big_pipe it won't work for anonymous users. Admin user works fine.
/**
* @file
* Sidebar navigation template.
*
* Available variables:
* - attributes: Attributes.
* - title_suffix: Title suffix. To place contextual links.
* - content: Block content.
*
* @ingroup themeable
*/
{% set panel_classes = ['panel', 'panel-default', 'panel-subnav'] %}
<div{{ attributes.addClass(panel_classes) }}>
{{ title_suffix }}
{{ content }}
</div>
Resulting into
<div>
...
...
</div>
Theme definition
/**
* Implements hook_theme().
*/
function example_menu_theme($existing, $type, $theme, $path) {
return [
'sidebar_navigation' => [
'variables' => [
'content' => NULL,
'attributes' => NULL,
],
'template' => 'sidebar-navigation',
],
];
}
Template call
/**
* Implements hook_preprocess_node().
*/
function example_preprocess_node(&$variables) {
$menu_name = 'example';
$variables['sidebar_navigation'] = [
'#theme' => 'sidebar_navigation',
'#content' => [
'#contextual_links' => [
'menu' => [
'route_parameters' => ['menu' => $menu_name],
],
],
],
];
}
Comments
Comment #2
podarokComment #3
krlucas CreditAttribution: krlucas at Genuine commented@podarok I am unable to reproduce on a standard Drupal 8 install.
Many core templates use attributes.addClass. They all seem to be rendering the classes specified when Big Pipe is enabled and pages are viewed as an anonymous user.
Can you provide some more detailed testing instructions? I notice your code snippet references Panels...
Comment #4
krlucas CreditAttribution: krlucas commentedComment #5
podarokComment #6
podarokHey
I've put some additional info about using template into issue's summary
Fix was done by putting attributes into hook_preprocess_node
and for template
Comment #7
podarokNo, we aren't using panels, these classes are for theming only
Comment #8
krlucas CreditAttribution: krlucas commentedThis has nothing to do with Big Pipe. You declared a brand new theme hook and template but never initialized the attributes variable in a preprocessor before trying to use it in the template.
No idea why you were seeing different results for anonymous/admin.
Feel free to re-open with a patch on a clean Drupal 8 install that clearly demonstrates the bug.