class JuiceboxXmlControllerField in Juicebox HTML5 Responsive Image Galleries 8.3
Same name and namespace in other branches
- 8.2 src/Controller/JuiceboxXmlControllerField.php \Drupal\juicebox\Controller\JuiceboxXmlControllerField
Controller routines for field-based XML.
Hierarchy
- class \Drupal\juicebox\Controller\JuiceboxXmlControllerBase implements ContainerInjectionInterface
- class \Drupal\juicebox\Controller\JuiceboxXmlControllerField
Expanded class hierarchy of JuiceboxXmlControllerField
File
- src/
Controller/ JuiceboxXmlControllerField.php, line 19
Namespace
Drupal\juicebox\ControllerView source
class JuiceboxXmlControllerField extends JuiceboxXmlControllerBase {
/**
* The entity type involved in this XML request (e.g. "node").
*
* @var string
*/
protected $entityType;
/**
* The numeric ID of the entity involved in this XML request.
*
* @var string
*/
protected $entityId;
/**
* The field name of the entity field involved in this XML request.
*
* @var string
*/
protected $fieldName;
/**
* The display name (view mode) on the entity involved in this XML request.
*
* @var string
*/
protected $displayName;
/**
* The loaded entity involved in this XML request.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* A Drupal entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* A Drupal entity respository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Factory to fetch required dependencies from container.
*/
public static function create(ContainerInterface $container) {
// Create the actual controller instance.
return new static($container
->get('config.factory'), $container
->get('request_stack'), $container
->get('http_kernel'), $container
->get('entity_type.manager'), $container
->get('entity.repository'));
}
/**
* {@inheritdoc}
*/
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);
// The field XML controller requires a couple extra services that are not
// initiated in the base controller class.
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
protected function init() {
// Collect the args from the path manually. We can't pass them in from the
// controller base because that class has to work for a variable number
// of args.
$attribs = $this->request->attributes
->get('_raw_variables');
// Set data sources as properties.
$this->entityType = $attribs
->get('entityType');
$this->entityId = $attribs
->get('entityId');
$this->fieldName = $attribs
->get('fieldName');
$this->displayName = $attribs
->get('displayName');
// Grab the loaded entity as well, translated appropriately.
$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) {
// All looks good.
return;
}
throw new \Exception('Cannot instantiate field-based Juicebox gallery as no entity can be loaded.');
}
/**
* {@inheritdoc}
*/
protected function access() {
// Drupal 8 has unified APIs for access checks so this is pretty easy.
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;
}
/**
* {@inheritdoc}
*/
protected function getGallery() {
// Build the field and gallery.
$field = $this->entity->{$this->fieldName}
->view($this->displayName);
// Make sure that the Juicebox is actually built.
if (!empty($field[0]['#gallery']) && $field[0]['#gallery'] instanceof JuiceboxGalleryInterface && $field[0]['#gallery']
->getId()) {
return $field[0]['#gallery'];
}
throw new \Exception('Cannot build Juicebox XML for field-based gallery.');
}
/**
* {@inheritdoc}
*/
protected function calculateXmlCacheTags() {
// Add tags for the entity that this XML comes from.
$entity_tags = $this->entity instanceof CacheableDependencyInterface ? $this->entity
->getCacheTags() : [];
// Also fetch the tags from the display configuration as that is where our
// gallery-specific settings are stored (so changes there should also
// invalidate the XML).
$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 this is not a custom display then we need to also include the
// default display cache tags as Drupal may reference this display
// elsewhere by the "default" label.
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JuiceboxXmlControllerBase:: |
protected | property | An array for Drupal cache tags that applies to the page request. | |
JuiceboxXmlControllerBase:: |
protected | property | A Drupal configuration factory service. | |
JuiceboxXmlControllerBase:: |
protected | property | The Symfony http kernel service. | |
JuiceboxXmlControllerBase:: |
protected | property | A Symfony request object for the current request. | |
JuiceboxXmlControllerBase:: |
protected | property | The global Juicebox configuration. | |
JuiceboxXmlControllerBase:: |
protected | function | Attempt to fetch the gallery's XML via a sub-request to another page. | |
JuiceboxXmlControllerBase:: |
public | function | Common controller for the Juicebox XML. | |
JuiceboxXmlControllerField:: |
protected | property | The display name (view mode) on the entity involved in this XML request. | |
JuiceboxXmlControllerField:: |
protected | property | The loaded entity involved in this XML request. | |
JuiceboxXmlControllerField:: |
protected | property | The numeric ID of the entity involved in this XML request. | |
JuiceboxXmlControllerField:: |
protected | property | A Drupal entity respository service. | |
JuiceboxXmlControllerField:: |
protected | property | The entity type involved in this XML request (e.g. "node"). | |
JuiceboxXmlControllerField:: |
protected | property | A Drupal entity type manager service. | |
JuiceboxXmlControllerField:: |
protected | property | The field name of the entity field involved in this XML request. | |
JuiceboxXmlControllerField:: |
protected | function |
Check access to the Drupal data that will be used to build the gallery. Overrides JuiceboxXmlControllerBase:: |
|
JuiceboxXmlControllerField:: |
protected | function |
Calculate any cache tags that should be applied to the XML. Overrides JuiceboxXmlControllerBase:: |
|
JuiceboxXmlControllerField:: |
public static | function |
Factory to fetch required dependencies from container. Overrides JuiceboxXmlControllerBase:: |
|
JuiceboxXmlControllerField:: |
protected | function |
Get the Juicebox gallery object. Overrides JuiceboxXmlControllerBase:: |
|
JuiceboxXmlControllerField:: |
protected | function |
Initialize the controller based on request data. Overrides JuiceboxXmlControllerBase:: |
|
JuiceboxXmlControllerField:: |
public | function |
Constructor. Overrides JuiceboxXmlControllerBase:: |