/**
  * Helper function to get the default output format from the current request.
  *
  * @param \RestfulBase $restful_handler
  *   The restful handler for the formatter.
  *
  * @return \RestfulFormatterBase
  *   The formatter plugin to use.
  */
 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) {
       // 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);
 }
Example #2
0
  /**
   * Call resource using the OPTIONS http method.
   *
   * This is an special method since it does not return anything in the body, it
   * only provides information about the selected endpoint. The information is
   * provided via HTTP headers.
   *
   * @param string $path
   *   (optional) The path.
   * @param array $request
   *   (optional) The request.
   *
   * @return array
   *   Information about the fields in the current resource.
   */
  public function options($path = '', array $request = array()) {
    $this->setMethod(\RestfulInterface::OPTIONS);
    $this->setPath($path);
    $this->setRequest($request);
    // A list of discoverable methods.
    $allowed_methods = array();
    foreach ($this->getControllers() as $pattern => $controllers) {
      // Find the controllers for the provided path.
      if ($pattern != $path && !($pattern && preg_match('/' . $pattern . '/', $path))) {
        continue;
      }
      $allowed_methods = array_keys($controllers);
      // We have found the controllers for this path.
      break;
    }
    if (!empty($allowed_methods)) {
      $this->setHttpHeaders('Access-Control-Allow-Methods', implode(',', $allowed_methods));
    }

    // Make your formatters discoverable.
    $formatter_names = $this->formatterNames();
    // Loop through all the formatters and add the Content-Type header to the
    // array.
    $accepted_formats = array();
    foreach ($formatter_names as $formatter_name) {
      $formatter = restful_get_formatter_handler($formatter_name, $this);
      $accepted_formats[] = $formatter->getContentTypeHeader();
    }
    if (!empty($accepted_formats)) {
      $this->setHttpHeaders('Accept', implode(',', $accepted_formats));
    }

    $output = array();
    // Default options for the discovery information.
    $discovery_defaults = array(
      'info' => array(
        'label' => '',
        'description' => '',
      ),
      // Describe the data.
      'data' => array(
        'type' => NULL,
        'read_only' => FALSE,
        'cardinality' => 1,
        'required' => FALSE,
      ),
      // Information about the form element.
      'form_element' => array(
        'type' => NULL,
        'default_value' => '',
        'placeholder' => '',
        'size' => NULL,
        'allowed_values' => NULL,
      ),
    );

    foreach ($this->getPublicFields() as $public_field => $field_info) {
      if (empty($field_info['discovery'])) {
        continue;
      }
      $output[$public_field] = drupal_array_merge_deep($discovery_defaults, $field_info['discovery']);
    }
    return $output;

  }