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

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

Related

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.

how to change value of select list on change of another select list in exposed filter

I have created two exposed filter with the same field District.
When I change the selected value of District field 1, value of list one is set to District field 2.
what should I do for achieving this for more clarification of the question I have image attached bellow.
Please see the image:
Did you try this Drupal module
https://www.drupal.org/project/views_dependent_filters
Try this module :
https://www.drupal.org/project/views_conditional
Also this post can helps : https://drupal.stackexchange.com/questions/174334/views-conditional-filter

Wordpress Timber merge terms from two taxonomies

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!

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]) }}

Drupal Custom CCK field with multiple child fields

Is there a way of creating a composite field that can have multiple values, with each value having another group of composite values?
E.g. we want to have this structure at the end:
Group 1 (unlimited number of groups)
Child field (unlimited children for each group)
Child field
...
Group 2
Child field
Child field
...
...
Is this possible at all for a custom module that defines a CCK field? If so, can someone push me in the right direction?
this is a very know and debated issue in the drupal world.
this feature is called cck "multigroup" and it looks it's pretty difficult to implement.
there are a lot of posts in the drupal forum about this, i suggest you to start here:
http://drupal.org/node/494100
it's a kind of "hidden" feature in the cck module. looking in the module directory, you will find instructions here:
cck/modules/content_multigroup/README.txt
Edit: Ongoing work on the multigroup module has moved to the experimental
CCK 3.0 branch.
in the meantime you can try to deal with it using the flexifield module
(but it's kinda hackish, i won't use it in production)
I ended up creating my own "Cost/Product" CCK compound field based on this excellent tutorial with example modules: http://poplarware.com/es/articles/cck_field_module
I haven't figured out yet how to pull out a specific sub-field, such as cost, in Views. I only get the entire ": $" compound.
For Drupal 7+, you probably want to check out the Field collection module, or Field group if multiples aren't necessary.
This issue is pretty old, but I just happened across it. I would think the way to handle it for now would be with a second cck type for the children, and the groups as a node_reference field. So you'd have:
MasterType
group field
unlimited per node
each one, a reference to a ChildType node
ChildType
child field
unlimited per node

Resources