You are here

protected function DefaultExceptionSubscriber::getFormat in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber::getFormat()

Gets the error-relevant format from the request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

string The format as which to treat the exception.

1 call to DefaultExceptionSubscriber::getFormat()
DefaultExceptionSubscriber::onException in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php
Handles errors for this subscriber.

File

core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php, line 194
Contains \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber.

Class

DefaultExceptionSubscriber
Last-chance handler for exceptions.

Namespace

Drupal\Core\EventSubscriber

Code

protected function getFormat(Request $request) {
  $format = $request->query
    ->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request
    ->getRequestFormat());

  // These are all JSON errors for our purposes. Any special handling for
  // them can/should happen in earlier listeners if desired.
  if (in_array($format, [
    'drupal_modal',
    'drupal_dialog',
    'drupal_ajax',
  ])) {
    $format = 'json';
  }

  // Make an educated guess that any Accept header type that includes "json"
  // can probably handle a generic JSON response for errors. As above, for
  // any format this doesn't catch or that wants custom handling should
  // register its own exception listener.
  foreach ($request
    ->getAcceptableContentTypes() as $mime) {
    if (strpos($mime, 'html') === FALSE && strpos($mime, 'json') !== FALSE) {
      $format = 'json';
    }
  }
  return $format;
}