What is best way to retrieve Drupal 8 taxonomy terms and pass array to twig template?
Now I have:
In template.theme
function template_preprocess_page(&$variables) {
$variables['some_term'] = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('some_term', $parent = 0, $max_depth = NULL, $load_entities = FALSE);
}
Now in page.html.twig I can access {{ some_term }} array.
I feel that something is wrong about this. What is the best way to achieve this? I just need an array of selected taxonomy terms in page.html.twig. I want to use it to build custom HTML code.
Equivalent function in Wordpress is simple get_terms().
Your code seems to be OK.
Please check next:
Are you getting taxonomy tree array values in template_preprocess_page?
If not ($variables['some_term'] should not be empty array) check if you are loading correct vocabulary with "...->loadTree('machine_name_of_vocabulary'..."
If yes check what twig is getting in page.html.twig You can check with: {{ dump(some_term) }}. Probably you will need to process array to get only for example list of term names.
Did you cleared Drupal cache?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.