You are here

class JuiceboxXmlControllerViewsStyle in Juicebox HTML5 Responsive Image Galleries 8.3

Same name and namespace in other branches
  1. 8.2 src/Controller/JuiceboxXmlControllerViewsStyle.php \Drupal\juicebox\Controller\JuiceboxXmlControllerViewsStyle

Controller routines for field-based XML.

Hierarchy

Expanded class hierarchy of JuiceboxXmlControllerViewsStyle

File

src/Controller/JuiceboxXmlControllerViewsStyle.php, line 12

Namespace

Drupal\juicebox\Controller
View source
class JuiceboxXmlControllerViewsStyle extends JuiceboxXmlControllerBase {

  /**
   * The view machine name for the view involved in this XML request.
   *
   * @var string
   */
  protected $viewName;

  /**
   * The view display name for the view involved in this XML request.
   *
   * @var string
   */
  protected $displayName;

  /**
   * The loaded view involved in this XML request.
   *
   * @var \Drupal\views\ViewExecutable
   */
  protected $view;

  /**
   * An indexed array of view args that apply to view used in this XML request.
   *
   * @var array
   */
  protected $viewArgs = [];

  /**
   * {@inheritdoc}
   */
  protected function init() {
    $attribs = $this->request->attributes
      ->get('_raw_variables');

    // Set data sources as properties.
    $this->viewName = $attribs
      ->get('viewName');
    $this->displayName = $attribs
      ->get('displayName');
    $this->viewArgs = $this
      ->queryToArgs();

    // Load the view itself.
    $this->view = Views::getView($this->viewName);
    if (is_object($this->view) && $this->view instanceof ViewExecutable) {

      // All looks good.
      return;
    }
    throw new \Exception('Cannot instantiate view-based Juicebox gallery as no view can be loaded.');
  }

  /**
   * {@inheritdoc}
   */
  protected function access() {
    return $this->view
      ->access($this->displayName);
  }

  /**
   * {@inheritdoc}
   */
  protected function getGallery() {
    $this->view
      ->setDisplay($this->displayName);
    $this->view
      ->setArguments($this->viewArgs);
    $rendered_view = $this->view
      ->render();

    // Make sure that the Juicebox is actually built.
    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.');
  }

  /**
   * {@inheritdoc}
   */
  protected function calculateXmlCacheTags() {

    // Pull the tags directly out of the view object.
    return $this->view
      ->getCacheTags();
  }

  /**
   * Utility to extract the current set of view args from query params.
   *
   * @return array
   *   A indexed array of the view args.
   */
  protected function queryToArgs() {
    $args = [];

    // Get the args from the query params.
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JuiceboxXmlControllerBase::$cacheTags protected property An array for Drupal cache tags that applies to the page request.
JuiceboxXmlControllerBase::$configFactory protected property A Drupal configuration factory service.
JuiceboxXmlControllerBase::$httpKernel protected property The Symfony http kernel service.
JuiceboxXmlControllerBase::$request protected property A Symfony request object for the current request.
JuiceboxXmlControllerBase::$settings protected property The global Juicebox configuration.
JuiceboxXmlControllerBase::create public static function Factory to fetch required dependencies from container. Overrides ContainerInjectionInterface::create 1
JuiceboxXmlControllerBase::fetchXmlSubRequest protected function Attempt to fetch the gallery's XML via a sub-request to another page.
JuiceboxXmlControllerBase::xmlController public function Common controller for the Juicebox XML.
JuiceboxXmlControllerBase::__construct public function Constructor. 1
JuiceboxXmlControllerViewsStyle::$displayName protected property The view display name for the view involved in this XML request.
JuiceboxXmlControllerViewsStyle::$view protected property The loaded view involved in this XML request.
JuiceboxXmlControllerViewsStyle::$viewArgs protected property An indexed array of view args that apply to view used in this XML request.
JuiceboxXmlControllerViewsStyle::$viewName protected property The view machine name for the view involved in this XML request.
JuiceboxXmlControllerViewsStyle::access protected function Check access to the Drupal data that will be used to build the gallery. Overrides JuiceboxXmlControllerBase::access
JuiceboxXmlControllerViewsStyle::calculateXmlCacheTags protected function Calculate any cache tags that should be applied to the XML. Overrides JuiceboxXmlControllerBase::calculateXmlCacheTags
JuiceboxXmlControllerViewsStyle::getGallery protected function Get the Juicebox gallery object. Overrides JuiceboxXmlControllerBase::getGallery
JuiceboxXmlControllerViewsStyle::init protected function Initialize the controller based on request data. Overrides JuiceboxXmlControllerBase::init
JuiceboxXmlControllerViewsStyle::queryToArgs protected function Utility to extract the current set of view args from query params.