Theme colour switcher

Add body classes per group content type - Drupal 8 & 9

The following preprocess function will give you unique classes on the body tag for each Group content type (from the Group module). It adds two classes, one that includes the Group type that the content is in as well as the Group content type (e.g. group-content--client-group--node-page); and one with just the Group content type (e.g. group-content--node-page).

Place this code in your theme's .theme file and replace THEMENAME with the name of your theme.

/**
* Prepares variables for the html.html.twig template.
*/
function THEMENAME_preprocess_html(&$variables) {
  // Add body classes based on group content type
  if ($group_content = \Drupal::routeMatch()->getParameter('group_content')) {
    $gc_full = str_replace('_', '--', $group_content->bundle());
    $gc_short = explode("_", $group_content->bundle())[1];

    $variables['attributes']['class'][] = 'group-content--' . $gc_full;
    $variables['attributes']['class'][] = 'group-content--' . $gc_short;
  }
}

Tags:

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.