Score:0

Extending block, a block with a paragraph field, and 3 paragraph fields... how to loop through the paragraphs from the block extension

cn flag

This is what I have:

I have three twig files:

  1. block--paragraph-images.html.twig (which extends block.html.twig)
  2. field--field-paragraph-image-block.html.twig (the paragraph field which is located in the custom block type, block--paragraph-images.html.twig.)
  3. paragraph--field-paragraph-image.html.twig (which houses three fields: field_paragraph_image, field_paragraph_title, and field_paragraph_text).

I can access the individual fields in the paragraph--field-paragraph-image.html.twig from the block--paragraph-images.html.twig, but it only shows the first entry. I need to loop through the paragraph fields in the field--field-paragraph-image-block.html.twig. I'm trying to execute everything from the block--paragraph-images.html.twig to keep the files more manageable. Is this doable?

What I have below only shows the first index in the array. If I uncomment the loop statement, it shows around 12 each of the first index in the array. What I need is each paragraph index to show only once in a grid.

0, image, title, text

1, image, title, text

2, image, title, text

Any help and direction is appreciated.

{% block content %}

    {# block fields #}
    {% set paragraph_count = content.field_paragraph_image_block['#items']|length %}
    {% set paragraph_count_css = 'grid-cols-' ~ paragraph_count %}

    {# paragraph fields #}
    {% set paragraph_title = content.field_paragraph_image_block.0["#paragraph"].field_paragraph_title.value %}
    {% set paragraph_text = content.field_paragraph_image_block.0["#paragraph"].field_paragraph_text.value %}
    {% set media_id = content.field_paragraph_image_block.0["#paragraph"].field_paragraph_image.target_id %}
    
    <div class="block__content image-paragraph mb-0 grid gap-4  {{ paragraph_count_css }} " >
        {% set paragraph_classes = [
            'paragraph-image-effect',
            'image-count-' ~ paragraph_count,
        ] | sort | join(' ') | trim %}

       {# {% for item in content.field_paragraph_image_block %} #}
            <div class="{{ paragraph_classes }}">
                    {{ drupal_entity('media', media_id) }}
                    {{ paragraph_title }}
                    {{ paragraph_text }}
            </div>
       {# {% endfor %} #}
    </div>

{% endblock %}
Score:1
cn flag

Found the solution to this issue. You can have just one twig file, the custom block twig file, and access the fields inside the paragraphs.

Move the paragraph variables to inside the for loop, and replace the variables like this:

old code

["#paragraph"].field_paragraph_title.value

new code

item.entity.field_paragraph_title.value

and replace the for loop like this:

old code

{% for item in content.field_paragraph_image_block %}

new code

{% for item in content.field_paragraph_image_block['#items'] %}

Here's the new code in block--paragraph-images.html.twig, no other twigs are needed:

{% block content %}
    {# block fields #}
    {% set paragraph_count = content.field_paragraph_image_block['#items']|length %}
    {% set paragraph_count_css = 'grid-cols-' ~ paragraph_count %}

   <div class="block__content image-paragraph mb-0 grid gap-4  {{ paragraph_count_css }} " >

    {% for item in content.field_paragraph_image_block['#items'] %}

        {# paragraph fields #}  
        {% set paragraph_title = item.entity.field_paragraph_title.value %}
        {% set paragraph_text = item.entity.field_paragraph_text.value %}
        {% set media_id = item.entity.field_paragraph_image.target_id %}

        {% set paragraph_classes  = [
            'paragraph-image-effect',
            'image-count-' ~ paragraph_count,
        ] | sort | join(' ') | trim %}

            <div class="{{ paragraph_classes }}">
               {{ drupal_entity('media', media_id, 'original_size') }}
               {{ paragraph_title }}
               {{ paragraph_text }}
            </div>
     {% endfor %}
    </div>
{% endblock %}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.