Drupal 9 image field migration

Let's assume you have a paragraph SimpleBlock with Title+Text fields. You also have a node with field_paragraphs reference to SimpleBlock paragraph.
You can add any paragraph to field_paragraphs. But you can't set there a default value. For example if you want to display 3 default SimpleBlock if there are no references in field_paragraphs.
One of the solutions is to use "Paragraphs Library", but in this case you need to add this paragraph to every node.
But I want something automatic and to use the same styles from twig templates.

So, let's copy default node.html.twig to node--page--full.html.twig in our theme's folder. Add the code.       

  {% if node.field_paragraphs[0] is not empty %}
    {{ node.field_paragraphs }}
  {% else %}
    {% include [
      '@my_theme/custom/simple_block_defaults/default.html.twig',
      '@my_theme/custom/empty.html.twig'
      ] %}
  {% endif %}   

Create file @my_theme/custom/simple_block_defaults/default.html.twig with code:

{% set items = {
  '0': {
    'content': {
      '#type': 'inline_template',
      '#template': include('@my_theme/paragraph/paragraph--simple-block.html.twig', {'content': {
        'field_paragraph_title': 'Title 1',
        'field_paragraph_text': {
          '#markup': 'Text 1'
        },
      }})
    }},
  '1': {
    'content': {
      '#type': 'inline_template',
      '#template': include('@my_theme/paragraph/paragraph--simple-block.html.twig', {'content': {
        'field_paragraph_title': 'Title 2',
        'field_paragraph_text': {
          '#markup': 'Text 2'
        },
      }})
    }},
}
%}

{% include '@rt_base/field/field--field-simple-block.html.twig' with {'items': items} %}

In my case it was very useful, since I have multilingual defaults. And every language can have different amount of items which dont need to be translated in BO.

Коментувати

Вміст цього поля є приватним і не буде доступний широкому загалу.
  • Не дозволено жодних HTML теґів.