| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides a module for giving access to submit any form via the
|
| 7 |
* drupal_execute() Form API call.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implements hook_restapi_menu(). Adds the execute callback.
|
| 12 |
*/
|
| 13 |
function restapi_execute_restapi_menu($may_cache) {
|
| 14 |
if (!$may_cache) {
|
| 15 |
if (arg(0) == '=' && arg(1) == 'execute' && arg(2)) {
|
| 16 |
$items[] = array(
|
| 17 |
'method' => 'GET',
|
| 18 |
'path' => '=/execute/'.arg(2),
|
| 19 |
'callback' => 'restapi_execute_show_form',
|
| 20 |
'callback arguments' => array(arg(2)),
|
| 21 |
);
|
| 22 |
|
| 23 |
$items[] = array(
|
| 24 |
'method' => 'POST',
|
| 25 |
'path' => '=/execute/'.arg(2),
|
| 26 |
'callback' => 'restapi_execute_submit_form',
|
| 27 |
'callback arguments' => array(arg(2)),
|
| 28 |
);
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
return $items;
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implements hook_restapi_help(). Shows help for GET and POST of a form.
|
| 37 |
*/
|
| 38 |
function restapi_execute_restapi_help() {
|
| 39 |
$help['prototypes'][] = array(
|
| 40 |
'method' => 'GET',
|
| 41 |
'path' => '/=/execute/<form-id>',
|
| 42 |
'description' => 'show the form',
|
| 43 |
);
|
| 44 |
|
| 45 |
$help['prototypes'][] = array(
|
| 46 |
'method' => 'POST',
|
| 47 |
'path' => '/=/execute/<form-id>',
|
| 48 |
'description' => 'drupal_execute() the form',
|
| 49 |
);
|
| 50 |
|
| 51 |
return $help;
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Handles GET /=/execute/<form-id>
|
| 56 |
*
|
| 57 |
* @param $form_id the form ID of the form to show
|
| 58 |
*/
|
| 59 |
function restapi_execute_restapi_show_form($data, $form_id) {
|
| 60 |
if (!user_access('access rest api')) {
|
| 61 |
return drupal_access_denied();
|
| 62 |
}
|
| 63 |
|
| 64 |
print restapi_serialize(drupal_get_form($form_id));
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Handles POST /=/execute/<form-id>
|
| 69 |
*
|
| 70 |
* @param $form_id the form ID of the form to execute
|
| 71 |
*/
|
| 72 |
function restapi_execute_restapi_submit_form($data, $form_id) {
|
| 73 |
if (!user_access('access rest api')) {
|
| 74 |
return drupal_access_denied();
|
| 75 |
}
|
| 76 |
|
| 77 |
$result = drupal_execute($form_id, $data);
|
| 78 |
print restapi_serialize($result);
|
| 79 |
}
|