You are here

function photos_user_view in Album Photos 6.0.x

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

Implements hook_ENTITY_TYPE_view() for user entities.

File

./photos.module, line 743
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')) {
        if ($account->album['album']['count']) {
          $user_albums_text = \Drupal::translation()
            ->formatPlural($account->album['album']['count'], '@count album', '@count albums');
          $user_albums = $user_albums_text;
          if (\Drupal::moduleHandler()
            ->moduleExists('views')) {
            $view_display = \Drupal::config('photos.settings')
              ->get('user_albums_link_override');
            if (!$view_display) {
              $view_display = 'photos_album_list:page_2';
            }
            $view_parts = explode(':', $view_display);
            $views_view = $view_parts[0];
            $view_display_id = $view_parts[1];
            $photos_album_list_view = Views::getView($views_view);
            if ($photos_album_list_view) {
              $view_enabled = $photos_album_list_view->storage
                ->status();
              if ($view_enabled) {
                $url = Url::fromRoute('view.' . $views_view . '.' . $view_display_id, [
                  'user' => $account
                    ->id(),
                ]);
                $user_albums = Link::fromTextAndUrl($user_albums_text, $url)
                  ->toString();
              }
            }
          }
          $description = $user_albums;
          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 and count 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']) {
          $user_images_text = \Drupal::translation()
            ->formatPlural($account->album['image']['count'], '@count image', '@count images');
          $user_images = $user_images_text;
          if (\Drupal::moduleHandler()
            ->moduleExists('views')) {
            $view_display = \Drupal::config('photos.settings')
              ->get('user_images_link_override');
            if (!$view_display) {
              $view_display = 'photos_album:page_1';
            }
            $view_parts = explode(':', $view_display);
            $views_view = $view_parts[0];
            $view_display_id = $view_parts[1];
            $photos_album_list_view = Views::getView($views_view);
            if ($photos_album_list_view) {
              $view_enabled = $photos_album_list_view->storage
                ->status();
              if ($view_enabled) {
                $url = Url::fromRoute('view.' . $views_view . '.' . $view_display_id, [
                  'user' => $account
                    ->id(),
                ]);
                $user_images = Link::fromTextAndUrl($user_images_text, $url)
                  ->toString();
              }
            }
          }
          $description = $user_images;
        }
        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(),
            ],
          ],
        ];
      }
    }
  }
}