Skip to content

Random notes

Chen Hui Jing edited this page Jan 12, 2024 · 1 revision
  • Add page title to body class, downside, if the page title is long, you get a ridiculous class name

    /**
     * Adds page title to body class
     */
    function interledger_preprocess_html(&$variables) {
      $request = \Drupal::request();
      $route_match = \Drupal::routeMatch();
      $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
    
      $css_title = \Drupal\Component\Utility\Html::cleanCssIdentifier($title);
      $variables['attributes']['class'][] = strtolower($css_title);
    }