You are here

function restws_menu_alter in RESTful Web Services 7.2

Same name and namespace in other branches
  1. 7 restws.module \restws_menu_alter()

Implements hook_menu_alter().

File

./restws.module, line 253
RESTful web services module.

Code

function restws_menu_alter(&$items) {
  foreach (restws_get_resource_info() as $resource => $info) {

    // Resource full path (e.g. /node/% or /user/%) for accessing specific
    // resources.
    $menu_path = isset($info['menu_path']) ? $info['menu_path'] . '/%' : $resource . '/%';

    // Replace existing page callbacks with our own (e.g. node/%)
    if (isset($items[$menu_path])) {

      // Prepend the page callback and the resource to the page arguments.
      // So we can re-use it on standard HTML page requests.
      array_unshift($items[$menu_path]['page arguments'], $resource, $items[$menu_path]['page callback']);
      $items[$menu_path]['page callback'] = 'restws_page_callback';
    }
    elseif (isset($items[$menu_path . $resource])) {
      $menu_path = $menu_path . $resource;
      array_unshift($items[$menu_path]['page arguments'], $resource, $items[$menu_path]['page callback']);
      $items[$menu_path]['page callback'] = 'restws_page_callback';
    }
    else {
      $items[$menu_path] = array(
        'page callback' => 'restws_page_callback',
        'page arguments' => array(
          $resource,
          'drupal_not_found',
        ),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
      );
    }

    // Resource base path (e.g. /node or /user) for creating resources.
    $menu_path = isset($info['menu_path']) ? substr($menu_path, 0, strlen($menu_path) - 2) : $resource;
    if (isset($items[$menu_path])) {

      // Prepend the page callback and the resource to the page arguments.
      if (!isset($items[$menu_path]['page arguments'])) {
        $items[$menu_path]['page arguments'] = array();
      }
      array_unshift($items[$menu_path]['page arguments'], $resource, $items[$menu_path]['page callback']);
      $items[$menu_path]['page callback'] = 'restws_page_callback';
    }
    else {
      $items[$menu_path] = array(
        'page callback' => 'restws_page_callback',
        'page arguments' => array(
          $resource,
          'drupal_not_found',
        ),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
      );
    }

    // Querying menu paths.
    foreach (array_keys(restws_get_format_info()) as $format) {

      // Resource base path URLs with the suffixes (e.g. node.json or user.xml)
      // for querying.
      if (isset($items["{$menu_path}.{$format}"])) {

        // Prepend the page callback and the resource to the page arguments.
        if (!isset($items["{$menu_path}.{$format}"]['page arguments'])) {
          $items["{$menu_path}.{$format}"]['page arguments'] = array();
        }
        array_unshift($items["{$menu_path}.{$format}"]['page arguments'], $resource, $items["{$menu_path}.{$format}"]['page callback']);
        $items["{$menu_path}.{$format}"]['page callback'] = 'restws_page_callback';
      }
      else {
        $items["{$menu_path}.{$format}"] = array(
          'page callback' => 'restws_page_callback',
          'page arguments' => array(
            $resource,
            'drupal_not_found',
          ),
          'access callback' => TRUE,
          'type' => MENU_CALLBACK,
        );
      }
    }
  }
}