-2

I have a site with Drupal 8 and the Flag module.

I would like to display a text when the user has the association role and when it is flagged with the following flag (his machine name):

flag_request_association_role

Then put its 2 conditions in a single line and how to know if the user is flagged ?

  {% if 'association' not in user.getroles and ??? %}
     <p>my text</p>
  {% endif %}

UPDATE

Here are my templates :

  {% if 'association' in user.getroles %}
    <div><i class="fas fa-circle text-success"></i> Votre compte association est actif.</div>
  {% elseif 'association' not in user.getroles and is_flagged_role_association %}
    <div><i class="fas fa-circle text-warning"></i> Votre compte association est en attente.</div>
  {% elseif 'association' not in user.getroles and not is_flagged_role_association %}
    <div><i class="fas fa-circle text-danger"></i> Votre compte association est inactif.</div>
    {{ content.flag_role_association }}
  {% endif %}

  <span class="border-top mt-4 mb-4"></span>

  <p>Les boutiques éphémères vous permettent d'exposer les créations des artisans présents sur la plateforme (pas de vente en ligne).</p>
  {% if 'pop_up_store' in user.getroles %}
    <div><i class="fas fa-circle text-success"></i> Votre compte boutique éphémère est actif.</div>
  {% elseif 'pop_up_store' not in user.getroles and is_flagged_role_pop_up_store %}
    <div><i class="fas fa-circle text-warning"></i> Votre compte boutique éphémère est en attente.</div>
  {% elseif 'pop_up_store' not in user.getroles and not is_flagged_role_pop_up_store %}
    <div><i class="fas fa-circle text-danger"></i> Votre compte boutique éphémère est inactif.</div>
    {{ content.flag_role_pop_up_store }}
  {% endif %}

  <span class="border-top mt-4 mb-4"></span>

  <p>Si vous êtes artisans, nous serions ravis de vous accueillir sur notre marketplace. Nous facturons une commission de 10% sur chaque vente. Vous recevrez une facture à chaque début de mois, si vous réalisez des ventes le mois précédent. Plus d'infos sur <a href="/node/271">cette page</a>.</p>
  <p>Pas de vente = Rien à payer</p>
  {% if 'marchand' in user.getroles %}
    <div><i class="fas fa-circle text-success"></i> Votre compte artisan est actif.</div>
  {% elseif 'marchand' not in user.getroles and is_flagged_role_artisan %}
    <div><i class="fas fa-circle text-warning"></i> Votre compte artisan est en attente.</div>
  {% elseif 'marchand' not in user.getroles and not is_flagged_role_artisan %}
    <div><i class="fas fa-circle text-danger"></i> Votre compte artisan est inactif.</div>
    {{ content.flag_role_artisan }}
  {% endif %}

bootstrap_subtheme_front_office_old.theme :

/**
 * Implements hook_preprocess_HOOK() for user.html.twig.
 */
function bootstrap_subtheme_front_office_old_preprocess_user(&$variables) {
  $flag_id = 'flag_role_association';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $user_to_flag = $variables['user'];
  $flag_status = $flag_service->getFlagging($flag, $user_to_flag);
  $variables['is_flagged_role_association'] = $flag_status;

  $flag_id = 'flag_role_pop_up_store';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $user_to_flag = $variables['user'];
  $flag_status = $flag_service->getFlagging($flag, $user_to_flag);
  $variables['is_flagged_role_pop_up_store'] = $flag_status;

  $flag_id = 'flag_role_artisan';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $user_to_flag = $variables['user'];
  $flag_status = $flag_service->getFlagging($flag, $user_to_flag);
  $variables['is_flagged_role_artisan'] = $flag_status;
}
8
  • 1
    You can't declare the same function name more than once. You declare function bootstrap_subtheme_front_office_old_theme_preprocess_user (&$variables) { 3 times. You need to put each flag code all under 1 function.
    – No Sssweat
    Commented Sep 26, 2020 at 20:47
  • 1 function, pastebin.com/2Vv6QTRt
    – No Sssweat
    Commented Sep 26, 2020 at 23:15
  • it's cached, if you clear the cache should show proper text.
    – No Sssweat
    Commented Sep 28, 2020 at 1:15
  • Shows expected text for me after clearing cache, so I don't know.
    – No Sssweat
    Commented Sep 29, 2020 at 0:11
  • @No Sssweat Ok thank you I found the problem. But the code created an error on most of my site pastebin.com/ku0ACkHH
    – user99486
    Commented Oct 1, 2020 at 22:30

1 Answer 1

0

mytheme.theme

/**
 * Implements hook_preprocess_HOOK() for user.html.twig.
 */
function mytheme_preprocess_user(&$variables) {
  $flag_id = 'flag_request_association_role';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $user_to_flag = $variables['user'];
  $flag_status = $flag_service->getFlagging($flag, $user_to_flag); // get flag status
  $variables['is_flagged'] = $flag_status; // create variable for user.html.twig
}

I would like to display a text when the user has the association role and when it is flagged

user.html.twig

  {% if user.hasRole('association') and is_flagged %}
    <p>my text</p>
  {% endif %}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.