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.

  • 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.
    – apaderno
    Sep 26, 2021 at 12:36
  • Welcome. could you please clarify in which twig template you want to do it? page? node? block?
    – Yuseferi
    Sep 26, 2021 at 12:54

1 Answer 1


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 %}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.