You are here

public static function RestfulManager::outputFormat in RESTful 7

Helper function to get the default output format from the current request.

Parameters

\RestfulBase $restful_handler: The restful handler for the formatter.

Return value

\RestfulFormatterBase The formatter plugin to use.

3 calls to RestfulManager::outputFormat()
RestfulBase::formatter in plugins/restful/RestfulBase.php
Get the formatter handler for the current restful formatter.
RestfulSimpleJsonTestCase::testSimpleJson in tests/RestfulSimpleJsonTestCase.test
Test the Simple JSON formatter.
restful_delivery in ./restful.module
Returns data in JSON format.

File

includes/RestfulManager.php, line 225
Contains \RestfulManager.

Class

RestfulManager
@file Contains \RestfulManager.

Code

public static function outputFormat(\RestfulBase $restful_handler = NULL) {
  $restful_handler = $restful_handler ? $restful_handler : restful_get_restful_handler_for_path();
  if ($restful_handler && ($formatter_name = $restful_handler
    ->getPluginKey('formatter'))) {
    return restful_get_formatter_handler($formatter_name, $restful_handler);
  }

  // Sometimes we will get a default Accept: */* in that case we want to return
  // the default content type and not just any.
  if (!empty($GLOBALS['_SERVER']['HTTP_ACCEPT']) && $GLOBALS['_SERVER']['HTTP_ACCEPT'] != '*/*') {
    foreach (explode(',', $GLOBALS['_SERVER']['HTTP_ACCEPT']) as $accepted_content_type) {

      // Bypass case where Content-Type HTTP_ACCEPT looks like '*/*,image/webp'.
      if ($accepted_content_type == '*/*') {
        continue;
      }

      // Loop through all the formatters and find the first one that matches the
      // Content-Type header.
      foreach (restful_get_formatter_plugins() as $formatter_info) {
        $formatter = restful_get_formatter_handler($formatter_info['name'], $restful_handler);
        if (static::matchContentType($formatter
          ->getContentTypeHeader(), $accepted_content_type)) {
          return $formatter;
        }
      }
    }
  }
  $formatter_name = variable_get('restful_default_output_formatter', 'json');
  return restful_get_formatter_handler($formatter_name, $restful_handler);
}