2

I have a block template in Drupal 8. I want to wrap a section element around the block when it is in the right region.

Twig gives me this error.

Twig_Error_Syntax: The block 'content' has already been defined line

I understand the error, but I don't know how to fix it. Can anyone show me how to fix the code?

   {% set block_region_var = block_region %}
   {% if block_region_var == 'footer' %}
     <div{{ attributes.addClass(classes) }}>
       {% block content %}
         {{ content.body }}
       {% endblock %}
     </div>
   {% else %}
     <section id="{{ section_custom_id }}">
       <div{{ attributes.addClass(classes,'container') }}>
         {{ title_suffix }}
         {% block content %}
           {{ content.body }}
         {% endblock %}
       </div>
     </section>
   {% endif %}
2

You can remove all twig blocks from the template; that is, remove all of these lines:

{% block content %}
{% endblock %}

This defines a Twig Block, which could be used to extend this template, but this doesn't make sense anymore after you have implemented the condition.

| improve this answer | |
1
  1. Write a block preprocessing function in the .theme file and check the block region as in the following code.

     function THEMENAME_preprocess_block(&$variables ){
       $region = $variables['elements']['#configuration']['region'];
       if($region == "right"){  // please use specific region value
         $variables['right_region'] = TRUE;
       }
     }
    
  2. In block.html.twig, verify right_region is set to TRUE; then, add the wrapper class.

    {% if right_region %}
      // Add the wrapper class.
    {% else %}
      //
    {% endif %}
    
| improve this answer | |
1

For future developers who encounter same issue as me. I fixed it like this

{% if condition %}
    <div class="simple block">{% block content %}{% endblock %}</div>
{% else %}
    {% include 'extended_block.twig' %}
{% endif %}

// extended_block.twig with a bit different html structure
<div class="something">
    <div class="something2">
        {% block content %}{% endblock %}
    <div>
</div>
| improve this answer | |
0

Although this might not exactly fit OP's needs I'd like to highlight Twig extends to only override one certain {% block %} to get rid of that error.

In Twig blocks are overridable portions. You can simply extend one {% block %} in a child template and everything else in the parent template stays untouched. You also can simply reference the parent's {{ content }} by calling {{ parent() }}.

themes/custom/MYTHEME/templates/paragraph.html.twig:

{# extends and include paths always start inside Drupal web root #}
{% extends "modules/contrib/paragraphs/templates/paragraph.html.twig" %}

{# And now you only extend the content block of the parent template #}
{% block content %}
  {% if neat_grid %}
    <div class="neat-container">
      <div class="neat-row">
        <div class="inner">
          {{ parent() }}
        </div>
      </div>
    </div>
  {% else %}
    {{ parent() }}
  {% endif %}
{% endblock %}
| improve this answer | |
0

This is the way I would do it:

{% block content %}
  {% set block_region_var = block_region %}
   {% if block_region_var == 'footer' %}
     <div{{ attributes.addClass(classes) }}>
         {{ content.body }}
     </div>
   {% else %}
     <section id="{{ section_custom_id }}">
       <div{{ attributes.addClass(classes,'container') }}>
         {{ title_suffix }}
           {{ content.body }}
       </div>
     </section>
   {% endif %}
{% endblock %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.