public function PhotosImageViewController::view in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 src/Controller/PhotosImageViewController.php \Drupal\photos\Controller\PhotosImageViewController::view()
 
Provides a page to render a single entity.
Parameters
\Drupal\Core\Entity\EntityInterface $_entity: The Entity to be rendered. Note this variable is named $_entity rather than $entity to prevent collisions with other named placeholders in the route.
string $view_mode: (optional) The view mode that should be used to display the entity. Defaults to 'full'.
Return value
array A render array as expected by \Drupal\Core\Render\RendererInterface::render().
Overrides EntityViewController::view
1 call to PhotosImageViewController::view()
- PhotosLegacyImageViewController::view in src/
Controller/ PhotosLegacyImageViewController.php  - Provides a page to render a single entity.
 
1 method overrides PhotosImageViewController::view()
- PhotosLegacyImageViewController::view in src/
Controller/ PhotosLegacyImageViewController.php  - Provides a page to render a single entity.
 
File
- src/
Controller/ PhotosImageViewController.php, line 116  
Class
- PhotosImageViewController
 - Image view controller.
 
Namespace
Drupal\photos\ControllerCode
public function view(EntityInterface $photos_image, $view_mode = 'full', $langcode = NULL) {
  if (!$photos_image) {
    throw new NotFoundHttpException();
  }
  $build = parent::view($photos_image, $view_mode);
  /** @var \Drupal\photos\Entity\PhotosImage $photosImage */
  $photosImage = $photos_image;
  // Get config settings.
  // @todo inject config factory?
  $config = \Drupal::config('photos.settings');
  // Current destination.
  $destination = $this
    ->getDestinationArray();
  // Get album node.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($photos_image
    ->getAlbumId());
  switch ($view_mode) {
    case 'list':
      if ($photosImage
        ->access('edit')) {
        // Image edit link.
        $build['links']['edit'] = [
          '#type' => 'link',
          '#title' => 'Edit',
          '#url' => Url::fromRoute('entity.photos_image.edit_form', [
            'photos_image' => $photosImage
              ->id(),
          ], [
            'query' => [
              $destination,
            ],
            'attributes' => [
              'class' => [
                'colorbox-load',
                'photos-edit-edit',
              ],
            ],
          ]),
        ];
        // Set to album cover link.
        $build['links']['cover'] = [
          '#type' => 'link',
          '#title' => 'Set to Cover',
          '#url' => Url::fromRoute('photos.album.update.cover', [
            'node' => $photosImage
              ->getAlbumId(),
            'photos_image' => $photosImage
              ->id(),
          ], [
            'query' => [
              $destination,
            ],
          ]),
        ];
      }
      if ($photosImage
        ->access('delete')) {
        // Image delete link.
        // @todo cancel should go back to image. Confirm to album.
        $build['links']['delete'] = [
          '#type' => 'link',
          '#title' => 'Delete',
          '#url' => Url::fromRoute('entity.photos_image.delete_form', [
            'photos_image' => $photosImage
              ->id(),
          ], [
            'query' => [
              'destination' => 'node/' . $photosImage
                ->getAlbumId(),
            ],
            'attributes' => [
              'class' => [
                'colorbox-load',
                'photos-edit-delete',
              ],
            ],
          ]),
        ];
      }
      break;
    case 'full':
      // Image pager.
      $build['links']['pager'] = $photosImage
        ->getPager($photosImage
        ->getAlbumId(), 'album_id');
      if ($photosImage
        ->access('update')) {
        // Set image to album cover link.
        $build['links']['cover'] = [
          '#type' => 'link',
          '#title' => 'Set to Cover',
          '#url' => Url::fromRoute('photos.album.update.cover', [
            'node' => $photosImage
              ->getAlbumId(),
            'photos_image' => $photosImage
              ->id(),
          ], [
            'query' => [
              $destination,
            ],
          ]),
        ];
      }
      // Get comments.
      $renderCommentCount = [];
      if ($config
        ->get('photos_comment') && \Drupal::moduleHandler()
        ->moduleExists('comment')) {
        // Comment integration.
        $entities = [
          $photosImage
            ->id() => $photosImage,
        ];
        $stats = \Drupal::service('comment.statistics')
          ->read($entities, 'photos_image');
        if ($stats) {
          $comCount = 0;
          foreach ($stats as $commentStats) {
            $comCount = $comCount + $commentStats->comment_count;
          }
          $renderCommentCount = [
            '#markup' => $this
              ->formatPlural($comCount, "@count comment", "@count comments"),
          ];
        }
      }
      $build['links']['comment'] = $renderCommentCount;
      // Check count image views variable.
      $disableImageVisitCount = $config
        ->get('photos_image_count');
      if (!$disableImageVisitCount) {
        // @todo migrate to core statistics when it can handle other entities.
        // @see https://www.drupal.org/project/drupal/issues/2532334
        $build['#attached']['library'][] = 'photos/photos.statistics';
        $settings = [
          'data' => [
            'id' => $photosImage
              ->id(),
          ],
          'url' => Url::fromRoute('photos.statistics.update')
            ->toString(),
        ];
        $build['#attached']['drupalSettings']['photosStatistics'] = $settings;
      }
      // Attach default styling.
      // @see https://www.drupal.org/docs/8/theming/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-theme#override-extend
      $build['#attached']['library'][] = 'photos/photos.default.style';
      break;
    default:
      break;
  }
  // Since this generates absolute URLs, it can only be cached "per site".
  $build['#cache']['contexts'][] = 'url.site';
  // Given this varies by $this->currentUser->isAuthenticated(), add a cache
  // context based on the anonymous role.
  $build['#cache']['contexts'][] = 'user.roles:anonymous';
  return $build;
}