Verified Commit 3923a9f8 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #2977950 by Lendude, smustgrave, lauriii: Move the bc layer for views UI...

Issue #2977950 by Lendude, smustgrave, lauriii: Move the bc layer for views UI CSS classes from views to stable9
parent 02c07301
Loading
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -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);
  }

  /**
+2 −9
Original line number Diff line number Diff line
@@ -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
+17 −0
Original line number Diff line number Diff line
@@ -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));
  }
}