comparison sites/all/modules/features/features.api.php @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
comparison
equal deleted inserted replaced
3:b28be78d8160 4:ce11bbd8f642
1 <?php
2
3 /**
4 * Main info hook that features uses to determine what components are provided
5 * by the implementing module.
6 *
7 * @return array
8 * An array of components, keyed by the component name. Each component can
9 * define several keys:
10 *
11 * 'file': Optional path to a file to include which contains the rest
12 * of the features API hooks for this module.
13 *
14 * 'default_hook': The defaults hook for your component that is called
15 * when the cache of default components is generated. Examples include
16 * hook_views_default_views() or hook_context_default_contexts().
17 *
18 * 'default_file': The file-writing behavior to use when exporting this
19 * component. May be one of 3 constant values:
20 *
21 * FEATURES_DEFAULTS_INCLUDED_COMMON: write hooks/components to
22 * `.features.inc` with other components. This is the default behavior
23 * if this key is not defined.
24 *
25 * FEATURES_DEFAULTS_INCLUDED: write hooks/components to a component-
26 * specific include named automatically by features.
27 *
28 * FEATURES_DEFAULTS_CUSTOM: write hooks/components to a component-
29 * specific include with a custom name provided. If your module provides
30 * large amounts of code that should not be parsed often (only on specific
31 * cache clears/rebuilds, for example) you should use the 2nd or 3rd
32 * options to split your component into its own include.
33 *
34 * 'default_filename': The filename to use when 'default_file' is set to
35 * FEATURES_DEFAULTS_CUSTOM.
36 *
37 * 'feature_source': Boolean value for whether this component should be
38 * offered as an option on the initial feature creation form.
39 *
40 * 'base': Optional. An alternative base key to use when calling features
41 * hooks for this component. Can be used for features component types that
42 * are declared "dynamically" or are part of a family of components.
43 *
44 * 'alter_type': What type of alter hook this hook uses. 'normal' is called
45 * after the main hook is called. 'inline' is embeded within the default hook
46 * and may not be implemented by some default hooks.
47 * 'none' is no alter hook exists. Defaults to 'normal'
48 *
49 * 'alter_hook': What the name of the alter hook for this component is.
50 * Do not include the '_alter' part. Defaults to 'default_hook'.
51 */
52 function hook_features_api() {
53 return array(
54 'mycomponent' => array(
55 'default_hook' => 'mycomponent_defaults',
56 'default_file' => FEATURES_DEFAULTS_INCLUDED,
57 'feature_source' => TRUE,
58 'file' => drupal_get_path('module', 'mycomponent') . '/mycomponent.features.inc',
59 ),
60 );
61 }
62
63 /**
64 * Component hook. The hook should be implemented using the name of the
65 * component, not the module, eg. [component]_features_export() rather than
66 * [module]_features_export().
67 *
68 * Process the export array for a given component. Implementations of this hook
69 * have three key tasks:
70 *
71 * 1. Determine module dependencies for any of the components passed to it
72 * e.g. the views implementation iterates over each views' handlers and
73 * plugins to determine which modules need to be added as dependencies.
74 *
75 * 2. Correctly add components to the export array. In general this is usually
76 * adding all of the items in $data to $export['features']['my_key'], but
77 * can become more complicated if components are shared between features
78 * or modules.
79 *
80 * 3. Delegating further detection and export tasks to related or derivative
81 * components.
82 *
83 * Each export processor can kickoff further export processors by returning a
84 * keyed array (aka the "pipe") where the key is the next export processor hook
85 * to call and the value is an array to be passed to that processor's $data
86 * argument. This allows an export process to start simply at a few objects:
87 *
88 * [context]
89 *
90 * And then branch out, delegating each component to its appropriate hook:
91 *
92 * [context]--------+------------+
93 * | | |
94 * [node] [block] [views]
95 * |
96 * [CCK]
97 * |
98 * [imagecache]
99 *
100 * @param array $data
101 * An array of machine names for the component in question to be exported.
102 * @param array &$export
103 * By reference. An array of all components to be exported with a given
104 * feature. Component objects that should be exported should be added to
105 * this array.
106 * @param string $module_name
107 * The name of the feature module to be generated.
108 * @return array
109 * The pipe array of further processors that should be called.
110 */
111 function hook_features_export($data, &$export, $module_name) {
112 // The following is the simplest implementation of a straight object export
113 // with no further export processors called.
114 foreach ($data as $component) {
115 $export['features']['mycomponent'][$component] = $component;
116 }
117 return array();
118 }
119
120 /**
121 * Component hook. The hook should be implemented using the name of the
122 * component, not the module, eg. [component]_features_export() rather than
123 * [module]_features_export().
124 *
125 * List all objects for a component that may be exported.
126 *
127 * @return array
128 * A keyed array of items, suitable for use with a FormAPI select or
129 * checkboxes element.
130 */
131 function hook_features_export_options() {
132 $options = array();
133 foreach (mycomponent_load() as $mycomponent) {
134 $options[$mycomponent->name] = $mycomponent->title;
135 }
136 return $options;
137 }
138
139 /**
140 * Component hook. The hook should be implemented using the name of the
141 * component, not the module, eg. [component]_features_export() rather than
142 * [module]_features_export().
143 *
144 * Render one or more component objects to code.
145 *
146 * @param string $module_name
147 * The name of the feature module to be exported.
148 * @param array $data
149 * An array of machine name identifiers for the objects to be rendered.
150 * @param array $export
151 * The full export array of the current feature being exported. This is only
152 * passed when hook_features_export_render() is invoked for an actual feature
153 * update or recreate, not during state checks or other operations.
154 * @return array
155 * An associative array of rendered PHP code where the key is the name of the
156 * hook that should wrap the PHP code. The hook should not include the name
157 * of the module, e.g. the key for `hook_example` should simply be `example`
158 * The values in the array can also be in the form of an associative array
159 * with the required key of 'code' and optional key of 'args', if 'args' need
160 * to be added to the hook.
161 */
162 function hook_features_export_render($module_name, $data, $export = NULL) {
163 $code = array();
164 $code[] = '$mycomponents = array();';
165 foreach ($data as $name) {
166 $code[] = " \$mycomponents['{$name}'] = " . features_var_export(mycomponent_load($name)) .";";
167 }
168 $code[] = "return \$mycomponents;";
169 $code = implode("\n", $code);
170 return array('mycomponent_defaults' => $code);
171 }
172
173 /**
174 * Component hook. The hook should be implemented using the name of the
175 * component, not the module, eg. [component]_features_export() rather than
176 * [module]_features_export().
177 *
178 * Revert all component objects for a given feature module.
179 *
180 * @param string $module_name
181 * The name of the feature module whose components should be reverted.
182 * @return boolean
183 * TRUE or FALSE for whether the components were successfully reverted.
184 * NOTE: This return value is no longer used in the latest Features so
185 * modules should no longer count on this value
186 */
187 function hook_features_revert($module_name) {
188 $mycomponents = module_invoke($module_name, 'mycomponent_defaults');
189 if (!empty($mycomponents)) {
190 foreach ($mycomponents as $mycomponent) {
191 mycomponent_delete($mycomponent);
192 }
193 }
194 }
195
196 /**
197 * Component hook. The hook should be implemented using the name of the
198 * component, not the module, eg. [component]_features_export() rather than
199 * [module]_features_export().
200 *
201 * Rebuild all component objects for a given feature module. Should only be
202 * implemented for 'faux-exportable' components.
203 *
204 * This hook is called at points where Features determines that it is safe
205 * (ie. the feature is in state `FEATURES_REBUILDABLE`) for your module to
206 * replace objects in the database with defaults that you collect from your
207 * own defaults hook. See API.txt for how Features determines whether a
208 * rebuild of components is possible.
209 *
210 * @param string $module_name
211 * The name of the feature module whose components should be rebuilt.
212 */
213 function hook_features_rebuild($module_name) {
214 $mycomponents = module_invoke($module_name, 'mycomponent_defaults');
215 if (!empty($mycomponents)) {
216 foreach ($mycomponents as $mycomponent) {
217 mycomponent_save($mycomponent);
218 }
219 }
220 }
221
222 /**
223 * Alter the final array of Component names to be exported, just prior to
224 * the rendering of defaults. Allows modules a final say in whether or not
225 * certain Components are exported (the Components' actual data, however,
226 * cannot be altered by this hook).
227 *
228 * @param array &$export
229 * By reference. An array of all component names to be exported with a given
230 * feature.
231 * @param array $module_name
232 * The name of the feature module to be generated.
233 */
234 function hook_features_export_alter(&$export, $module_name) {
235 // Example: do not allow the page content type to be exported, ever.
236 if (!empty($export['features']['node']['page'])) {
237 unset($export['features']['node']['page']);
238 }
239 }
240
241 /**
242 * Alter the pipe array for a given component. This hook should be implemented
243 * with the name of the component type in place of `component` in the function
244 * name, e.g. `features_pipe_views_alter()` will alter the pipe for the Views
245 * component.
246 *
247 * @param array &$pipe
248 * By reference. The pipe array of further processors that should be called.
249 * @param array $data
250 * An array of machine names for the component in question to be exported.
251 * @param array &$export
252 * By reference. An array of all components to be exported with a given
253 * feature.
254 */
255 function hook_features_pipe_COMPONENT_alter(&$pipe, $data, $export) {
256 if (in_array($data, 'my-node-type')) {
257 $pipe['dependencies'][] = 'mymodule';
258 }
259 }
260
261 /**
262 * Alter the pipe array for a given component.
263 *
264 * @param array &$pipe
265 * By reference. The pipe array of further processors that should be called.
266 * @param array $data
267 * An array of machine names for the component in question to be exported.
268 * @param array &$export
269 * By reference. An array of all components to be exported with a given
270 * feature.
271 *
272 * The component being exported is contained in $export['component'].
273 * The module being exported contained in $export['module_name'].
274 */
275 function hook_features_pipe_alter(&$pipe, $data, $export) {
276 if ($export['component'] == 'node' && in_array($data, 'my-node-type')) {
277 $pipe['dependencies'][] = 'mymodule';
278 }
279 }
280
281 /**
282 * @defgroup features_component_alter_hooks Feature's component alter hooks
283 * @{
284 * Hooks to modify components defined by other features. These come in the form
285 * hook_COMPONENT_alter where COMPONENT is the default_hook declared by any of
286 * components within features.
287 *
288 * CTools also has a variety of hook_FOO_alters.
289 *
290 * Note: While views is a component of features, it declares it's own alter
291 * function which takes a similar form:
292 * hook_views_default_views_alter(&$views)
293 */
294
295 /**
296 * Alter the default fields right before they are cached into the database.
297 *
298 * @param &$fields
299 * By reference. The fields that have been declared by another feature.
300 */
301 function hook_field_default_fields_alter(&$fields) {
302 }
303
304 /**
305 * Alter the default fieldgroup groups right before they are cached into the
306 * database.
307 *
308 * @param &$groups
309 * By reference. The fieldgroup groups that have been declared by another
310 * feature.
311 */
312 function hook_fieldgroup_default_groups_alter(&$groups) {
313 }
314
315 /**
316 * Alter the default filter formats right before they are cached into the
317 * database.
318 *
319 * @param &$formats
320 * By reference. The formats that have been declared by another feature.
321 */
322 function hook_filter_default_formats_alter(&$formats) {
323 }
324
325 /**
326 * Alter the default menus right before they are cached into the database.
327 *
328 * @param &$menus
329 * By reference. The menus that have been declared by another feature.
330 */
331 function hook_menu_default_menu_custom_alter(&$menus) {
332 }
333
334 /**
335 * Alter the default menu links right before they are cached into the database.
336 *
337 * @param &$links
338 * By reference. The menu links that have been declared by another feature.
339 */
340 function hook_menu_default_menu_links_alter(&$links) {
341 }
342
343 /**
344 * Alter the default menu items right before they are cached into the database.
345 *
346 * @param &$items
347 * By reference. The menu items that have been declared by another feature.
348 */
349 function hook_menu_default_items_alter(&$items) {
350 }
351
352 /**
353 * Alter the default vocabularies right before they are cached into the
354 * database.
355 *
356 * @param &$vocabularies
357 * By reference. The vocabularies that have been declared by another feature.
358 */
359 function hook_taxonomy_default_vocabularies_alter(&$vocabularies) {
360 }
361
362 /**
363 * Alter the default permissions right before they are cached into the
364 * database.
365 *
366 * @param &$permissions
367 * By reference. The permissions that have been declared by another feature.
368 */
369 function hook_user_default_permissions_alter(&$permissions) {
370 }
371
372 /**
373 * Alter the default roles right before they are cached into the database.
374 *
375 * @param &$roles
376 * By reference. The roles that have been declared by another feature.
377 */
378 function hook_user_default_roles_alter(&$roles) {
379 }
380
381 /**
382 * @}
383 */
384
385
386 /**
387 * @defgroup features_module_hooks Feature module hooks
388 * @{
389 * Hooks invoked on Feature modules when that module is enabled, disabled,
390 * rebuilt, or reverted. These are ONLY invoked on the Features module on
391 * which these actions are taken.
392 */
393
394 /**
395 * Feature module hook. Invoked on a Feature module before that module is
396 * reverted.
397 *
398 * @param $component
399 * String name of the component that is about to be reverted.
400 */
401 function hook_pre_features_revert($component) {
402 }
403
404 /**
405 * Feature module hook. Invoked on a Feature module after that module is
406 * reverted.
407 *
408 * @param $component
409 * String name of the component that has just been reverted.
410 */
411 function hook_post_features_revert($component) {
412 }
413
414 /**
415 * Feature module hook. Invoked on a Feature module before that module is
416 * rebuilt.
417 *
418 * @param $component
419 * String name of the component that is about to be rebuilt.
420 */
421 function hook_pre_features_rebuild($component) {
422 }
423
424 /**
425 * Feature module hook. Invoked on a Feature module after that module is
426 * rebuilt.
427 *
428 * @param $component
429 * String name of the component that has just been rebuilt.
430 */
431 function hook_post_features_rebuild($component) {
432 }
433
434 /**
435 * Feature module hook. Invoked on a Feature module before that module is
436 * disabled.
437 *
438 * @param $component
439 * String name of the component that is about to be disabled.
440 */
441 function hook_pre_features_disable_feature($component) {
442 }
443
444 /**
445 * Feature module hook. Invoked on a Feature module after that module is
446 * disabled.
447 *
448 * @param $component
449 * String name of the component that has just been disabled.
450 */
451 function hook_post_features_disable_feature($component) {
452 }
453
454 /**
455 * Feature module hook. Invoked on a Feature module before that module is
456 * enabled.
457 *
458 * @param $component
459 * String name of the component that is about to be enabled.
460 */
461 function hook_pre_features_enable_feature($component) {
462 }
463
464 /**
465 * Feature module hook. Invoked on a Feature module after that module is
466 * enabled.
467 *
468 * @param $component
469 * String name of the component that has just been enabled.
470 */
471 function hook_post_features_enable_feature($component) {
472 }
473
474 /**
475 * @}
476 */