0

I have a site with Drupal 8.9 and Twig Tweak.

I created 7 view blocks to set up a task list in stores. I want to display a task counter for my current user on my home page.

The tasks checks :

  • If the store has no product (danger).
  • If a product has no product variation (danger).
  • If an order does not have the processed status (danger).
  • If the store does not have a delivery method (danger).
  • If the store does not have a payment gateway (danger).
  • If a product from the store is not published (warning).
  • If the store owner does not have the "merchant" role (warning).

I created a display mode in the types of stores, which I rewrite (integrating the 7 task blocks) with the following code :

commerce-store--professionnel--tasks-frontpage.html.twig

{% set warnings = 0 %}
{% if drupal_view_result('boutique_page_liste_des_taches_produit_non_publie', 'block_1') is not empty %}
  {% set warnings = warnings + 1 %}
{% endif %}
{% if drupal_view_result('boutique_page_liste_des_taches_role_marchand', 'block_1') is empty %}
  {% set warnings = warnings + 1 %}
{% endif %}
{% set dangers = 0 %}
{% if drupal_view_result('boutique_page_liste_des_taches_aucun_produit', 'block_1', store_entity.id()) is empty %}
  {% set dangers = dangers + 1 %}
{% endif %}
{% if drupal_view_result('boutique_page_liste_des_taches_aucune_variation', 'block_1', store_entity.id()) is not empty %}
  {% set dangers = dangers + 1 %}
{% endif %}
{% if drupal_view_result('boutique_page_liste_des_taches_commande', 'block_1', store_entity.id()) is not empty %}
  {% set dangers = dangers + 1 %}
{% endif %}
{% if drupal_view_result('boutique_page_liste_des_taches_mode_de_livraison', 'block_1', store_entity.id()) is empty %}
  {% set dangers = dangers + 1 %}
{% endif %}
{% if drupal_view_result('boutique_page_liste_des_taches_passerelle_de_paiement', 'block_1', store_entity.id()) is empty %}
  {% set dangers = dangers + 1 %}
{% endif %}

{% if dangers or warnings > 0 %}
  <div class="alert alert-light border overflow-hidden shadow rounded hover mt-5 mb-0" role="alert">
    <div>
      <i class="fas fa-tasks fa-2x mr-4 float-left"></i>
      <div class="alert-heading h5 mt-0 mb-2">Des tâches requièrent votre attention dans votre <span class="text-lowercase">{{ store_entity.type.entity.label }}</span> "{{ store_entity.name.value }}".</div>
      <small>Veuillez passer en revue <a href="/store/{{ store_entity.id }}/tasks" data-drupal-link-system-path="/store/{{ store_entity.id }}/tasks">cette liste</a>.</small>
    </div>
    <hr class="mt-3 mb-3 border">
    <div class="d-flex justify-content-around">
      {% if dangers > 0 %}
        <p class="text-center mb-0"><i class="fas fa-times-circle fa-2x text-danger"></i><br>{{ dangers }} importante{% if dangers > 1 %}s{% endif %}</p>
      {% endif %}
      {% if warnings > 0 %}
        <p class="text-center mb-0"><i class="fas fa-exclamation-circle fa-2x text-warning"></i><br>{{ warnings }} avertissement{% if warnings > 1 %}s{% endif %}</p>
      {% endif %}
    </div>
  </div>
{% endif %}

{# /** TEST */ #}
{{ drupal_view('boutique_page_liste_des_taches_role_marchand', 'block_1', store_entity.id()) }}

I created a view block to display the stores of the current user :

enter image description here

I integrated this block to my home page :

page--front.html.twig

<div class="main-timeline">
  {{ drupal_view('accueil_page_liste_des_taches_utilisateur', 'block_1') }}
  {{ drupal_view('accueil_page_liste_des_taches_boutique', 'block_1') }}
  {{ drupal_view('accueil_page_liste_des_taches_groupe', 'block_1') }}
  {{ drupal_view('message_activity_stream_timeline_private', 'block_1') }}
</div>

I have disabled the cache for all views :

enter image description here

Great, the counter works :

enter image description here

My problem :

The counter never changes, to update it I have to do a drush cr.

How to correct this problem ?

Maybe I should add something to my .theme file.

To deactivate the cache of this template.

Or update this code with JS.

I DON'T WANT TO DESECTIVATE THE COVER FOR THE WHOLE SITE, BUT ONLY ON THE COUNTER.

Just for information, here is what the list of stains in a store looks like :

enter image description here

UPDATE :

I added this code at the end of the commerce-store--professionnel--tasks-frontpage.html.twig template.

Only this block is updated without any problems. For the rest of the code, it is cache and there is no update without clearing the cache manually.

It seems that the code adds them to the code (the counting functionality) is problematic.

{# /** TEST */ #}
{{ drupal_view('boutique_page_liste_des_taches_role_marchand', 'block_1', store_entity.id()) }}

enter image description here

I did not clear the cache. The counter is not updated. The added block is updated.

enter image description here

  • You have to follow the guide from here drupal.org/node/2598914 to disable twig cache – miststudent2011 Jun 22 at 15:21
  • 1
    @miststudent2011 Sorry but that's not my question. My site is in production I want to deactivate the cache or update my code only for the template of my question. – user99169 Jun 22 at 15:25
  • I'm not sure what this means: I disabled the cache in the block views used in this TWIG. Can you add some more information about what you mean here? What exactly did you do? – sonfd Jun 22 at 16:10
  • 1
    Also, generally speaking, you usually don't want to disable cache outright. You're better off figuring out the correct contexts and tags to use so you can cache this. – sonfd Jun 22 at 16:13
  • 1
    @sonfd I have updated the question. I don't want to deactivate the cache for the whole site, that's not my question. I want to disable the cache only for the counter, so that it is updated without manually clearing the cache. – user99169 Jun 22 at 16:24
2

You need to add the correct cache metadata to the template. Since you don't render anything, you are just counting view results, Drupal can't bubble up the caching information from the rendered output. It's hard to tell what is the right thing to do because you don't provide any details about the type of template.

For testing you could render a cache max-age in the template directly

{{ {'#cache': {'max-age': 0}} }}

and later build a proper render array with the correct cache metadata when preparing the variables for the template.

| improve this answer | |
  • Hi, I just updated my question. – user99169 Jun 23 at 8:53
  • 3
    The answer is still valid for the original question. As a side note, you should place blocks via the block layout. Rendering a View via Twig Tweak is not a block even if you render a block display. I would use a custom block plugin, query the view results in php and then render the result by passing it via a template variable to your custom template. When then placed as real block with a cache max-age 0 it doesn't impact performance that much because it gets auto-placeholdered and lazy-built. – 4k4 Jun 23 at 9:04
  • It looks very complicated. If I add the {{ drupal_view('accueil_page_liste_des_taches_boutique', 'block_1') }} block in "Block layout" it is not updated. Is it not possible to update the counter with JS ? Or add a code to the .theme file ? I didn't think cache management was that complicated in Drupal. – user99169 Jun 23 at 9:32
  • It is very strange, because I have a block integrated in the TWIG file of the home page {{ drupal_view('message_activity_stream_timeline_private', 'block_1') }}, which displays the nodes and it updates without problem. As soon as a new node is created, if I reload my page, it appears. – user99169 Jun 23 at 9:36
  • Your comment does not seem correct. I just updated this question. – user99169 Jun 23 at 23:31

Your Answer

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