You are here

public function JuiceboxXmlControllerBase::xmlController in Juicebox HTML5 Responsive Image Galleries 8.3

Same name and namespace in other branches
  1. 8.2 src/Controller/JuiceboxXmlControllerBase.php \Drupal\juicebox\Controller\JuiceboxXmlControllerBase::xmlController()

Common controller for the Juicebox XML.

Return value

\Symfony\Component\HttpFoundation\Response A Symfony response object containing the XML information.

File

src/Controller/JuiceboxXmlControllerBase.php, line 90

Class

JuiceboxXmlControllerBase
Controller routines for Juicebox XML.

Namespace

Drupal\juicebox\Controller

Code

public function xmlController() {
  $xml = '';

  // If we have xml-source query parameters this indicates that the XML can
  // probably not be generated here from scratch. Instead we must depend on a
  // sub-request to another Drupal path (e.g., the gallery page) and search
  // for embedded XML there. This is an experimental method for special cases.
  $query = $this->request->query
    ->all();
  if (isset($query['xml-source-path']) && isset($query['xml-source-id'])) {
    $xml = $this
      ->fetchXmlSubRequest($query['xml-source-path'], $query['xml-source-id']);
  }

  // If a sub-request XML lookup does not apply then we build the gallery and
  // its XML from scratch. This is the more common and preferred method.
  if (empty($xml)) {
    try {

      // Initialize data and test access.
      $this
        ->init();
      if (!$this
        ->access()) {
        throw new AccessDeniedHttpException();
      }
      $gallery = $this
        ->getGallery();
      $xml = $gallery
        ->renderXml();

      // Set the cache tags from the type-specific method.
      $this->cacheTags = Cache::mergeTags($this->cacheTags, $this
        ->calculateXmlCacheTags());
    } catch (\Exception $e) {

      // An access denied exception should just be re-thrown.
      if ($e instanceof AccessDeniedHttpException) {
        throw $e;
      }

      // Otherwise we have an error. Log it and trigger a general 404
      // response.
      $message = 'Exception building Juicebox XML: !message in %function (line %line of %file).';
      watchdog_exception('juicebox', $e, $message);
      throw new NotFoundHttpException();
    }
  }

  // Calculate headers.
  $headers = [
    'Content-Type' => 'application/xml; charset=utf-8',
    'X-Robots-Tag' => 'noindex',
  ];
  if ($this->settings['enable_cors']) {
    $headers['Access-Control-Allow-Origin'] = '*';
  }

  // Capture relevant Drupal cache tags.
  $headers['X-Drupal-Cache-Tags'] = implode(' ', $this->cacheTags);

  // Package the XML as a Response object.
  return new Response($xml, 200, $headers);
}