protected function DefaultExceptionSubscriber::getFormat

protected DefaultExceptionSubscriber::getFormat(Request $request)

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.

File

core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php, line 209

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!EventSubscriber!DefaultExceptionSubscriber.php/function/DefaultExceptionSubscriber::getFormat/8.1.x