Skip to content

Instantly share code, notes, and snippets.

@amitaibu
Last active February 21, 2016 17:57
Show Gist options
  • Save amitaibu/10e9d462c72d7b722685 to your computer and use it in GitHub Desktop.
Save amitaibu/10e9d462c72d7b722685 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Turn Drupal to a RESTful server, following best practices.
*/
include_once __DIR__ . '/restful.entity.inc';
include_once __DIR__ . '/restful.cache.inc';
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginBase;
use Drupal\restful\Exception\RestfulException;
use Drupal\restful\Http\HttpHeader;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Http\ResponseInterface;
use Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource;
use Drupal\restful\Plugin\resource\Decorators\RateLimitDecoratedResource;
use Drupal\restful\Plugin\resource\ResourceInterface;
use Drupal\restful\RestfulManager;
/**
* Implements hook_menu().
*/
function restful_menu() {
$base_path = variable_get('restful_hook_menu_base_path', 'api');
$items = array();
$plugins = restful()
->getResourceManager()
->getPlugins();
foreach ($plugins->getIterator() as $plugin) {
if (!$plugin instanceof ResourceInterface) {
// If the plugin is disabled $plugin gets set to NULL. If that is the case
// do not set any menu values based on it.
continue;
}
$plugin_definition = $plugin->getPluginDefinition();
if (!$plugin_definition['hookMenu']) {
// Plugin explicitly declared no hook menu should be created automatically
// for it.
continue;
}
$item = array(
'title' => $plugin_definition['name'],
'access callback' => RestfulManager::FRONT_CONTROLLER_ACCESS_CALLBACK,
'access arguments' => array($plugin_definition['resource']),
'page callback' => RestfulManager::FRONT_CONTROLLER_CALLBACK,
'page arguments' => array($plugin_definition['resource']),
'delivery callback' => 'restful_delivery',
'type' => MENU_CALLBACK,
);
// If there is no specific menu item allow the different version variations.
if (!isset($plugin_definition['menuItem'])) {
// Add the version string to the arguments.
$item['access arguments'][] = 1;
$item['page arguments'][] = 1;
// Ex: api/v1.2/articles
$items[$base_path . '/v' . $plugin_definition['majorVersion'] . '.' . $plugin_definition['minorVersion'] . '/' . $plugin_definition['resource']] = $item;
// Ex: api/v1/articles will use the latest minor version.
$items[$base_path . '/v' . $plugin_definition['majorVersion'] . '/' . $plugin_definition['resource']] = $item;
// Ex: api/articles will use the header or the latest version.
// Do not add the version string to the arguments.
$item['access arguments'] = $item['page arguments'] = array(1);
$items[$base_path . '/' . $plugin_definition['resource']] = $item;
}
else {
$path = implode('/', array($base_path, $plugin_definition['menuItem']));
// Remove trailing slashes that can lead to 404 errors.
$path = rtrim($path, '/');
$items[$path] = $item;
}
}
// Make sure the Login endpoint has the correct access callback.
if (!empty($items[$base_path . '/login'])) {
$items[$base_path . '/login']['access callback'] = 'user_is_anonymous';
}
// Add administration page.
$items['admin/config/services/restful'] = array(
'title' => 'RESTful',
'description' => 'Administer the RESTful module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('restful_admin_settings'),
'access arguments' => array('administer restful'),
'file' => 'restful.admin.inc',
);
$items['admin/config/services/restful/restful'] = $items['admin/config/services/restful'];
$items['admin/config/services/restful/restful']['type'] = MENU_DEFAULT_LOCAL_TASK;
// Add cache administration page.
$items['admin/config/services/restful/cache'] = array(
'title' => 'Cache',
'description' => 'Administer the RESTful module cache system.',
'page callback' => 'drupal_get_form',
'page arguments' => array('restful_admin_cache_settings'),
'access arguments' => array('administer restful'),
'file' => 'restful.cache.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
return $items;
}
/**
* Implements hook_permission().
*/
function restful_permission() {
return array(
'administer restful' => array(
'title' => t('Administer the RESTful module'),
'description' => t('Access the administration pages for the RESTful module.'),
),
'administer restful resources' => array(
'title' => t('Administer the resources'),
'description' => t('Perform operations on the resources.'),
),
'restful clear render caches' => array(
'title' => t('Clear RESTful render caches'),
'description' => t('Clear the render caches and their correspoding cache fragments.'),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment