Follow-up to #2502089: Remove SafeMarkup::set() in template_preprocess_views_view_table()

Follow-up to #2280965: [meta] Remove every SafeMarkup::set() call

Problem/Motivation

@MauPalantir noticed there was a bunch of class concatenation done in template_preprocess_views_view_table()

Proposed resolution

Use Attribute() object to addClass() and/or move the class building to the template.

Remaining tasks

Has this been fixed in #2894449: Indirect modification of overloaded element with Views responsive table ?

Manual testing steps

User interface changes

N/A

API changes

N/A

Comments

joelpittet created an issue. See original summary.

star-szr’s picture

Title: Clean up CSS class concatination in template_preprocess_views_view_table() » Clean up CSS class concatenation in template_preprocess_views_view_table()
joelpittet’s picture

Status: Active » Needs review
Issue tags: +code cleanup, +Needs manual testing
StatusFileSize
new4.26 KB

This likely needs to be postponed till after 8.0.0 release. There should be no markup diff.

Status: Needs review » Needs work

The last submitted patch, 3: clean_up_css_class-2554957-3.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Needs work » Needs review
Issue tags: +Novice
xito’s picture

Status: Needs review » Needs work
Issue tags: +Dublin2016

I saw the patch provided on the #3 comment at it seems that all the "['class']" instances has been removed, but when I tried to apply the patch, it doesn't work, I think that the reason could be for the differences between the core version (when the patch was sent it and the current).

Can you update the patch? I review it again.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new4.25 KB

Seems to still apply but with fuzz. Would you like to review this @xito?

Status: Needs review » Needs work

The last submitted patch, 9: 2554957-9.patch, failed testing.

The last submitted patch, 9: 2554957-9.patch, failed testing.

mmrares’s picture

I am having a look at this.

Anonymous’s picture

The patch doesn't work for me too and I'm also trying to figure how to fix that today at DrupalCon Dublin.

mmrares’s picture

Status: Needs work » Needs review
StatusFileSize
new5.41 KB
new1.15 KB

The tests failed because the order of the classes changed and xpath checks for precise string. Changed the test to use cssSelect method.

Anonymous’s picture

StatusFileSize
new827 bytes

Damn, I was close to propose my patch first ! :)
Can I propose you to correct a attributes['id'] with a ->setAttribute('id',...) ?

I don't have enough time to review the patch now, so I just give you my correction to yours

Status: Needs review » Needs work

The last submitted patch, 15: 2554957-15.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review

@Anansi_boy thanks for the suggestion but actually you can do both ways because the Attribute object implements ArrayAccess. I think your patch failed there because maybe it's missing the -p1 for the patch command but that's a guess.

#14 seems like the one that needs some review so I'll hide #15.

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.

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.

star-szr’s picture

StatusFileSize
new5.4 KB

Quick reroll of #14 for 8.5.x, only short array syntax stuff changed.

tacituseu’s picture

Component: theme system » views.module
joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Nice cleanup

xjm’s picture

Title: Clean up CSS class concatenation in template_preprocess_views_view_table() » Use Attribute::addClass() instead of CSS class concatenation in template_preprocess_views_view_table()
xjm’s picture

Status: Reviewed & tested by the community » Needs review

This is surprisingly difficult to review for a 5 K patch.

