Taxonomies
Taxonomies are the main way to categorize content in Drupal.
Get Taxonomy Term Names
Method 1 - Using entityTypeManager
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
'grafica_ambito', // The taxonomy term vocabulary machine name.
0, // The "tid" of parent using "0" to get all.
1, // Get only 1st level.
TRUE // Get full load of taxonomy term entity.
);
$results = [];
foreach ($tree as $term) {
$results[] = $term->getName();
}Method 2 - Using EntityQuery
$results = \Drupal::entityQuery('taxonomy_term')
->condition('vid', "TAXONOMY_TERM")
->sort('FIELD')
->execute();
$term_names_array = [];
$term = \Drupal\taxonomy\Entity\Term::load($result);
$term_names_array[] = $term->getName();Method 3 - Same as method 2 but using entityTypeManager
Last updated