JuiceboxXmlControllerField.php in Juicebox HTML5 Responsive Image Galleries 8.2
File
src/Controller/JuiceboxXmlControllerField.php
View source
<?php
namespace Drupal\juicebox\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\juicebox\JuiceboxGalleryInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
class JuiceboxXmlControllerField extends JuiceboxXmlControllerBase {
protected $entityType;
protected $entityId;
protected $fieldName;
protected $displayName;
protected $entity;
protected $entityTypeManager;
protected $entityRepository;
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('request_stack'), $container
->get('http_kernel'), $container
->get('entity_type.manager'), $container
->get('entity.repository'));
}
public function __construct(ConfigFactoryInterface $config_factory, RequestStack $request_stack, HttpKernelInterface $http_kernel, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($config_factory, $request_stack, $http_kernel);
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
protected function init() {
$attribs = $this->request->attributes
->get('_raw_variables');
$this->entityType = $attribs
->get('entityType');
$this->entityId = $attribs
->get('entityId');
$this->fieldName = $attribs
->get('fieldName');
$this->displayName = $attribs
->get('displayName');
$entity_base = $this->entityTypeManager
->getStorage($this->entityType)
->load($this->entityId);
$this->entity = $this->entityRepository
->getTranslationFromContext($entity_base);
if (is_object($this->entity) && $this->entity instanceof EntityInterface) {
return;
}
throw new \Exception(t('Cannot instantiate field-based Juicebox gallery as no entity can be loaded.'));
}
protected function access() {
if (is_object($this->entity)) {
$entity_access = $this->entity
->access('view');
$field_access = $this->entity->{$this->fieldName}
->access('view');
return $entity_access && $field_access;
}
return FALSE;
}
protected function getGallery() {
$field = $this->entity->{$this->fieldName}
->view($this->displayName);
if (!empty($field[0]['#gallery']) && $field[0]['#gallery'] instanceof JuiceboxGalleryInterface && $field[0]['#gallery']
->getId()) {
return $field[0]['#gallery'];
}
throw new \Exception(t('Cannot build Juicebox XML for field-based gallery.'));
}
protected function calculateXmlCacheTags() {
$entity_tags = $this->entity instanceof CacheableDependencyInterface ? $this->entity
->getCacheTags() : [];
$display = $this->entityTypeManager
->getStorage('entity_view_display')
->load($this->entityType . '.' . $this->entity
->bundle() . '.' . $this->displayName);
$display_tags = [];
if ($display instanceof CacheableDependencyInterface) {
$display_tags = $display
->getCacheTags();
if (!$display
->status() || $display
->isNew()) {
$display_default = $this->entityTypeManager
->getStorage('entity_view_display')
->load($this->entityType . '.' . $this->entity
->bundle() . '.default');
if ($display_default instanceof CacheableDependencyInterface) {
$display_tags = Cache::mergeTags($display_tags, $display_default
->getCacheTags());
}
}
}
return Cache::mergeTags($entity_tags, $display_tags);
}
}