You are here

function role_expire_user_view in Role Expire 8

Same name and namespace in other branches
  1. 7 role_expire.module \role_expire_user_view()
  2. 2.x role_expire.module \role_expire_user_view()

Implements hook_ENTITY_TYPE_view() for user entities.

File

./role_expire.module, line 269
Role Expire module.

Code

function role_expire_user_view(&$build, $entity, $display, $view_mode) {
  $account = $build['#user'];
  $currentUser = \Drupal::currentUser();
  if ($display
    ->getComponent('role_expire')) {

    // Only show the role expire field to role administrators or the user.
    if ($currentUser
      ->hasPermission('administer role expire') || $currentUser
      ->hasPermission('administer users') || $currentUser
      ->id() == $account
      ->id()) {

      // 1. Gather all role expiration information.
      $roles = [];
      $expiry_roles = \Drupal::service('role_expire.api')
        ->getAllUserRecords($account
        ->id());
      foreach ($account
        ->getRoles() as $rid) {
        if (array_key_exists($rid, $expiry_roles)) {
          $roles[] = t("%role role expiration date: %timedate", [
            '%role' => ucfirst($rid),
            '%timedate' => \Drupal::service('date.formatter')
              ->format($expiry_roles[$rid]),
          ]);
        }
      }

      // 2. Build role expiration information.
      if ($roles) {
        $build['role_expire'] = [
          '#theme' => 'item_list',
          '#items' => $roles,
          '#title' => t('Role expiration'),
          '#attributes' => [
            'class' => [
              'role-expiry-roles',
            ],
          ],
          '#weight' => 1000,
        ];
      }
    }
  }
}