Skip to content
Snippets Groups Projects
Select Git revision
  • a6ffb282610159d168a1e64cffdd5d1c0ab5d78f
  • 11.x default protected
  • 11.3.x protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 10.5.4 protected
  • 11.2.5 protected
  • 10.5.3 protected
  • 11.2.4 protected
  • 10.5.2 protected
  • 11.2.3 protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
41 results

ContentNegotiation.php

Blame
  • catch's avatar
    Issue #1992834 by alexpott: Fixed Unnecessarily setting up the array of...
    catch authored
    Issue #1992834 by alexpott: Fixed Unnecessarily setting up the array of priority formats for content negotiation within a loop.
    3ad81ec0
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ContentNegotiation.php 1.72 KiB
    <?php
    
    /**
     * @file
     * Definition of Drupal\Core\ContentNegotiation.
     */
    
    namespace Drupal\Core;
    
    use Symfony\Component\HttpFoundation\Request;
    
    /**
     * 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.
     */
    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';
      }
    }