Drupal Taxonomy

Drupal Get Taxonomy Term From Route

The process of loading taxonomy term from routematch for Drupal 8, 9 and 10 is as follows.

$tax = \Drupal::routeMatch()->getParameter('taxonomy_term');
if ($tax instanceof \Drupal\taxonomy\TermInterface) {
 // $tax
}

We got the taxonomy from the route with the following code.

$tax = \Drupal::routeMatch()->getParameter('taxonomy_term');

With the code below, we confirmed that the entity we loaded is a taxonomy.

Drupal 8 Taxonomy Term Load

To access taxonomy terms in Drupal 8, you must include the Term class in your project.

use Drupal\taxonomy\Entity\Term;

In order to load the taxonomy, you must send the tid to the term: load function.

$term = Term::load($tid);

The getName function is used to get the name of the taxonomy loaded with the term: load function.