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 :

https://i.stack.imgur.com/MpANw.png

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 :

https://i.stack.imgur.com/JkhhN.png

Great, the counter works :

https://i.stack.imgur.com/QuQC2.png

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 :

https://i.stack.imgur.com/xCqkx.png

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()) }}

https://i.stack.imgur.com/Q146k.png

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

https://i.stack.imgur.com/cW14w.png

  • I suggest using the default Block system to add to the page. Then Drupal knows the context of what is on the page. When you manually print the View content in the template, then Drupal has no idea what the content is or when and if it should be cached or refreshed. Example setup: https://www.drupal.org/docs/user_guide/en/block-place.html – Brian Wagner Jun 24 '20 at 18:38
  • @BrianWagner with your solution, I don't know how to create my counter. –  Jun 24 '20 at 21:59
  • Mathieu, so you have multiple views, right? In this case, you need a custom Block that can do the logic you have in your template. Then you would have to add the User to the cache tags. What happens then is that Drupal will cache the block per user, and re-run your logic and send that data to the template. Here is info on creating a custom Block: https://www.drupal.org/docs/8/creating-custom-modules/creating-custom-blocks/create-a-custom-block And some info on caching: https://dev.acquia.com/blog/coding-with-cache-tags-in-drupal-8/13/09/2018/19851 – Brian Wagner Jun 25 '20 at 20:23
  • @BrianWagner By inserting the following code in my TWIG file, it works. What do you think ? `{{ {'#cache': {'max-age': 0}} }}` –  Jun 26 '20 at 20:05
  • Hey, glad you found a solution. That is one way to do it, for sure. – Brian Wagner Jun 26 '20 at 20:07

0 Answers0