Add class to body based on path
<?php
/**
 * @file
 * Functions to support theming in the custom theme.
 */
use Drupal\Core\Url;
/**
 * Implements templatepreprocesshtml().
 * @param array $variables
 */
function custompreprocesshtml(&$variables) {
  $currentpath = \Drupal::service('path.current')->getPath();
  $pathalias = \Drupal::service('path.aliasmanager')->getAliasByPath($currentpath);
  $pathalias = ltrim($pathalias, '/');
  $variables['attributes']['class'][] = 'path-' . \Drupal\Component\Utility\Html::cleanCssIdentifier($path_alias);
  }
//kint($variables);
}
Written by jasloe
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
 #Drupal php 
Authors
Related Tags
#drupal php
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#