Score:1

is it possible to deactive twig cache for a single theme custom template?

za flag

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

sonfd avatar
in flag
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.
Matoeil avatar
za flag
@sonfd how do u do that?
Score:5
ru flag

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.

Matoeil avatar
za flag
i would not have find it without u :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.