0

I have a TWIG template in Drupal 8. The "dangers" value is incremented according to the result of each view. How to display the TWIG code if the result of "dangers" is not 0

I tested this code {% if dangers > 0 %} but it doesn't work :

{% set dangers = 0 %}
{% if dangers > 0 %}
Vous avez
{% 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 %}
{{ dangers }}
tâches importantes à traiter dans votre {{ store_entity.type.entity.label }} "{{ store_entity.name.value }}".
<a href="/store/{{ store_entity.id }}/tasks" data-drupal-link-system-path="/store/{{ store_entity.id }}/tasks">Voir la liste</a>
{% endif %}

1 Answers1

0

Your condition is misplaced, if I do a simple logic out of your actual code it would say:

dangers = 0
if dangers > 0 
  compute dangers 
  display dangers
end if

So, yes, dangers will always be 0 because you compute the value of dangers in the condition dangers > 0.

Your correct logic should be

dangers = 0
compute dangers 
if dangers > 0 
  display dangers
end if

So for your twig code, this c would be the correct version:

{% 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 > 0 %}
  Vous avez {{ dangers }} tâches importantes à traiter dans votre {{ store_entity.type.entity.label }} "{{ store_entity.name.value }}".
  <a href="/store/{{ store_entity.id }}/tasks" data-drupal-link-system-path="/store/{{ store_entity.id }}/tasks">Voir la liste</a>
{% endif %}

Pro tip: although you don't need to indent and space code properly in HTML, I would really recommend you to do it, that would help you spot the errors by yourself more easily.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thank you, like that yes I see the logic –  Jun 21 '20 at 15:23
  • However, I have no solution to exclude the TWIG code from the Drupal 8 cache. Even disabling the view cache does not work. –  Jun 21 '20 at 15:34