You are here

public function RestfulBase::options in RESTful 7

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.

Parameters

string $path: (optional) The path.

array $request: (optional) The request.

Return value

array Information about the fields in the current resource.

File

plugins/restful/RestfulBase.php, line 726
Contains RestfulBase.

Class

RestfulBase
Class \RestfulBase

Code

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