Skip to content
Snippets Groups Projects

Add current user roles as classes in body

Open ressa requested to merge issue/drupal-3524122:3524122-insert-all-roles into 11.x
Files
4
@@ -10,6 +10,7 @@
use Drupal\Component\Assertion\Inspector;
use Drupal\user\RoleInterface;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Html;
use Drupal\Core\Session\AccountInterface;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\Core\Render\Element;
@@ -220,6 +221,12 @@ public function templatePreprocessDefaultVariablesAlter(&$variables): void {
unset($variables['user']->pass, $variables['user']->sid, $variables['user']->ssid);
$variables['is_admin'] = $user->hasPermission('access administration pages');
$variables['logged_in'] = $user->isAuthenticated();
$roles = $user->getRoles();
$all_roles = '';
foreach ($roles as $role) {
$all_roles .= Html::cleanCssIdentifier($role) . '-role ';
}
$variables['roles'] = trim($all_roles);
}
/**
Loading