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.

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
CAPTCHA
 ooo        ooooo   oooooooooooo                            oooooooo  ooooo     ooo 
`88. .888' d'""""""d888' dP""""""" `888' `8'
888b d'888 .888P .ooooo. oooo oooo d88888b. 888 8
8 Y88. .P 888 d888' d88' `88b `888 `888 `Y88b 888 8
8 `888' 888 .888P 888ooo888 888 888 ]88 888 8
8 Y 888 d888' .P 888 .o 888 888 o. .88P `88. .8'
o8o o888o .8888888888P `Y8bod8P' `V88V"V8P' `8bd88P' `YbodP'


Enter the code depicted in ASCII art style.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.