2

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?

12

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) – Janey Feb 22 '16 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. – 4k4 Feb 22 '16 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.