I have a twig template where I am displaying certain details based on the type of person.
But the condition is not working, I'm just wondering what is wrong with the IF clause?
{% if field_person_type == 'XXXXXXX' %}{{ (content.field_position) }}, {{ (content.field_unit) }}
{% else %} {{ (content.field_position) }}, {{ (content.field_institution) }} {% endif %}
And the content is defined below
Position field_position Text Text field
Person Type field_person_type Term reference Check boxes/radio buttons
Unit field_unit Text Text field
Institution field_institution Term reference Check boxes/radio buttons
When I use dump(field_person_type), it shows following
ARRAY(1) {
[0]=> ARRAY(2) {
["TID"]=> STRING(2) "40"
["TAXONOMY_TERM"]=> OBJECT(STDCLASS)#179 (8) {
["TID"]=> STRING(2) "40"
["VID"]=> STRING(1) "5"
["NAME"]=> STRING(7) "XXXXXXX"
["DESCRIPTION"]=> STRING(0) ""
["FORMAT"]=> STRING(2) "21"
["WEIGHT"]=> STRING(1) "2"
["VOCABULARY_MACHINE_NAME"]=> STRING(11) "PERSON_TYPE"
["PATH"]=> ARRAY(1) {
["PATHAUTO"]=> STRING(1) "1"
}
}
}
}
Try this field_person_type.0.taxonomy_term.name
Twig cannot access the name magically, you have to access it like you would access an array.
As the dump states: Your field (field_person_type) is an array. Inside you have [0] which is also an array with 2 entries. The "TAXONOMY_TERM" inside is an object so should be accessed as an object.
The following should give you your result
field_person_type[0]["TAXONOMY_TERM"].NAME
Related
This is my first question on stackoverflow.
I have a problem with ACF and Timber. I have created an ACF group like this :
My acf group
I want to check if type_pre-footer = XXX,so in my Twig template I have made this :
{% if post.meta('pre-footer').type_pre-footer == 'Titre et liens' %}
<section class="join-us">
<img src="https://picsum.photos/1800/537" class="alwaysgray" alt="">
<div class="top-right">
<h5>Rejoignez-nous !</h5>
<ul>
<li>{{ macros.svg('arrow','12','as--white', assetsDir) }}
Offres d'emploi</li>
<li>{{ macros.svg('arrow','12','as--white', assetsDir) }}
Candidature spontanée</li>
<li>{{ macros.svg('arrow','12','as--white', assetsDir) }}
Recrutement d'équipes</li>
</ul>
</div>
...
But it's not working, I have tried to display post.meta('pre-footer').type_pre-footer with a {{dump()}, but it return 'int(0)'. When I try to display only post.meta('pre-footer'), it return an array :
array(3) {
["type_pre-footer"]=> string(14) "Titre et liens"
["titreliens"]=> array(3) { ["titre"]=> string(0) "" ["liens"]=> bool(false) ["image"]=> string(0) "" }
["articles"]=> array(2) { ["titre"]=> string(0) "" ["articles"]=> bool(false) } }
have you any idea about how to solve it ?
if you need more logs, you can ask me, thanks
I'm trying to get a parent level custom taxonomy category name to output to an h6 tag, and I also need to get the child category name and output it in an h5 tag. I thought it would be simple by just accessing the index of the get_terms array, but the return doesn't seem to be consistent. Any suggestions would be greatly apprececiated. Is there a more efficient way to get a parent category, and a child category for a post?
$terms = get_the_terms( $post->ID , 'news-categories' );
if(count($terms) > 1) {
echo '<h5><i class="fas fa-tag"></i> ' . $terms[0]->name . '</h5>';
echo '<h6><i class="fas fa-tag"></i> ' . $terms[1]->name . '</h6>';
}
Here's a var_dump of the output of terms
array(2) {
[0]=>
object(WP_Term)#2985 (10) {
["term_id"]=>
int(45)
["name"]=>
string(17) "Project Spotlight"
["slug"]=>
string(17) "project-spotlight"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(45)
["taxonomy"]=>
string(15) "news-categories"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(87)
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Term)#2965 (10) {
["term_id"]=>
int(47)
["name"]=>
string(22) "Workforce Preparedness"
["slug"]=>
string(22) "workforce-preparedness"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(47)
["taxonomy"]=>
string(15) "news-categories"
["description"]=>
string(0) ""
["parent"]=>
int(45)
["count"]=>
int(37)
["filter"]=>
string(3) "raw"
}
}
Just as a note "Project Spotlight" should always show as the parent and "Workforce Preparedness" should return as the child, however, it's inconsistent on some posts.
Trying to retrieve all the data from a content type (the Article content type from the core installation).
I think I have managed to get the records, but I can't access the properties.
This is what I have in the controller
$query = \Drupal::entityQuery('node');
$query->condition('type', 'Article');
$items = $query->execute();
When I try to dump the items I'm getting
array(2) {
[3]=>
string(1) "2"
[4]=>
string(1) "3"
}
There is 2 results and I have 2 articles so I think this is the good way to get theses articles. But then in my twig templates I don't know how to call the properties.
This is empty result
{% for article in items %}
<li>{{article.body}}</li>
{% endfor %}
Tried with .title and .name too.
Actually I think that what I'm getting here are just the articles ids but not the properties, so I guess I miss something again here.
Thanks for the assistance
EDIT: Also tried this, but it makes it crash
$items_ids = $query->execute();
$items = Article::loadMultiple($items_ids);
Ended up by find out what was wrong with that, I needed to use Node for the load in controller and then .field.value in templates to render.
Controller
$query = \Drupal::entityQuery('node');
$query->condition('type', 'Article');
$items_ids = $query->execute();
$items = Node::loadMultiple($items_ids);
Twig
{% for article in items %}
<li>{{article.title.value}}</li>
{% endfor %}
I want to add a class to the <a>-Tag of a Field that consists of a URL-link and a link text (it's a field of type "Link") and the name of the field is content.field_c_button_link
So with twig in my HTML-File I want to have something like this:
{{ content.field_c_button_link.0.addClass('button blue') }}
How can I add a class properly?
Why not piece the anchor tag together manually? That way you have complete control over everything. Something like this in your template
{{content.field_url.0['#title']}}
Ok, this is horrible but it's the only way I found to get this to work:
If you look at the default drupal build array for your link you should see that content.field_c_button_link.0 is an array(4)
'#type' => string(4) "link"
'#title' => string(15) "Big Blue Button"
'#options' => array(0)
'#url' => object Drupal\Core\Url(11)
So, to set classes directly on the <a> tag we have to load '#options' (which is presently empty) with the right setup of subarrays
'#options' => array(1)
'attributes' => array(1)
'class' => array(2)
string(6) "button"
string(4) "blue"
The only way I could find to do this within twig was to use a series of temps and merging them with the original array because twig wouldn't parse anything else I tried:
{% set temp = {'attributes': {'class': ['button','blue']}} %}
{% set temp2 = content.field_c_button_link.0 %}
{% set temp2 = temp2|merge({'#options': temp}) %}
{% set temp3 = content.field_c_button_link|without('0') %}
{% set temp3 = temp3|merge({'0': temp2}) %}
{% set content = content|merge({'field_c_button_link': temp3}) %}
Note the |without which is a Drupal/twig filter. I had to use it to remove the empty '0' element to avoid having the link print twice.
Please tell me there is an easier way.
I have a problem with twig. I need to use nested variables such as (account is an object)
{{ likes_array.[account.idOnSn].lifetime }}
This is a result of print_r(likes_array)
array(2) {
["tanitweb"]=> array(2) {
["lifetime"]=> int(1420)
["daily"]=> int(0)
}
["fstjuniorentreprise"]=> array(2) {
["lifetime"]=> int(3310)
["daily"]=> int(3310)
}
}
I get this exception
Expected name or number in StatsDotTnMainBundle:Default:acceuil.php.twig at line 188
Use index notation, with square brackets:
{{ likes_array[account.idOnSn].lifetime }}