Unverified Commit 35d3174d authored by alexpott's avatar alexpott

Issue #2849954 by acbramley, criz, vaplas, arshadcn, Munavijayalakshmi,...

Issue #2849954 by acbramley, criz, vaplas, arshadcn, Munavijayalakshmi, alexpott, Lendude, dawehner, xjm, jibran: Double underscores are not preserved in main views CSS classes defined in views UI
parent 99f215b1
langcode: en
status: true
dependencies: { }
id: test_preprocess
label: ''
module: views
description: ''
tag: ''
base_table: entity_test
base_field: nid
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: tag
exposed_form:
type: basic
sorts:
id:
table: entity_test
id: id
field: id
plugin_id: standard
entity_type: entity_test
entity_field: id
order: desc
pager:
type: full
options:
items_per_page: 5
style:
type: default
row:
type: 'entity:entity_test'
css_class: 'entity-test__default'
display_plugin: default
display_title: Master
id: default
position: 0
display_2:
display_options:
access:
type: none
cache:
type: tag
exposed_form:
type: basic
sorts:
id:
table: entity_test
id: id
field: id
plugin_id: standard
entity_type: entity_test
entity_field: id
order: desc
pager:
type: full
options:
items_per_page: 5
style:
type: default
row:
type: 'entity:entity_test'
css_class: 'entity-test__default and_another-class'
display_plugin: default
display_title: Alternate
id: display_2
<?php
namespace Drupal\Tests\views\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\views\Views;
/**
* Tests the preprocessing functionality in views.theme.inc.
*
* @group views
*/
class ViewsPreprocessTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = ['test_preprocess'];
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test', 'user', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp();
$this->installEntitySchema('entity_test');
}
/**
* Tests css classes on displays are cleaned correctly.
*/
public function testCssClassCleaning() {
\Drupal::service('theme_handler')->install(['test_theme']);
$this->config('system.theme')->set('default', 'test_theme')->save();
$entity = EntityTest::create();
$entity->save();
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$view = Views::getview('test_preprocess');
$build = $view->buildRenderable();
$renderer->renderRoot($build);
$this->assertContains('class="entity-test--default entity-test__default', (string) $build['#markup']);
$view->destroy();
$view->setDisplay('display_2');
$build = $view->buildRenderable();
$renderer->renderRoot($build);
$markup = (string) $build['#markup'];
$this->assertContains('css_class: entity-test--default and-another-class entity-test__default', $markup);
$this->assertContains('attributes: class="entity-test--default and-another-class entity-test__default', $markup);
}
}
......@@ -34,8 +34,17 @@ function template_preprocess_views_view(&$variables) {
$css_class = $view->display_handler->getOption('css_class');
if (!empty($css_class)) {
$variables['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class);
$variables['attributes']['class'][] = $variables['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);
}
// contextual_preprocess() only works on render elements, and since this theme
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment