class ContentNegotiation

This class is a central library for content type negotiation.

@todo Replace this class with a real content negotiation library based on mod_negotiation. Development of that is a work in progress.

Hierarchy

Expanded class hierarchy of ContentNegotiation

8 files declare their use of ContentNegotiation
AjaxEnhancer.php in drupal/core/lib/Drupal/Core/Routing/Enhancer/AjaxEnhancer.php
Contains \Drupal\Core\Routing\Enhancer\AjaxEnhancer.
ContentControllerEnhancer.php in drupal/core/lib/Drupal/Core/Routing/Enhancer/ContentControllerEnhancer.php
Contains \Drupal\Core\Routing\Enhancer\ContentControllerEnhancer.
EntityFormEnhancer.php in drupal/core/lib/Drupal/Core/Entity/Enhancer/EntityFormEnhancer.php
Contains \Drupal\Core\Entity\Enhancer\EntityFormEnhancer.
ExceptionController.php in drupal/core/lib/Drupal/Core/Controller/ExceptionController.php
Contains \Drupal\Core\Controller\ExceptionController.
ExceptionControllerTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/System/ExceptionControllerTest.php

... See full list

1 string reference to 'ContentNegotiation'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml
1 service uses ContentNegotiation

File

drupal/core/lib/Drupal/Core/ContentNegotiation.php, line 18
Definition of Drupal\Core\ContentNegotiation.

Namespace

Drupal\Core
View source
class ContentNegotiation {

  /**
   * Gets the normalized type of a request.
   *
   * The normalized type is a short, lowercase version of the format, such as
   * 'html', 'json' or 'atom'.
   *
   * @param Symfony\Component\HttpFoundation\Request $request
   *   The request object from which to extract the content type.
   *
   * @return string
   *   The normalized type of a given request.
   */
  public function getContentType(Request $request) {

    // AJAX iframe uploads need special handling, because they contain a JSON
    // response wrapped in <textarea>.
    if ($request
      ->get('ajax_iframe_upload', FALSE)) {
      return 'iframeupload';
    }

    // Check all formats, if priority format is found return it.
    $first_found_format = FALSE;
    $priority = array(
      'html',
      'drupal_ajax',
      'drupal_modal',
      'drupal_dialog',
    );
    foreach ($request
      ->getAcceptableContentTypes() as $mime_type) {
      $format = $request
        ->getFormat($mime_type);
      if (in_array($format, $priority, TRUE)) {
        return $format;
      }
      if (!is_null($format) && !$first_found_format) {
        $first_found_format = $format;
      }
    }

    // No HTML found, return first found.
    if ($first_found_format) {
      return $first_found_format;
    }
    if ($request
      ->isXmlHttpRequest()) {
      return 'ajax';
    }

    // Do HTML last so that it always wins.
    return 'html';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentNegotiation::getContentType public function Gets the normalized type of a request.