From 3923a9f87d173b49140c42e92d343e20f6b1a1ea Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Fri, 23 Feb 2024 11:49:44 +0100 Subject: [PATCH] Issue #2977950 by Lendude, smustgrave, lauriii: Move the bc layer for views UI CSS classes from views to stable9 --- .../tests/src/Kernel/ViewsPreprocessTest.php | 6 +++--- core/modules/views/views.theme.inc | 11 ++--------- core/themes/stable9/stable9.theme | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php b/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php index fb0d03ad4ddc..9b8a9ea5ff04 100644 --- a/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php +++ b/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php @@ -46,15 +46,15 @@ public function testCssClassCleaning() { $view = Views::getView('test_preprocess'); $build = $view->buildRenderable(); $renderer->renderRoot($build); - $this->assertStringContainsString('class="entity-test--default entity-test__default', (string) $build['#markup']); + $this->assertStringContainsString('class="entity-test__default', (string) $build['#markup']); $view->destroy(); $view->setDisplay('display_2'); $build = $view->buildRenderable(); $renderer->renderRoot($build); $markup = (string) $build['#markup']; - $this->assertStringContainsString('css_class: entity-test--default and-another-class entity-test__default', $markup); - $this->assertStringContainsString('attributes: class="entity-test--default and-another-class entity-test__default', $markup); + $this->assertStringContainsString('css_class: entity-test__default and-another-class', $markup); + $this->assertStringContainsString('attributes: class="entity-test__default and-another-class', $markup); } /** diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 58d34f16ae72..fe840b638cd1 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -35,17 +35,10 @@ function template_preprocess_views_view(&$variables) { $css_class = $view->display_handler->getOption('css_class'); if (!empty($css_class)) { - // Views uses its own sanitization method. This is preserved to keep - // backwards compatibility. - // @todo https://www.drupal.org/project/drupal/issues/2977950 Decide what to - // do with the backwards compatibility layer. - $bc_classes = explode(' ', preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class)); - // Sanitize the classes using the classes using the proper API. $sanitized_classes = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', explode(' ', $css_class)); - $view_classes = array_unique(array_merge($bc_classes, $sanitized_classes)); // Merge the view display classes into any existing classes if they exist. - $variables['attributes']['class'] = !empty($variables['attributes']['class']) ? array_merge($variables['attributes']['class'], $view_classes) : $view_classes; - $variables['css_class'] = implode(' ', $view_classes); + $variables['attributes']['class'] = !empty($variables['attributes']['class']) ? array_merge($variables['attributes']['class'], $sanitized_classes) : $sanitized_classes; + $variables['css_class'] = implode(' ', $sanitized_classes); } // contextual_preprocess() only works on render elements, and since this theme diff --git a/core/themes/stable9/stable9.theme b/core/themes/stable9/stable9.theme index d1b54033e0be..228e3ff0e95f 100644 --- a/core/themes/stable9/stable9.theme +++ b/core/themes/stable9/stable9.theme @@ -15,3 +15,20 @@ function stable9_preprocess_item_list__search_results(&$variables) { $variables['empty']['#tag'] = 'h3'; } } + +/** + * Implements hook_preprocess_views_view(). + * + * Adds BC classes that were previously added by the Views module. + */ +function stable9_preprocess_views_view(&$variables) { + if (!empty($variables['attributes']['class'])) { + $bc_classes = preg_replace('/[^a-zA-Z0-9- ]/', '-', $variables['attributes']['class']); + $variables['attributes']['class'] = array_merge($variables['attributes']['class'], $bc_classes); + } + if (!empty($variables['css_class'])) { + $existing_classes = explode(' ', $variables['css_class']); + $bc_classes = preg_replace('/[^a-zA-Z0-9- ]/', '-', $existing_classes); + $variables['css_class'] = implode(' ', array_merge($existing_classes, $bc_classes)); + } +} -- GitLab