Looping through nested paragraphs on twig file (entity reference revisions list) in twig - twig

I have double nested paragraphs which I am trying to loop through on my twig file.
The paragraph structure is as follows:
SLIDER WRAPPER PARAGRAPH
reference slide paragraph (entity reference revisions)
SLIDE PARAGRAPH
title (text field)
reference component library (entity reference revisions)
In my wrapper twig file, I have been able to loop through the reference slide field on the wrapper paragraph and have rendered out the title of the slide, using the below code:
<div class="row">
<div class="col-sm-12">
{% for key, item in content.content.field_slide_reference if key|first != '#'%}
{{ item['#paragraph'].field_title[0].value|raw }}
{# WANT TO RENDER REFERENCE COMPONENT LIBRARY HERE, BELOW NOT WORKING #}
{{ item['#paragraph'].field_reference_component_library[0].value }}
{% endfor %}
</div>
</div>
Currently I'm receiving the following error:
USER ERROR: "TARGET_ID" IS AN INVALID RENDER ARRAY KEY IN DRUPAL\CORE\RENDER\ELEMENT::CHILDREN() (LINE 98 OF CORE/LIB/DRUPAL/CORE/RENDER/ELEMENT.PHP
Any idea on how I achieve this?

Related

Concatenate in an element acf fields or an objects

I'm trying to concatenate a Twig variable and an ACF options field but it doesn't work.
I have two footers and I want to select the correct one according to the correct page.
{% if post.slug == "page1" %}
{% set pageType = "pages" %}
{% elseif post.slug == "page2" %}
{% set pageType = "otherspages" %}
{% endif %}
<footer id="footer">
<h2>{{ options.footer_~pageType~_titre }}</h2>
<div>{{ options.footer_~pageType~_container }}<div>
</div>
The ACF fields are called footer_page_titre or footer_otherpage_titre depending on the footer I want to display
Thank you
Try constructing the field name first, for example with the twig format filter which formats a given string by replacing the placeholders, similar to sprintf, and then access the field value by in the options data array.
Construct the field name:
{% set footer_title = "footer_%s_title"|format(pageType) %}
Access the value by array key
<h2>{{ options[footer_title] }}</h2>

how to get content type in views-view.html.twig files | drupal 8

I have a view in which I show several content types so I need to filter some custom menu entries that I've made in "views-view--myview.html.twig" based on the content type that is shown in the current view.
I've used xdebug but cannot print the content type machine name in my view (I need the content type machine name, not the label!)
Please note that I don't want to do that in the views-view-fields template.
My question is: How can I get the content type (that is shown in a view) in the views-view.html.twig file?
If there is a preprocess function it would also help me!
in views-view.html.twig or overriden twig such as views-view--myview.html.twig you can do the following
get title of first row entity
{{rows[0]['#rows'][0]['#row']._entity.getTitle()}}
get type/bundle of first row entity
{{rows[0]['#rows'][0]['#row']._entity.bundle()}}
So to access all rows you probably need a loop
following checks the entity type/bundle for each row
{% for row in rows[0]['#rows'] %}
{% if row['#row']._entity.bundle() == 'page' %}
<div>its a page</div>
{% endif %}
{% endfor %}
But perhaps the best place to modify individual rows of a view is inside a specific display of a view such as views-view-unformatted.html.twig or views-view-unformatted--myview.html.twig
for comparison get title and type/bundle and looping through rows as above
{{rows[0]['content']['#row']._entity.getTitle()}}
{{rows[0]['content']['#row']._entity.bundle()}}
{% for row in rows %}
{% if row['content']['#row']._entity.bundle() == 'page' %}
<div>its a page</div>
{% endif %}
{% endfor %}

Set a property value in an existing twig object

I would like to define a twig object that contains two properties :
The first one is a short text
The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)
I first tried like this :
{% block principal %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
{% set a_form_help.help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
<p>And several lines of tips for this field</p>
{% endset %}
{% endblock %}
But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).
I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:
{% block principal %}
{% set tmp_help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
{% endset %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}
{{ dump(a_form_help) }}
{% endblock %}
I've used a a temporary variable since using the merge() filter would give the same result.
Is this workaroud a good practice or is there a better/cleaner way ?
Thanks in advance for any tip|advice ! :-)
There is no clean way to do it in twig.
Is this workaroud a good practice or is there a better/cleaner way ?
It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere

Twig loop.first not working

I have the following :
{% for field in fields -%}
<div class="item{% if loop.first %} active{% endif %}">
{{ field.content }}
</div>
{%- endfor %}
but it's adding the active class to every field wrapper, rather than just the first which is my intention.
Thanks Darkbee. User malfunction here. The code was in a nested template file ( Drupal ) and the loop I thought I was iterating through was actually in a higher level template file.

If statement in Twig (for Particle in Joomla)

Trying to edit a particle in Joomla -- I'm fairly new to twig and I'm trying to list information based on a selection in vertical tabs. I have an on-click refresh for my li edit that makes a tab "selected" and creates my internal url:
{% for item in particle.items %}
<li>
<a href="/Joomla/about-the-library/locations#{{ loop.index }}">{{ item.title|e }}
<img src="{{ url(item.image)|e }}" alt="{{ item.title|e }}">
<div class="g-flexslider-carousel-item-image-preview-icon"></div>
</a>
</li>
{% endfor %}
This is all well and good, but I ran into an issue when trying to display the data associated with the selected item. I tried to create a variable to check my items against, but the variable doesn't seem to be coming back as an integer, and I've tried a few things:
{% set branch = app.request.pathinfo|trim('/Joomla/about-the-library/locations#') %}
{% if loop.index == branch %}
<div class="g-flexslider-showcase-item-content-container">
<div class="g-flexslider-showcase-item-image">
<img src="{{ url(item.image)|e }}" alt="{{ item.title|e }}">
</div>
Can anyone tell me what I'm doing incorrectly?
(I found that get current url in twig template? helped, but I'm not sure I'm using the answers provided correctly. I've also sifted through the Twig Documentation for a couple hours to no avail.)
[Nov 2016] - This is still what "my" code looks like for this section. It seems to just be a problem with this if statement, as the "else" statement (which I'm using for debugging purposes) keeps coming through.
{% for item in particle.items %}
{% if app.request.get('name') == item.URLname|e %}
<p>You have branched {{ item.title|e }} correctly. </p>

Resources