I have a custom field on my taxonomy that I would like to access in template_preprocess_html(). How do I get the current taxonomy id, in Drupal 8, on a taxonomy listing page?
1 Answer
First, check if the route is for a taxonomy term; then, get the term ID.
if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical') {
$term_id = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
}
-
Thank you, this works perfectly. Am I correct in assuming that this is getting the id from route/url parameters? (Just trying to understand better)– JaneyFeb 22, 2016 at 9:49
-
Yes, that is correct, but the id has not to be in the url. The condition is, that the url matches the route of the taxonomy term. You can configure this for example as /term/label. Or you can put anything in "URL alias" of the term.– 4uk4Feb 22, 2016 at 10:26