While theming a views-view-list.html.twig file I noticed adding a class to the list.attributes didn't work on line 28 of views-view-list.html.twig:
<{{ list.type }}{{ list.attributes.addClass('some-class') }}>

The issue seems to be that since I don't have any classes added to the List class field in the Format > Settings > Style options for my HTML List the list.attributes is never instantiated as an Attribute.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Daniel Korte created an issue. See original summary.

Daniel Korte’s picture

Status: Active » Needs review
FileSize
927 bytes

The attached patch instantiates the list.attributes whether the List class field is empty or not.

Lendude’s picture

Yeah I've run into this in other parts of core too, annoying to have to do a hook_preprocess to just add the Attribute. I did a quick scan of the rest of views.theme.inc and this seems the only instance where this seems relevant.

Never looked into the testing side of theme stuff, can we/should we test this?

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Munavijayalakshmi’s picture

Daniel Korte’s picture

Thanks for the reroll! It applies cleanly to 8.3.2

mlncn’s picture

Status: Needs review » Reviewed & tested by the community

It also works great! RTBC

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

No one's answered @Lendude's question about whether this should be tested. It would be good at least to know if we have exiting test coverage of adding a class before making this change.

Manuel Garcia’s picture

Hi, just randomly found this issue while looking at views module issues, and thought I'd chip in.
If this is happening on other parts of core, I wonder whether we should do the test for all of core templates, which would help us identify every instance where this is happening. I'm guessing we should perhaps be patching template_preprocess() ? Should we open a follow up?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jwilson3’s picture

I'd like to take this one step further and only instantiate the Attribute if it hasn't already been created somewhere else.

It's pretty complicated for mere mortals to guarantee the order in which preprocess functions run, so if someone has setup Attributes in another preprocess for a contrib/custom module or theme, they might be blown away by this.

Wrapping the statement in an if or ternary operator could fix this:

if (empty($variables['list']['attributes']) {
  $variables['list']['attributes'] = new Attribute();
}

Would be nice to standardize on this as the preprocess way throughout core/contrib.

In the same vein, if you're just trying to add classes in the twig template, you can make use of the create_attribute() twig command, shown here in the context of the list.attributes in a custom views-view-list.html.twig using ternary operator:

{% set list_attributes = (list.attributes) ?: create_attribute() %}
{% set list_attributes = list_attributes.addClass('my-list-element-class') %}

Or the long hand form of the above:

{% set list_attributes = list.attributes %}
{% if list_attributes is empty %}
{% set list_attributes = create_attribute() %}
{% endif %}
{% set list_attributes = list_attributes.addClass('my-list-element-class') %}

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Daniel Korte’s picture

This same issue happens in pager.html.twig too. Adding a class to the item attributes does not work when no other attributes have been added: {{ item.attributes.addClass('foo') }}

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

MegaKeegMan’s picture

Can confirm that this patch does not apply, but is absolutely still needed.

Gauravvvv’s picture

Status: Needs work » Needs review
FileSize
1009 bytes

I have attached a patch for same, please review

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

Next step would be to get a test case to show the issue.

MegaKeegMan’s picture

At the very least I can say that patch #24 does apply and does fix the issue. Tested on Drupal 9.5.4

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.