Wordpress Timber merge terms from two taxonomies - wordpress

I see that Timber will allow a merged list of terms from all taxonomies. But is there a way to merge a list from two taxomomies?
This code works for me:
{% for term in post.terms('topics') %}
This works, but loads only the first taxonomy listed:
{% for term in post.terms('topics','regions') %}
This fails:
{% for term in post.terms(array('topics','regions')) %}
In a merged list, preferably with a sort by alpha. The Timber docs give these instructions terms($tax = 'any', $merge = true) and show how one taxonomy can be specified (default is "all") but not how two might be specified.

#slam I think you just need to use Twig's array syntax to make this work:
{% for term in post.terms(['topics', 'regions']) %}
Give that a shot and lemme know!

Related

twig show all content raw like {{#equals language "en"}}

We're working on twig to merge multiple values. The idea is to have multiple twig templates each containing the value of it's language.
The problem is the values contain handlebars conditions like {{#equals language "nl_NL"}} that causes trouble in twig.
Is it possible to show all contents raw?

How to filter an integer using the sort in twig

I want my project to be filter by the score and I am using a elasticsearch. now, the score have two variables: matchingScore and personalityScore. this two is should be added in the twig to get the actual score. Now, I dont know how to sort an integer since this is not an array and the sorting in the twig is required an array. This is the sample code:
{% for provider in matches | sort matchingScore[provider.id] + personalityScore[provider.id]('desc') %}
{% if matchingScore[provider.id] + personalityScore[provider.id] >= score_criteria %}
Please Help me. thanks.

Drupal 8 get value of a custom field that has multiple value

I'm developing a site with Drupal 8. I made a custom content type, with a lot of fields; in particular I have a field called "field_categoria_del_libro_presen" that is linked to a taxonomy term. It's possible to specify more than one value. I used kint() to obtain the structure of the node. Here:
field_categoria_del_libro_presen
→array(2)
target_id
1
"1"
2
"4"
I attached the screenshot too.
screenshot
How can I get the value "1" and "4" that represent the terms of my taxonomy?
Thank you so much in advance.
Regards.
Valentina
ANOTHER QUESTION UNSOLVED:
how can I get the length of the array?
I found a solution. So I answer my question.
{{ node.field_categoria_del_libro_presen.0.target_id}}
{{ node.field_categoria_del_libro_presen.1.target_id}}
According to your self provided answer I guess you're talking about how to get the value in a twig template. Well generally speaking if you want to get the length of an array you can use the "length" filter. It looks like this:
{{ someArray|length }}
And regarding your original question, if the number of selected values varies and you want to display them all I would suggest using a for loop, it looks like this:
{% for arrayElement in someArray %}
{{ arrayElement.someKey }}
{% endfor %}
For more information look here: http://twig.sensiolabs.org/doc/tags/for.html
and here:
http://twig.sensiolabs.org/doc/filters/length.html

Dynamic variables in Twigs

In a form, I dynamically define the fields in a controller. Then in the TWIG I would like to use form_widget to output the fields.
The normal approach, when knowing the fields is like shown bellow:
{{ form_widget(form.field1) }}
In this case, we cannot know whether we will have a field1, field2, etc. on beforehand. Though we have the field names in a variable called key.
So what we would like to achieve is replace the hardcoded field1 by something dynamic.
You can do something like
{{ form_widget(form[key]) }}

Getting taxanomy value from URL

I'm setting up a search page by views exposed filter. And one of the field are filter by taxonomy term.
For example, when I search with the taxonomy term filter field, the URL is like below.
domain.com/search?subjects=69
Now I wish to get the value of the taxonomy (it's showing tid instead of value)
<?php
$idenity = $_GET['subjects'];
print $idenity;
?>
Anyway to get the value of the taxonomy value but not taxonomy id?
You didn't specify which version you're using so I'm assuming Drupal 7 because it's the latest stable version.
You can load the term with taxonomy_term_load():
<?php
$tid = intval($_GET['subjects']);
$term = taxonomy_term_load($tid)
print $term->name;
?>
Personally, I find that the comments on the Drupal API site are usually very helpful in understanding how to use functions that sound like they are relevant to my problem.

Resources