Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 UTC on 18 March 2024, to get $100 off your ticket.
By d8v15 on
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
This works for me (Drupal 9 -
This worked for me
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