简体   繁体   中英

How to display blocks according to the conditions of the current user?

I have a Drupal 8.7 site with Drupal Commerce 2.14

I also installed TWIG Tweak module.

I want to display text based on certain conditions of the current user in my TWIG file.

Thanks for your help.

I write a comment for each condition I want.

Here is the code of my page :

Les étapes pour vendre sur la plateforme

<div class="mb-3">
  {% if logged_in %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Incrivez-vous sur la plateforme
</div>

{# IF THE CURRENT USER TO CREATE A "PRO" TYPE STORE #}
<div class="mb-3">
  {% if ??? %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Créer votre boutique
</div>

<div class="mb-3">
  {% if logged_in %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Créer vos conditions générales de vente
</div>

<div class="mb-3">
  {% if 'marchand_pro' in user.getroles  %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Attendre que l'administrateur de la plateforme valide votre boutique
</div>

{# IF THE CURRENT USER CREATING A PRODUCT IN A "PRO" TYPE STORE #}
<div class="mb-3">
  {% if ??? %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Créer vos produits ou services
</div>

{# IF THE CURRENT USER CREATES A SHIPPING METHOD IN A "PRO" TYPE STORE #}
<div class="mb-3">
  {% if ??? %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Créer les modes de livraison
</div>

{# IF THE CURRENT USER CREATES A PAYMENT GATEWAY IN A "PRO" TYPE STORE #}
<div class="mb-3">
  {% if ??? %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Associer la passerelle de paiement
</div>

<div class="mb-3">
  {% if 'marchand_premium' in user.getroles  %}
  <i class="fas fa-clipboard-check fa-2x"></i> 
  {% else %}
  <i class="fas fa-clipboard fa-2x"></i> 
  {% endif %}
  Activer le compte premium
</div>

Try implementing in a custom module a block plugin and a theme function. In the "build" method of the block plugin, add all your logic and just pass the results as variables to the block template.

https://www.drupal.org/docs/8/creating-custom-modules/creating-custom-blocks/create-a-custom-block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM