diff --git a/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php b/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php index b1be143718..a8c4a82194 100644 --- a/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php +++ b/core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php @@ -42,13 +42,13 @@ public function testCssClassCleaning() { $view = Views::getview('test_preprocess'); $build = $view->buildRenderable(); $renderer->renderRoot($build); - $this->assertContains('class="entity-test__default', (string) $build['#markup']); + $this->assertContains('class="entity-test--default entity-test__default', (string) $build['#markup']); $view->destroy(); $view->setDisplay('display_2'); $build = $view->buildRenderable(); $renderer->renderRoot($build); - $this->assertContains('class="entity-test__default and-another-class', (string) $build['#markup']); + $this->assertContains('class="entity-test--default and-another-class entity-test__default', (string) $build['#markup']); } } diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 9321da75ac..482131f522 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -34,10 +34,14 @@ function template_preprocess_views_view(&$variables) { $css_class = $view->display_handler->getOption('css_class'); if (!empty($css_class)) { - $css_classes = explode(' ', strip_tags($css_class)); - foreach ($css_classes as $class) { - $variables['attributes']['class'][] = Html::cleanCssIdentifier($class); - } + // Keep backwards compatibility of old sanitised classes and variables. + $variables['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class); + $variables['attributes']['class'][] = $variables['css_class']; + // Add properly sanitised classes. + $css_classes = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', explode(' ', $css_class)); + // Merge the BC classes with the properly sanitised classes and remove + // duplicates. + $variables['attributes']['class'] = array_unique(array_merge($variables['attributes']['class'], $css_classes)); } // contextual_preprocess() only works on render elements, and since this theme