For theming purpose, I would like to add in body class the vocabulary name (or ID) of the current taxonomy. I know how to add a class based on the taxonomy term ID.
For example, I have the following vocabulary (Color).
- red (term id = 1)
- green (term id = 2)
- yellow (term id = 3)
I know how to get <body class="term-id-1">
. I would like to get <body class="vocabulary-id-1">
or <body class="vocabulary-name-color">
.
I've tried the following code.
function mytheme_preprocess_html(&$variables) {
// Works
if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical') {
$variables['term_id'] = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
}
// doesn't work
if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_vocabulary.canonical') {
$variables['vocabulary_id'] = \Drupal::routeMatch()->getRawParameter('taxonomy_vocabulary');
}
}
As said in the comments, it doesn't work, and I am not able to use the vocabulary name, or its ID as CSS class to add to the <body>
tag.
How can I achieve this?