You are here

public function UserController::buildUserInfo in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::buildUserInfo()

Builds render array for a user info block.

Parameters

\Drupal\user\UserInterface $user: User.

Return value

array Render array.

1 call to UserController::buildUserInfo()
UserController::index in src/Controller/UserController.php
Builds render array for a user statistics index page.

File

src/Controller/UserController.php, line 87

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildUserInfo(UserInterface $user) {
  $user_picture = $user
    ->get('user_picture')
    ->getValue();
  if (isset($user_picture[0]['target_id'])) {
    $user_picture = File::load($user_picture[0]['target_id']);
    $user_picture = ImageStyle::load('medium')
      ->buildUrl($user_picture
      ->getFileUri());
  }
  else {
    $user_picture = base_path() . drupal_get_path('module', 'opigno_statistics') . '/img/picto-profile-default.svg';
  }
  $created_timestamp = $user
    ->getCreatedTime();
  $accessed_timestamp = $user
    ->getLastAccessedTime();
  $date_joined = $this->date_formatter
    ->format($created_timestamp, 'custom', 'Y-m-d');
  $last_access = $this->date_formatter
    ->format($accessed_timestamp, 'custom', 'Y-m-d');
  $member_for = $this->date_formatter
    ->formatTimeDiffSince($created_timestamp);
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'user-info',
      ],
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'user-info-photo-wrapper',
        ],
      ],
      isset($user_picture) ? [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'class' => [
            'user-info-photo',
          ],
          'style' => 'background-image: url(' . $user_picture . ')',
        ],
      ] : [],
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'user-info-text-wrapper',
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'class' => [
            'user-info-icon',
          ],
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'user-info-name',
          ],
        ],
        '#value' => $user
          ->getDisplayName(),
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'user-info-email',
          ],
        ],
        '#value' => $user
          ->getEmail(),
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'user-info-date-joined',
          ],
        ],
        '#value' => $this
          ->t('Date joined @date', [
          '@date' => $date_joined,
        ]),
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'user-info-last-access',
          ],
        ],
        '#value' => $this
          ->t('Last access @date', [
          '@date' => $last_access,
        ]),
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'user-info-member-for',
          ],
        ],
        '#value' => $this
          ->t('Member for @time', [
          '@time' => $member_for,
        ]),
      ],
    ],
  ];
}