Last active
December 10, 2024 14:15
Revisions
-
davidjguru revised this gist
Jun 22, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -137,7 +137,7 @@ $results = []; foreach ($tree as $term) { // Check if has children or not to validate if is at last level. if (empty($manager->loadChildren($term->id()))) { $results[] = $term->getName(); } } ``` -
davidjguru revised this gist
Jun 22, 2021 . 1 changed file with 36 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ For all the next cases we will use the [loadTree() method](https://api.drupal.or And this method will return an array of terms objects that are the children of the vocabulary $vid. We're gonna to play extracting terms and then processing the values cutting some levels (up or down) in order to get only the terms from the desired levels. ## 1- Load Taxonomy Terms by Name only first level ```php // Load the taxonomy tree using values. @@ -25,12 +25,35 @@ $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( $results = []; foreach ($tree as $term) { $results[] = $term->getName(); } ``` ## 2- Load Taxonomy Terms by Name getting the first level from a whole taxonomy terms tree ```php // Load the whole taxonomy tree using values. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); $taxonomy_tree = $manager->loadTree( 'vocabulary_name', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all. NULL, // Get all available levels. TRUE // Get full load of taxonomy term entity. ); $results_terms = []; // By now we need only the first level. foreach ($taxonomy_tree as $term) { // Filtering asking for parents first level hasn't got any parent. if(empty($manager->loadParents($term->id()))) { // We need a normalized version of the string without accents nor diacritics $results_terms[] = preg_replace('/[\x{0300}-\x{036f}]/u', "", Normalizer::normalize($term->getName() , Normalizer::FORM_D)); } } ``` ## 3- Load Taxonomy Terms by Name first and second level ```php // Load the taxonomy tree using values. @@ -44,15 +67,16 @@ $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( $results = []; foreach ($tree as $term) { $results[] = $term->getName(); } ``` ## 4- Getting Taxonomy Terms only the second level ```php // Load the taxonomy tree using values. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); $tree = $manager->loadTree( 'some_vocabulary', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all. 2, // Get terms from 1st and 2nd levels. @@ -62,14 +86,14 @@ $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( $results = []; foreach ($tree as $term) { // We get 1st and 2nd levels so we check parents cause only 2nd level has parents. if (!empty($manager->loadParents($term->id()))) { $results[] = $term->getName(); } } ``` ## 5- Getting Taxonomy Terms only second and third level ```php // Getting first the entity type manager service. @@ -88,13 +112,13 @@ $results = []; foreach ($tree as $term) { // We want only 2nd and 3rd levels so we check parents cause 1st level does not have parents. if (!empty($manager->loadParents($term->id()))) { $results[] = $term->getName(); } } ``` ## 6- Getting Taxonomy Terms only the last level ```php // Getting first the entity type manager service. -
davidjguru revised this gist
Jun 21, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,7 +62,7 @@ $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( $results = []; foreach ($tree as $term) { // We get 1st and 2nd levels, also we check parents cause only 2nd level has parents. if (!empty($manager->loadParents($term->id()))) { $results[] = $term->getTerm(); } -
davidjguru revised this gist
Jun 21, 2021 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -83,12 +83,12 @@ $tree = $manager->loadTree( TRUE // Get full load of taxonomy term entity. ); $results = []; foreach ($tree as $term) { // We want only 2nd and 3rd levels so we check parents cause 1st level does not have parents. if (!empty($manager->loadParents($term->id()))) { $results[] = $term->getTerm(); } } ``` @@ -108,12 +108,12 @@ $tree = $manager->loadTree( TRUE // Get full load of taxonomy term entity. ); $results = []; foreach ($tree as $term) { // Check if has children or not to validate if is at last level. if (empty($manager->loadChildren($term->id()))) { $results[] = $term->getTerm(); } } ``` -
davidjguru revised this gist
Jun 21, 2021 . 2 changed files with 18 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ ## Introduction In this snippet I've gathered some examples and cases about how to get taxonomy terms programmatically in Drupal, using always the same method and the same mechanics: get terms and then processing it in order to get only the required under certain criteria. **Author** ---------------- * David Rodríguez, [@davidjguru](https://twitter.com/davidjguru). * Contact at davidjguru@gmail.com * Website: [https://therussianlullaby.com](https://therussianlullaby.com) * Drupal.org profile: [https://www.drupal.org/u/davidjguru](https://www.drupal.org/u/davidjguru) * Linkedin profile: [https://www.linkedin.com/in/davidjguru](https://www.linkedin.com/in/davidjguru/) * Sketchbook, english: [https://davidjguru.github.io](https://davidjguru.github.io) * Medium, spanish: [https://medium.com/@davidjguru](https://medium.com/@davidjguru) * Snippets: [https://gitlab.com/users/davidjguru/snippets](https://gitlab.com/users/davidjguru/snippets) * Dev.to: [https://dev.to/davidjguru](https://dev.to/davidjguru) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ ## 0 - The loadTree() method For all the next cases we will use the [loadTree() method](https://api.drupal.org/api/drupal/core%21modules%21taxonomy%21src%21TermStorage.php/function/TermStorage%3A%3AloadTree/8.2.x), from the [TermStorage class](https://api.drupal.org/api/drupal/core%21modules%21taxonomy%21src%21TermStorage.php/class/TermStorage/8.2.x) in Drupal. This method finds all terms in a given vocabulary ID, using the next parameters: - string $vid: Vocabulary ID to retrieve terms for. - int $parent: The term ID under which to generate the tree. If 0, generate the tree for the entire vocabulary. - int $max_depth: The number of levels of the tree to return. Leave NULL to return all levels. -
davidjguru revised this gist
Jun 21, 2021 . 1 changed file with 22 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,19 @@ ## 0 - The loadTree() method For all the next cases we will use the loadTree() method, from the TermStorage class in Drupal. This method finds all terms in a given vocabulary ID, using the next parameters: - string $vid: Vocabulary ID to retrieve terms for. - int $parent: The term ID under which to generate the tree. If 0, generate the tree for the entire vocabulary. - int $max_depth: The number of levels of the tree to return. Leave NULL to return all levels. - bool $load_entities: If TRUE, a full entity load will occur on the term objects. Otherwise they are partial objects queried directly from the {taxonomy_term_data} table to save execution time and memory consumption when listing large numbers of terms. Defaults to FALSE. And this method will return an array of terms objects that are the children of the vocabulary $vid. We're gonna to play extracting terms and then processing the values cutting some levels (up or down) in order to get only the terms from the desired levels. ## 1- Load Taxonomy Terms only first level ```php // Load the taxonomy tree using values. $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( 'vocabulary_name', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all. @@ -20,10 +32,10 @@ foreach ($tree as $term) { ## 2- Load Taxonomy Terms first and second level ```php // Load the taxonomy tree using values. $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( 'some_vocabulary', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all. 2, // Get terms from 1st and 2nd levels. TRUE // Get full load of taxonomy term entity. ); @@ -38,12 +50,10 @@ foreach ($tree as $term) { ## 3- Getting Taxonomy Terms only the second level ```php // Load the taxonomy tree using values. $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( 'some_vocabulary', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all. 2, // Get terms from 1st and 2nd levels. TRUE // Get full load of taxonomy term entity. ); @@ -61,7 +71,7 @@ foreach ($tree as $term) { ## 4- Getting Taxonomy Terms only second and third level ```php // Getting first the entity type manager service. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); // Load the taxonomy tree using values. @@ -86,7 +96,7 @@ foreach ($tree as $term) { ## 5- Getting Taxonomy Terms only the last level ```php // Getting first the entity type manager service. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); // Load the taxonomy tree using values. -
davidjguru created this gist
Jun 21, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,108 @@ ## 1- Load Taxonomy Terms only first level ```php // Load the taxonomy tree with special parameters. $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( 'vocabulary_name', // 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->getTerm(); } ``` ## 2- Load Taxonomy Terms first and second level ```php // Load the taxonomy tree with special parameters. $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( 'some_vocabulary', // This is your taxonomy term vocabulary (machine name). 0, // This is "tid" of parent. Set "0" to get all. 2, // Get terms from 1st and 2nd levels. TRUE // Get full load of taxonomy term entity. ); $results = []; foreach ($tree as $term) { $results[] = $term->getTerm(); } ``` ## 3- Getting Taxonomy Terms only the second level ```php // Get entity type manager service. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); // Load the taxonomy tree with special parameters. $tree = $manager->loadTree( 'some_vocabulary', // This is your taxonomy term vocabulary (machine name). 0, // This is "tid" of parent. Set "0" to get all. 2, // Get terms from 1st and 2nd levels. TRUE // Get full load of taxonomy term entity. ); $results = []; foreach ($tree as $term) { // We get 1st and 2nd levels, also we check parents (only 2nd level has parents). if (!empty($manager->loadParents($term->id()))) { $results[] = $term->getTerm(); } } ``` ## 4- Getting Taxonomy Terms only second and third level ```php // Get entity type manager service. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); // Load the taxonomy tree using values. $tree = $manager->loadTree( 'some_vocabulary', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all. 3, // Get terms from 1st, 2nd and 3rd levels. TRUE // Get full load of taxonomy term entity. ); $result = []; foreach ($tree as $term) { // We want only 2nd and 3rd levels so we check parents cause 1st level does not have parents. if (!empty($manager->loadParents($term->id()))) { $result[] = $term->getTerm(); } } ``` ## 5- Getting Taxonomy Terms only the last level ```php // Get entity type manager service. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); // Load the taxonomy tree using values. $tree = $manager->loadTree( 'some_vocabulary', // The taxonomy term vocabulary machine name. 0, // The "tid" of parent using "0" to get all tree. NULL, // Get all levels. TRUE // Get full load of taxonomy term entity. ); $result = []; foreach ($tree as $term) { // Check if has children or not to validate if is at last level. if (empty($manager->loadChildren($term->id()))) { $result[] = $term->getTerm(); } } ```