View source
<?php
namespace Drupal\photos\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\Controller\EntityViewController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PhotosImageViewController extends EntityViewController {
use RedirectDestinationTrait;
use StringTranslationTrait;
protected $connection;
protected $libraryDiscovery;
protected $routeMatch;
protected $entityRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, Connection $connection, LibraryDiscoveryInterface $library_discovery, RouteMatchInterface $route_match, EntityRepositoryInterface $entity_repository = NULL) {
parent::__construct($entity_type_manager, $renderer);
$this->connection = $connection;
$this->libraryDiscovery = $library_discovery;
$this->routeMatch = $route_match;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('database'), $container
->get('library.discovery'), $container
->get('current_route_match'));
}
public function access(AccountInterface $account) {
$photos_image = $this->routeMatch
->getParameter('photos_image');
if ($photos_image
->access('view')) {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}
public function view(EntityInterface $photos_image, $view_mode = 'full', $langcode = NULL) {
if (!$photos_image) {
throw new NotFoundHttpException();
}
$build = parent::view($photos_image, $view_mode);
$photosImage = $photos_image;
$config = \Drupal::config('photos.settings');
$destination = $this
->getDestinationArray();
$node = $this->entityTypeManager
->getStorage('node')
->load($photos_image
->getAlbumId());
switch ($view_mode) {
case 'list':
if ($photosImage
->access('edit')) {
$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',
],
],
]),
];
$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')) {
$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':
$build['links']['pager'] = $photosImage
->getPager($photosImage
->getAlbumId(), 'album_id');
if ($photosImage
->access('update')) {
$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,
],
]),
];
}
$renderCommentCount = [];
if ($config
->get('photos_comment')) {
$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;
$disableImageVisitCount = $config
->get('photos_image_count');
if (!$disableImageVisitCount) {
$build['#attached']['library'][] = 'photos/photos.statistics';
$settings = [
'data' => [
'id' => $photosImage
->id(),
],
'url' => Url::fromRoute('photos.statistics.update')
->toString(),
];
$build['#attached']['drupalSettings']['photosStatistics'] = $settings;
}
$build['#attached']['library'][] = 'photos/photos.default.style';
break;
default:
break;
}
$build['#cache']['contexts'][] = 'url.site';
$build['#cache']['contexts'][] = 'user.roles:anonymous';
return $build;
}
public function title(EntityInterface $photos_image) {
$title = '';
if ($this->entityRepository) {
$title = $this->entityRepository
->getTranslationFromContext($photos_image)
->label();
}
return $title;
}
}