5 * Hooks provided by Services for the definition of new services.
13 * Defines function signatures for resources available to services.
15 * Functionally this is very similar to the way hook_menu() works, and in many
16 * ways Services can be seen as an abstraction layer on top of hook_menu().
19 * An associative array which defines available resources.
21 * The associative array which defines services has five possible top
22 * level keys. The operations array has five possible keys representing
23 * the CRUD operations.
36 * The #api_version is the services resource API that the resource is written
37 * for. This makes it possible for services to upgrade resource declarations
38 * if the format changes. See services_resource_api_version_info(). If no
39 * version is given then 3001 is assumed. Which is the format used before the
40 * versioning was introduced.
42 * The CRUD functions in 'operations' are pretty self-explanatory. Index is
43 * an extra CRUD-type function that allows you to create pageable lists.
45 * Actions are performed directly on the resource type, not a individual
46 * resource. The following example is hypothetical (but plausible). Say
47 * that you want to expose a API for the apachesolr module. One of the
48 * things that could be exposed is the functionality to reindex the whole
49 * site at apachesolr/reindex.
51 * Targeted actions acts on a individual resource. A good, but again -
52 * hypothetical, example would be the publishing and unpublishing of nodes
53 * at node/123/publish.
55 * Relationship requests are convenience methods (sugar) to get something
56 * thats related to a individual resource. A real example would be the
57 * ability to get the files for a node at node/123/files.
59 * The first five (the CRUD functions + index) define the indvidual service
60 * callbacks for each function. However 'actions', 'targeted_actions',
61 * and 'relationships' can contain multiple callbacks.
63 * For those familiar with Services 2.x, these callbacks are created
64 * similarly, but the keys have changed around a bit. The following keys
65 * are used to describe a callback.
67 * - help: Text describing what this callback does.
68 * - callback: The name of a function to call when this resource is
70 * - file: an array describing the file which contains the callback
72 * - access callback: The name of a function to call to check whether
73 * the requesting user has permission to access this resource. If not
74 * specified, this defaults to 'user_access'.
75 * - access callback file: an array describing the file which contains the
76 * access callback function. This attribute only needs to be supplied if
77 * the method callback and the access callback are defined in different
78 * files, for example when a method callback is overridden using
79 * hook_services_resources_alter but the access callback is not
80 * - access arguments: The arguments to pass to the access callback.
81 * - access arguments append: A boolean indicating whether the resource's
82 * arguments should be appended to the access arguments. This can be useful
83 * in situations where an access callback is specific to the particular
84 * item ('edit all nodes' vs 'edit my nodes'). Defaults to FALSE.
85 * - args: an array describing the arguments which should be passed to this
86 * resource when it is called. Each element in the array is an associative
87 * array containing the following keys:
89 * - name: The name of this argument.
90 * - type: The data type of this argument (int, string, array)
91 * - description: Text describing this argument's usage.
92 * - optional: A boolean indicating whether or not this argument is optional.
93 * - source: Where this argument should be retrieved from. This can be
94 * 'data' (indicating the POST data), 'param' (indicating the query
95 * string) or 'path' (indicating the url path). In the case of path,
96 * an additional parameter must be passed indicating the index to be used.
97 * - default value: this is a value that will be passed to the method for
98 * this particular argument if no argument value is passed
100 * A detailed example of creating a new resource can be found at
101 * http://drupal.org/node/783460 and more information about how
102 * REST resources are managed can be found at http://drupal.org/node/783254.
104 function hook_services_resources() {
105 $node_resource = array(
108 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
109 'callback' => '_node_resource_retrieve',
114 'source' => array('path' => 0),
116 'description' => 'The nid of the node to get',
119 'access callback' => '_node_resource_access',
120 'access callback file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
121 'access arguments' => array('view'),
122 'access arguments append' => TRUE
,
125 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
126 'callback' => '_node_resource_create',
132 'description' => 'The node data to create',
136 'access callback' => '_node_resource_access',
137 'access arguments' => array('create'),
138 'access arguments append' => TRUE
,
141 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
142 'callback' => '_node_resource_update',
147 'source' => array('path' => 0),
149 'description' => 'The nid of the node to get',
155 'description' => 'The node data to update',
159 'access callback' => '_node_resource_access',
160 'access arguments' => array('update'),
161 'access arguments append' => TRUE
,
164 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
165 'callback' => '_node_resource_delete',
170 'source' => array('path' => 0),
174 'access callback' => '_node_resource_access',
175 'access arguments' => array('delete'),
176 'access arguments append' => TRUE
,
179 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
180 'callback' => '_node_resource_index',
186 'description' => 'The zero-based index of the page to get, defaults to 0.',
187 'default value' => 0,
188 'source' => array('param' => 'page'),
194 'description' => 'The fields to get.',
195 'default value' => '*',
196 'source' => array('param' => 'fields'),
199 'name' => 'parameters',
202 'description' => 'Parameters array',
203 'default value' => array(),
204 'source' => array('param' => 'parameters'),
207 'access arguments' => array('access content'),
209 'relationships' => array(
211 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
212 'help' => t('This method returns files associated with a node.'),
213 'access callback' => '_node_resource_access',
214 'access arguments' => array('view'),
215 'access arguments append' => TRUE
,
216 'callback' => '_node_resource_load_node_files',
221 'source' => array('path' => 0),
223 'description' => 'The nid of the node whose files we are getting',
226 'name' => 'file_contents',
228 'description' => t('To return file contents or not.'),
229 'source' => array('path' => 2),
231 'default value' => TRUE
,
238 if (module_exists('comment')) {
240 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
241 'help' => t('This method returns the number of new comments on a given node.'),
242 'access callback' => 'user_access',
243 'access arguments' => array('access comments'),
244 'access arguments append' => FALSE
,
245 'callback' => '_node_resource_load_node_comments',
250 'description' => t('The node id to load comments for.'),
251 'source' => array('path' => 0),
257 'description' => t('Number of comments to load.'),
258 'source' => array('param' => 'count'),
264 'description' => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
265 'source' => array('param' => 'offset'),
270 $node_resource['node']['relationships']['comments'] = $comments;
272 return $node_resource;