1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Hooks provided by Services for the definition of new services. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * @addtogroup hooks |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Implementation of hook_services_resources(). |
---|
14 | * Defines function signatures for resources available to services. |
---|
15 | * |
---|
16 | * Functionally this is very similar to the way hook_menu() works, and in many |
---|
17 | * ways Services can be seen as an abstraction layer on top of hook_menu(). |
---|
18 | * |
---|
19 | * @return |
---|
20 | * An associative array which defines available resources. |
---|
21 | * |
---|
22 | * The associative array which defines services has seven possible top |
---|
23 | * level keys: |
---|
24 | * |
---|
25 | * - create |
---|
26 | * - retrieve |
---|
27 | * - update |
---|
28 | * - delete |
---|
29 | * - actions |
---|
30 | * - targeted_actions |
---|
31 | * - relationships |
---|
32 | * |
---|
33 | * |
---|
34 | * The first four (the CRUD functions) define the indvidual service |
---|
35 | * callbacks for each function. However 'actions' and 'targeted_actions' |
---|
36 | * can contain multiple callbacks. |
---|
37 | * |
---|
38 | * For those familiar with Services 2.x, these callbacks are created |
---|
39 | * similarly, but the keys have changed around a bit. The following keys |
---|
40 | * are used to describe a callback. |
---|
41 | * |
---|
42 | * - help: Text describing what this callback does. |
---|
43 | * - callback: The name of a function to call when this resource is |
---|
44 | * requested. |
---|
45 | * - file: an array describing the file which contains the callback |
---|
46 | * function |
---|
47 | * - access callback: The name of a function to call to check whether |
---|
48 | * the requesting user has permission to access this resource. If not |
---|
49 | * specified, this defaults to 'user_access'. |
---|
50 | * - access callback file: an array describing the file which contains the |
---|
51 | * access callback function. This attribute only needs to be supplied if |
---|
52 | * the method callback and the access callback are defined in different |
---|
53 | * files, for example when a method callback is overridden using |
---|
54 | * hook_services_resources_alter but the access callback is not |
---|
55 | * - access arguments: The arguments to pass to the access callback. |
---|
56 | * - access arguments append: A boolean indicating whether the resource's |
---|
57 | * arguments should be appended to the access arguments. This can be useful |
---|
58 | * in situations where an access callback is specific to the particular |
---|
59 | * item ('edit all nodes' vs 'edit my nodes'). Defaults to FALSE. |
---|
60 | * - args: an array describing the arguments which should be passed to this |
---|
61 | * resource when it is called. Each element in the array is an associative |
---|
62 | * array containing the following keys: |
---|
63 | * |
---|
64 | * - name: The name of this argument. |
---|
65 | * - type: The data type of this argument (int, string, array) |
---|
66 | * - description: Text describing this argument's usage. |
---|
67 | * - optional: A boolean indicating whether or not this argument is optional. |
---|
68 | * - source: Where this argument should be retrieved from. This can be |
---|
69 | * 'data' (indicating the POST data), 'param' (indicating the query |
---|
70 | * string) or 'path' (indicating the url path). In the case of path, |
---|
71 | * an additional parameter must be passed indicating the index to be used. |
---|
72 | * - default value: this is a value that will be passed to the method for this particular argument if no argument value is passed |
---|
73 | */ |
---|
74 | function hook_services_resources() { |
---|
75 | $node_resource = array( |
---|
76 | 'node' => array( |
---|
77 | 'retrieve' => array( |
---|
78 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
79 | 'callback' => '_node_resource_retrieve', |
---|
80 | 'args' => array( |
---|
81 | array( |
---|
82 | 'name' => 'nid', |
---|
83 | 'optional' => FALSE, |
---|
84 | 'source' => array('path' => 0), |
---|
85 | 'type' => 'int', |
---|
86 | 'description' => 'The nid of the node to get', |
---|
87 | ), |
---|
88 | ), |
---|
89 | 'access callback' => '_node_resource_access', |
---|
90 | 'access callback file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
91 | 'access arguments' => array('view'), |
---|
92 | 'access arguments append' => TRUE, |
---|
93 | ), |
---|
94 | 'create' => array( |
---|
95 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
96 | 'callback' => '_node_resource_create', |
---|
97 | 'args' => array( |
---|
98 | array( |
---|
99 | 'name' => 'node', |
---|
100 | 'optional' => FALSE, |
---|
101 | 'source' => 'data', |
---|
102 | 'description' => 'The node data to create', |
---|
103 | 'type' => 'array', |
---|
104 | ), |
---|
105 | ), |
---|
106 | 'access callback' => '_node_resource_access', |
---|
107 | 'access arguments' => array('create'), |
---|
108 | 'access arguments append' => TRUE, |
---|
109 | ), |
---|
110 | 'update' => array( |
---|
111 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
112 | 'callback' => '_node_resource_update', |
---|
113 | 'args' => array( |
---|
114 | array( |
---|
115 | 'name' => 'nid', |
---|
116 | 'optional' => FALSE, |
---|
117 | 'source' => array('path' => 0), |
---|
118 | 'type' => 'int', |
---|
119 | 'description' => 'The nid of the node to get', |
---|
120 | ), |
---|
121 | array( |
---|
122 | 'name' => 'node', |
---|
123 | 'optional' => FALSE, |
---|
124 | 'source' => 'data', |
---|
125 | 'description' => 'The node data to update', |
---|
126 | 'type' => 'array', |
---|
127 | ), |
---|
128 | ), |
---|
129 | 'access callback' => '_node_resource_access', |
---|
130 | 'access arguments' => array('update'), |
---|
131 | 'access arguments append' => TRUE, |
---|
132 | ), |
---|
133 | 'delete' => array( |
---|
134 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
135 | 'callback' => '_node_resource_delete', |
---|
136 | 'args' => array( |
---|
137 | array( |
---|
138 | 'name' => 'nid', |
---|
139 | 'optional' => FALSE, |
---|
140 | 'source' => array('path' => 0), |
---|
141 | 'type' => 'int', |
---|
142 | ), |
---|
143 | ), |
---|
144 | 'access callback' => '_node_resource_access', |
---|
145 | 'access arguments' => array('delete'), |
---|
146 | 'access arguments append' => TRUE, |
---|
147 | ), |
---|
148 | 'index' => array( |
---|
149 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
150 | 'callback' => '_node_resource_index', |
---|
151 | 'args' => array( |
---|
152 | array( |
---|
153 | 'name' => 'page', |
---|
154 | 'optional' => TRUE, |
---|
155 | 'type' => 'int', |
---|
156 | 'description' => 'The zero-based index of the page to get, defaults to 0.', |
---|
157 | 'default value' => 0, |
---|
158 | 'source' => array('param' => 'page'), |
---|
159 | ), |
---|
160 | array( |
---|
161 | 'name' => 'fields', |
---|
162 | 'optional' => TRUE, |
---|
163 | 'type' => 'string', |
---|
164 | 'description' => 'The fields to get.', |
---|
165 | 'default value' => '*', |
---|
166 | 'source' => array('param' => 'fields'), |
---|
167 | ), |
---|
168 | array( |
---|
169 | 'name' => 'parameters', |
---|
170 | 'optional' => TRUE, |
---|
171 | 'type' => 'array', |
---|
172 | 'description' => 'Parameters array', |
---|
173 | 'default value' => array(), |
---|
174 | 'source' => array('param' => 'parameters'), |
---|
175 | ), |
---|
176 | ), |
---|
177 | 'access arguments' => array('access content'), |
---|
178 | ), |
---|
179 | 'relationships' => array( |
---|
180 | 'files' => array( |
---|
181 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
182 | 'help' => t('This method returns files associated with a node.'), |
---|
183 | 'access callback' => '_node_resource_access', |
---|
184 | 'access arguments' => array('view'), |
---|
185 | 'access arguments append' => TRUE, |
---|
186 | 'callback' => '_node_resource_load_node_files', |
---|
187 | 'args' => array( |
---|
188 | array( |
---|
189 | 'name' => 'nid', |
---|
190 | 'optional' => FALSE, |
---|
191 | 'source' => array('path' => 0), |
---|
192 | 'type' => 'int', |
---|
193 | 'description' => 'The nid of the node whose files we are getting', |
---|
194 | ), |
---|
195 | array( |
---|
196 | 'name' => 'file_contents', |
---|
197 | 'type' => 'int', |
---|
198 | 'description' => t('To return file contents or not.'), |
---|
199 | 'source' => array('path' => 2), |
---|
200 | 'optional' => TRUE, |
---|
201 | 'default value' => TRUE, |
---|
202 | ), |
---|
203 | ), |
---|
204 | ), |
---|
205 | ), |
---|
206 | ), |
---|
207 | ); |
---|
208 | if (module_exists('comment')) { |
---|
209 | $comments = array( |
---|
210 | 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), |
---|
211 | 'help' => t('This method returns the number of new comments on a given node.'), |
---|
212 | 'access callback' => 'user_access', |
---|
213 | 'access arguments' => array('access comments'), |
---|
214 | 'access arguments append' => FALSE, |
---|
215 | 'callback' => '_node_resource_load_node_comments', |
---|
216 | 'args' => array( |
---|
217 | array( |
---|
218 | 'name' => 'nid', |
---|
219 | 'type' => 'int', |
---|
220 | 'description' => t('The node id to load comments for.'), |
---|
221 | 'source' => array('path' => 0), |
---|
222 | 'optional' => FALSE, |
---|
223 | ), |
---|
224 | array( |
---|
225 | 'name' => 'count', |
---|
226 | 'type' => 'int', |
---|
227 | 'description' => t('Number of comments to load.'), |
---|
228 | 'source' => array('param' => 'count'), |
---|
229 | 'optional' => TRUE, |
---|
230 | ), |
---|
231 | array( |
---|
232 | 'name' => 'offset', |
---|
233 | 'type' => 'int', |
---|
234 | '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.'), |
---|
235 | 'source' => array('param' => 'offset'), |
---|
236 | 'optional' => TRUE, |
---|
237 | ), |
---|
238 | ), |
---|
239 | ); |
---|
240 | $node_resource['node']['relationships']['comments'] = $comments; |
---|
241 | } |
---|
242 | return $node_resource; |
---|
243 | } |
---|