These hunks are the easy ones to understand:

  1. +++ b/core/modules/views/views.theme.inc
    @@ -487,19 +484,21 @@ function template_preprocess_views_view_table(&$variables) {
    -      $variables['header'][$field]['attributes'] = [];
    ...
    +      $variables['header'][$field]['attributes'] = new Attribute();
    
    @@ -514,13 +513,6 @@ function template_preprocess_views_view_table(&$variables) {
    -      $variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
    

    This switches to defining an Attribute directly rather than an array that will be overwritten later.

  2. +++ b/core/modules/views/views.theme.inc
    @@ -487,19 +484,21 @@ function template_preprocess_views_view_table(&$variables) {
    -      $class = $fields[$field]->elementLabelClasses(0);
    -      if ($class) {
    ...
    +      if ($class = $fields[$field]->elementLabelClasses(0)) {
    

    This just inlines the variable declaration in the if. This change probably isn't a necessary part of the issue scope.

  3. +++ b/core/modules/views/views.theme.inc
    @@ -487,19 +484,21 @@ function template_preprocess_views_view_table(&$variables) {
    -        $variables['header'][$field]['attributes']['class'][] = $class;
    ...
    +        $variables['header'][$field]['attributes']->addClass($class);
    ...
    -        $variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
    +        $variables['header'][$field]['attributes']->addClass($options['info'][$field]['responsive']);
    ...
    -        $variables['header'][$field]['attributes']['class'][] = Html::cleanCssIdentifier($options['info'][$field]['align']);
    +        $variables['header'][$field]['attributes']->addClass(Html::cleanCssIdentifier($options['info'][$field]['align']));
    

    These all switch to using the addClass() method instead of adding them to the array, now that it's an Attribute and not an array.

  4. +++ b/core/modules/views/views.theme.inc
    @@ -543,16 +535,25 @@ function template_preprocess_views_view_table(&$variables) {
    -        $column_reference['attributes'] = [];
    +        $column_reference['attributes'] = new Attribute();
    
    @@ -583,7 +584,6 @@ function template_preprocess_views_view_table(&$variables) {
    -      $column_reference['attributes'] = new Attribute($column_reference['attributes']);
    

    As above, defines an attribute directly instead of overwriting an array.

  5. +++ b/core/modules/views/views.theme.inc
    @@ -543,16 +535,25 @@ function template_preprocess_views_view_table(&$variables) {
    -        $column_reference['attributes']['class'][] = $classes;
    +        $column_reference['attributes']->addClass($classes);
    ...
    -        $column_reference['attributes']['class'][] = $options['info'][$field]['responsive'];
    +        $column_reference['attributes']->addClass($options['info'][$field]['responsive']);
    

    As above, switches to using addClass() now that the attribute is defined directly.

  6. +++ b/core/modules/views_ui/src/Tests/PreviewTest.php
    @@ -281,7 +281,7 @@ public function testPreviewSortLink() {
    -    $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name']);
    +    $elements = $this->cssSelect('th.views-field.views-field-name a');
    
    @@ -291,7 +291,7 @@ public function testPreviewSortLink() {
    -    $elements = $this->xpath('//th[contains(@class, :class)]/a', [':class' => 'views-field views-field-name is-active']);
    +    $elements = $this->cssSelect('th.is-active.views-field.views-field-name a');
    

    These changes are necessary because the test in HEAD hardcoded the classes in a certain order. cssSelect() is more of a best practice anyway.

These are the hunks that confuse me:

+++ b/core/modules/views/views.theme.inc
@@ -449,9 +449,6 @@ function template_preprocess_views_view_table(&$variables) {
-    if ($active == $field) {
-      $variables['fields'][$field] .= ' is-active';
-    }

@@ -487,19 +484,21 @@ function template_preprocess_views_view_table(&$variables) {
+      if ($active == $field) {
+        $variables['header'][$field]['attributes']->addClass('is-active');
       }

@@ -514,13 +513,6 @@ function template_preprocess_views_view_table(&$variables) {
-    // Add a CSS align class to each field if one was set.
-    if (!empty($options['info'][$field]['align'])) {
-      $variables['fields'][$field] .= ' ' . Html::cleanCssIdentifier($options['info'][$field]['align']);
     }

@@ -543,16 +535,25 @@ function template_preprocess_views_view_table(&$variables) {
+      if ($active == $field) {
+        $column_reference['attributes']->addClass('is-active');
+      }
...
+      // Add a CSS align class to each field if one was set.
+      if (!empty($options['info'][$field]['align'])) {
+        $column_reference['attributes']->addClass(Html::cleanCssIdentifier($options['info'][$field]['align']));
       }

So, previously, the active and alignment classes were being concatenated directly onto $variables['fields'][$field]. Now, we're more cleanly adding them with Attribute::addClass()... except that it is now being added to $variables['header'][$field]['attributes'] (in the case of the active class) and $column_reference['attributes'] (in both cases) instead of $variables['fields'][$field]['attributes']. Presumably this is because it gets split into those somewhere but I cannot for the life of me see where after half an hour of staring at the HEAD code. What am I missing?

Also, note the cheeky comment:

+++ b/core/modules/views/views.theme.inc
@@ -449,9 +449,6 @@ function template_preprocess_views_view_table(&$variables) {
     // Create a second variable so we can easily find what fields we have and
     // what the CSS classes should be.

"Easily". Ha!

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.

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.

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.

smustgrave’s picture

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

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Points in #24 still need to be addressed.

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.

kopeboy’s picture

Hoping this will help, here's how I can reproduce this error:

  1. Create a View with Table display and add some fields
  2. Exclude some fields from display or set some fields to the column of another field, save
  3. Change the Responsive (High/Medium/Low) of some these columns, save
  4. Get Error Notice: Indirect modification of overloaded element of Drupal\Core\Template\Attribute has no effect in template_preprocess_views_view_table() (line 569 of /var/www/html/web/core/modules/views/views.theme.inc)

Note that:

  • You cannot remove the error until you remember or by chance set the previous columns' responsive class
  • A workaround to stop the error is to change the view display and set it back to Table to reset those settings (fields' settings will stay)
catch’s picture

_utsavsharma’s picture

StatusFileSize
new3.45 KB
new3.45 KB

Patch for 11.x.
Pointer in #24 still needs to be addressed.

quietone’s picture

Issue summary: View changes
Status: Needs work » Postponed (maintainer needs more info)

There has been no answer to catch's query 11 months asking if there is more to do here. I am setting the status to Postponed (maintainer needs more info). If we don't receive confirmation that there is work to do here the issue will be closed in another month. .

Thanks!

quietone’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

After about 9 months there has been no response so state that this is still needed. Therefor, closing.

If there is work to do here, then either re-open the issue or open a new issue and reference this one. If the choice is to use this issue then add a comment change make sure to change the issue status to 'Active'.

xjm’s picture

Issue tags: -

Saving credits according to our core issue credit guidelines. Thanks!

xjm’s picture

Issue tags: -Needs manual testing