View source
<?php
namespace Drupal\photos\Plugin\Block;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PhotosInformation extends BlockBase implements ContainerFactoryPluginInterface {
protected $connection;
protected $entityTypeManager;
protected $requestStack;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection, EntityTypeManagerInterface $entity_manager, RequestStack $request_stack, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->connection = $connection;
$this->entityTypeManager = $entity_manager;
$this->requestStack = $request_stack;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'), $container
->get('entity_type.manager'), $container
->get('request_stack'), $container
->get('current_route_match'));
}
public function access(AccountInterface $account, $return_as_object = FALSE) {
if ($account
->hasPermission('view photo')) {
$access = AccessResult::allowed();
}
else {
$access = AccessResult::forbidden();
}
return $return_as_object ? $access : $access
->isAllowed();
}
public function build() {
$content = [];
$photosImage = $this->routeMatch
->getParameter('photos_image');
$pager_type = 'album_id';
if ($photosImage) {
$query = $this->connection
->select('photos_image_field_data', 'p');
$query
->join('node_field_data', 'n', 'n.nid = p.album_id');
$query
->join('users_field_data', 'u', 'p.uid = u.uid');
$query
->fields('p')
->fields('n', [
'nid',
'title',
])
->fields('u', [
'name',
'uid',
])
->condition('p.id', $photosImage
->id());
$query
->addTag('node_access');
$image = $query
->execute()
->fetchObject();
$blockImage = [];
if ($image) {
try {
$account = $this->entityTypeManager
->getStorage('user')
->load($image->uid);
$blockImage['name'] = $account
->getDisplayName();
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('photos', $e);
} catch (PluginNotFoundException $e) {
watchdog_exception('photos', $e);
}
$blockImage['photos_image'] = $photosImage;
$pager_id = $image->nid;
$blockImage['pager'] = $photosImage
->getPager($pager_id, $pager_type);
$content = [
'#theme' => 'photos_image_block',
'#image' => $blockImage,
'#cache' => [
'tags' => [
'photos:image:' . $photosImage
->id(),
'photos:album:' . $image->nid,
'node:' . $image->nid,
],
],
];
$content['#attached']['library'][] = 'photos/photos.block.information';
}
}
return $content;
}
public function getCacheMaxAge() {
return 0;
}
}