TransWikia.com

How can I split the body field to insert content between the first and the second paragraph?

Drupal Answers Asked on December 17, 2021

I have a node which shows content from the body field. I want to add a another paragraph or a block between between the first and the second paragraph, after the first </p> and before the second <p> tag.

But in my template to output the body there’s only {{ content.body }}.

Drupal 7 had something like block in block. How can I do the same in Drupal 8?

One Answer

I resolved my issue, using solution below

    {% set Paragraphs = node.body.value|split('</p>') %}
  {% set paragraphHalf = (Paragraphs|length / 2)|round %}
  
  {% if paragraphHalf %}
    {% for paragraph in Paragraphs %}
      {% if paragraphHalf == 1 or loop.index % paragraphHalf == 1 -%}
        
      {% endif %}
      {{ paragraph|raw }}

      {% if loop.last or loop.index % paragraphHalf == 0 -%}
      {{ insert item }}
      {% endif %}

    {% endfor %}

  {% endif %}

Answered by pr1859 on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP