You are here

function profile_user_view in Profile 2 8

Implements hook_user_view().

File

./profile.module, line 95
Support for configurable user profiles.

Code

function profile_user_view(array &$build, UserInterface $account, EntityViewDisplayInterface $display, $view_mode, $langcode) {

  // Only attach profiles for the full account view.
  if ($view_mode != 'full') {
    return;
  }

  // Position profiles at the bottom of account page.
  $weight = 100;
  foreach (\Drupal::configFactory()
    ->listAll('profile.type.') as $config_name) {
    $config = \Drupal::config($config_name);
    $profiles = \Drupal::entityManager()
      ->getStorage('profile')
      ->loadByProperties(array(
      'uid' => $account
        ->id(),
      'type' => $config
        ->get('id'),
      'status' => PROFILE_ACTIVE,
    ));
    if (count($profiles)) {

      // Only display profiles user has access.
      foreach ($profiles as $key => $profile) {
        if (!$profile
          ->access('view')) {
          unset($profiles[$key]);
        }
      }
      if (count($profiles)) {
        $build['profiles']['#weight'] = $weight + $config
          ->get('weight');
        $build['profiles'][$config
          ->get('id')] = array(
          '#theme' => 'profile_items',
          '#profile_items' => $profiles,
          '#type' => $config
            ->get('label'),
        );
      }
    }
  }
  $build['#attached']['library'][] = 'profile/drupal.profile-items';
}