Score:-1

Is it possible to check with Twig the role the current user has?

cn flag

Is it possible to check with Twig the Group role the current user has, to use it as in the following code?

{% if is_member == true %}
  {# … #}
{% endif %}

I tried with $group_memberships = \Drupal::service('group.membership_loader').loadByUser($account); in the .theme file, but it doesn't work.

apaderno avatar
us flag
Welcome to Drupal Answers! If you wrote already code, we would like to see more than a single line. It could be the code you wrote is the one to use, but it contains a typo.
Yuseferi avatar
cg flag
Welcome. could you please clarify in which twig template you want to do it? page? node? block?
Score:1
cg flag

to see the list of user roles try the following:

{% set roles = user.getroles() %}
{{ dump(roles) }}

if you want an specific role try

If you want to do anything only for the particular role then do this:

{% if 'ROLE_MACHINE_NAME' in user.getroles()  %}
  // do your stuffs here
{% endif %}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.