1
function THEMENAME_preprocess_views_view(&$variables){
        $view = $variables['view'];
      if($view->name == "YOUR_VIEW_NAME" && $view->current_display == "MACHINE_NAME_OF_VIEW"){
        $variables['attributes']['class'][] = 'page-'.$view->name;
    }

For some reason this does not work. Any ideas?

1 Answer 1

0

this one worked:

function THEMENAME_preprocess_html(&$variables) { 
  //view name as body class
  $variables['view_title'] = '';
  $route = \Drupal::routeMatch()->getRouteObject();
  if ($route) {
    $view_title = $route->getDefault('_title');
    $variables['attributes']['class'][] = 'view-' . Html::cleanCssIdentifier($view_title);
  }
}
2
  • What if the view title has an empty space? ex: Hello World
    – No Sssweat
    May 22, 2019 at 22:38
  • 1
    @NoSssweat, use the Html::cleanCssIdentifier() method. For example, 'view-' . Html::cleanCssIdentifier($view_title);. The method prepares a string to be used as a CSS identifier. It replaces spaces and everything.
    – Ivan Zugec
    Mar 24, 2022 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.