You are here

function photos_user_view in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 photos.module \photos_user_view()
  2. 7.3 photos.module \photos_user_view()
  3. 6.0.x photos.module \photos_user_view()

Implements hook_ENTITY_TYPE_view() for user entities.

File

./photos.module, line 942
Implementation of photos.module.

Code

function photos_user_view(array &$build, EntityInterface $account, EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'full') {
    if (\Drupal::currentUser()
      ->hasPermission('view photo') || $account
      ->hasPermission('create photo')) {
      $user = \Drupal::currentUser();
      if ($display
        ->getComponent('photos_album_count')) {
        $description = '';
        if ($account->album['album']['count']) {
          $url = Url::fromUri('base:photos/user/' . $account
            ->id() . '/album');
          $user_albums = \Drupal::translation()
            ->formatPlural($account->album['album']['count'], '@count album', '@count albums');
          $description = Link::fromTextAndUrl($user_albums, $url)
            ->toString();
          if ($account
            ->id() == $user
            ->id()) {
            $album_count = PhotosAlbum::userAlbumCount();
            if (!isset($album_count['rest']) || $album_count['rest'] < 1) {
              $description .= ' ' . Link::fromTextAndUrl(t('Create new album'), Url::fromUri('base:node/add/photos'))
                ->toString();
            }
          }
        }
        elseif ($account
          ->id() == $user
          ->id()) {

          // @todo check permissions before displaying.
          $create_album_link = Link::fromTextAndUrl(t('Create album'), Url::fromRoute('node.add', [
            'node_type' => 'photos',
          ]))
            ->toString();
          $description = t('No albums yet, @link', [
            '@link' => $create_album_link,
          ]);
        }
        else {
          $description = t('No albums yet.');
        }
        $build['photos_album_count'] = [
          '#type' => 'item',
          '#markup' => '<h4 class="label">' . t('User albums') . '</h4> ' . $description,
          '#cache' => [
            'tags' => [
              'photos:album:user:' . $user
                ->id(),
              'user:' . $user
                ->id(),
            ],
          ],
        ];
      }
      if ($display
        ->getComponent('photos_image_count')) {
        if ($account->album['image']['count']) {
          $url = Url::fromUri('base:photos/user/' . $account
            ->id() . '/image');
          $user_images = \Drupal::translation()
            ->formatPlural($account->album['image']['count'], '@count image', '@count images');
          $description = Link::fromTextAndUrl($user_images, $url)
            ->toString();
        }
        else {
          $description = t('No images yet.');
        }
        $build['photos_image_count'] = [
          '#type' => 'item',
          '#markup' => '<h4 class="label">' . t('User images') . '</h4> ' . $description,
          '#cache' => [
            'tags' => [
              'photos:image:user:' . $user
                ->id(),
              'user:' . $user
                ->id(),
            ],
          ],
        ];
      }
    }
  }
}