How i get Taxonomy Term URL Path by tid and TID from Term Name in Drupal 8

For taxonomy term

$aliasManager = \Drupal::service('path_alias.manager');
$alias = $aliasManager->getAliasByPath('/taxonomy/term/'.$tidss);

 

For node 


$aliasManager = \Drupal::service('path_alias.manager');
$alias = $aliasManager->getAliasByPath('/node/'.$id);


Taxonomy term path alias in twig tpl

<a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_taxonomy_reference.entity.tid.value}) }}">
  <h2>{{ content.field_taxonomy_reference }}</h2>
</a>


To load a single term ID by term name and vocabulary in Drupal 8 you can use the following

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')

  ->loadByProperties(['name' => $term_name, 'vid' => 'job_category']);

$term = reset($term);

$term_id = $term->id();


1 comment:

Sapphire Solutions said...

Very interesting post...

How i get Taxonomy Term URL Path by tid in Drupal 8

Will definitely try it...

Drupal Development Company

Drupal Module Development Services

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...