JuiceboxXmlControllerViewsStyle.php in Juicebox HTML5 Responsive Image Galleries 8.3
File
src/Controller/JuiceboxXmlControllerViewsStyle.php
View source
<?php
namespace Drupal\juicebox\Controller;
use Drupal\juicebox\JuiceboxGalleryInterface;
use Drupal\views\Views;
use Drupal\views\ViewExecutable;
class JuiceboxXmlControllerViewsStyle extends JuiceboxXmlControllerBase {
protected $viewName;
protected $displayName;
protected $view;
protected $viewArgs = [];
protected function init() {
$attribs = $this->request->attributes
->get('_raw_variables');
$this->viewName = $attribs
->get('viewName');
$this->displayName = $attribs
->get('displayName');
$this->viewArgs = $this
->queryToArgs();
$this->view = Views::getView($this->viewName);
if (is_object($this->view) && $this->view instanceof ViewExecutable) {
return;
}
throw new \Exception('Cannot instantiate view-based Juicebox gallery as no view can be loaded.');
}
protected function access() {
return $this->view
->access($this->displayName);
}
protected function getGallery() {
$this->view
->setDisplay($this->displayName);
$this->view
->setArguments($this->viewArgs);
$rendered_view = $this->view
->render();
if (!empty($rendered_view['#rows']['#gallery']) && $rendered_view['#rows']['#gallery'] instanceof JuiceboxGalleryInterface && $rendered_view['#rows']['#gallery']
->getId()) {
return $rendered_view['#rows']['#gallery'];
}
throw new \Exception('Cannot build Juicebox XML for view-based gallery.');
}
protected function calculateXmlCacheTags() {
return $this->view
->getCacheTags();
}
protected function queryToArgs() {
$args = [];
$query_args = $this->request->query
->all();
foreach ($query_args as $param => $value) {
if (preg_match('/^arg_[0-9]+$/', $param)) {
list(, $key) = explode('_', $param);
$args[$key] = $value;
}
}
return $args;
}
}