In Drupal 8, how would one retrieve a taxonomy by path/alias? The loadProperties method neither takes "path" or "alias"

i.e. the below throws error

      $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(
        [
          'path' => $result
        ]);

Thanks ~

Comments

golubovicm’s picture

This works for me (Drupal 9 -  for Drupal 8 'path_alias.manager' might have different name, 'path.alias_manager' or similar).

function getTagByAlias($alias) {
  $path = \Drupal::service('path_alias.manager')->getPathByAlias($alias);
  if ($path == $alias) {
    return NULL;
  }
  $params = Url::fromUri("internal:" . $path)->getRouteParameters();
  $entity_type = key($params);
  return \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);
}

Devashish Jangid’s picture

Use Drupal\taxonomy\Entity\Vocabulary;
use \Drupal\path_alias\AliasManager;

$vocabularies = Vocabulary::loadMultiple();
    // To get names:
foreach($vocabularies as $voc) {
    $voc_name = $voc->label();    
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
        ->loadByProperties(['vid' => $voc->id()]);
    foreach ($terms as $term) {
        $term_url = '';
        $term_id = $term->id();
        $aliasManager = \Drupal::service('path_alias.manager');
        $alias = $aliasManager->getAliasByPath('/taxonomy/term/'.$term_id);
        }
         
    }
}

Hope this will help