1

i am having a custom template for rendering a medialink url from a paragraph media entity ref field. In production with twig cache activated, modifiying the media attached file , does not update the content making reference to that media, unless the render cache is emptied.

paragraph--link-media.html.twig

{% set classes = [
  'paragraph',
  'paragraph-link-media',
] %}
{% set link_classes = [] %}
{% set aria_label = "" %}
{% set aria_describedby = "" %}
{% if content.field_aria_title|field_value %}
  {% set aria_label =  content.field_aria_title|field_value|render %}
{% endif %}
{% if content.field_link_description|field_value %}
  {% set aria_describedby = "link-description-"~paragraph.id() %}
{% endif %}
{% if content.field_link_style|field_value %}
  {% set link_classes = link_classes|merge(['cta-link']) %}
  {% set classes = classes|merge([content.field_link_style.0["#options"].entity.field_class_name.value]) %}
{% endif %}
{% if content.field_link_extra_styles|field_value %}
  {% for extra_style in content.field_link_extra_styles|field_value %}
    {% set classes = classes|merge([extra_style["#options"].entity.field_class_name.value]) %}
  {% endfor %}
{% endif %}
{% if content.field_link_fa_style|field_value %}
  {% set classes = classes|merge(['link-fa']) %}
{% endif %}
{% block paragraph %}
  {% block content %}
    {% if content.field_link_style|field_value %}
      {% if content.field_cta_align|field_value %}
        {% set class_cta_align = content.field_cta_align|field_value %}
      {% else %}
        {% set class_cta_align = "cta-center" %}
      {% endif %}
      <div class="{{ class_cta_align }}">
    {% endif %}
    <span{{ attributes.addClass(classes) }}>
      {% set attributes_link = create_attribute() %}
      {% if aria_label != "" and content.field_link_description|field_value and not content.field_link_style|field_value %}
        {% set attributes_link = attributes_link.setAttribute('aria-label', aria_label).setAttribute('aria-describedby', aria_describedby) %}
      {% elseif content.field_link_description|field_value and not content.field_link_style|field_value %}
        {% set attributes_link = attributes_link.setAttribute('aria-describedby', aria_describedby) %}
      {% elseif aria_label != "" %}
        {% set attributes_link = attributes_link.setAttribute('aria-label',aria_label) %}
      {% endif %}

      {% if link_media.url|length>0 %}
        <a {{ attributes_link.addClass(link_classes) }} href="{{ link_media.url }}">
      {% endif %}
      {% if content.field_link_fa_style|field_value %}
        {{ content.field_link_fa_style.0["#options"].entity.field_fa_code.value|raw }}
      {% endif %}

How can i fix that ?

The auto_reload: true does not seem to be recommended in production and specific conf to a theme does not seem to be possible https://www.drupal.org/project/drupal/issues/2002606

2
  • 3
    You need to make sure your paragraph display has the media's (and/or media file's) cache tag so that its render cache is invalidated when the media or media file is updated.
    – sonfd
    Jun 28, 2021 at 14:13
  • @sonfd how do u do that?
    – Matoeil
    Jun 28, 2021 at 15:33

1 Answer 1

6

In any entity template (like node.html.twig or paragraph.html.twig, but not field.html.twig), you must render {{ content }} at least once, otherwise the cache tags to not bubble up. A {{ content.field_something }} is not enough. You'll notice this problem everytime you need to render a nested entity (e.g. a media entity inside a paragraph or a node.)

If your design does not allow a blanket {{ content }}, do somehing like {% set dummy_force_cache_bubbling = content|render %}. This doesn't render anything visually, but it triggers the cache processing.

1
  • i would not have find it without u :)
    – Matoeil
    Jun 28, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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