PageRenderTime 99ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/features/features.api.php

https://github.com/woeldiche/dev.module
PHP | 343 lines | 75 code | 19 blank | 249 comment | 3 complexity | 48e9cbf40c03baf8607f6c6356deedbe MD5 | raw file
  1. <?php
  2. /**
  3. * Main info hook that features uses to determine what components are provided
  4. * by the implementing module.
  5. *
  6. * @return array
  7. * An array of components, keyed by the component name. Each component can
  8. * define several keys:
  9. *
  10. * 'file': Optional path to a file to include which contains the rest
  11. * of the features API hooks for this module.
  12. *
  13. * 'default_hook': The defaults hook for your component that is called
  14. * when the cache of default components is generated. Examples include
  15. * hook_views_default_views() or hook_context_default_contexts().
  16. *
  17. * 'default_file': The file-writing behavior to use when exporting this
  18. * component. May be one of 3 constant values:
  19. *
  20. * FEATURES_DEFAULTS_INCLUDED_COMMON: write hooks/components to
  21. * `.features.inc` with other components. This is the default behavior
  22. * if this key is not defined.
  23. *
  24. * FEATURES_DEFAULTS_INCLUDED: write hooks/components to a component-
  25. * specific include named automatically by features.
  26. *
  27. * FEATURES_DEFAULTS_CUSTOM: write hooks/components to a component-
  28. * specific include with a custom name provided. If your module provides
  29. * large amounts of code that should not be parsed often (only on specific
  30. * cache clears/rebuilds, for example) you should use the 2nd or 3rd
  31. * options to split your component into its own include.
  32. *
  33. * 'default_filename': The filename to use when 'default_file' is set to
  34. * FEATURES_DEFAULTS_CUSTOM.
  35. *
  36. * 'features_source': Boolean value for whether this component should be
  37. * offered as an option on the initial feature creation form.
  38. */
  39. function hook_features_api() {
  40. return array(
  41. 'mycomponent' => array(
  42. 'default_hook' => 'mycomponent_defaults',
  43. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  44. 'features_source' => TRUE,
  45. 'file' => drupal_get_path('module', 'mycomponent') .'/mycomponent.features.inc',
  46. ),
  47. );
  48. }
  49. /**
  50. * Component hook. The hook should be implemented using the name ot the
  51. * component, not the module, eg. [component]_features_export() rather than
  52. * [module]_features_export().
  53. *
  54. * Process the export array for a given component. Implementations of this hook
  55. * have three key tasks:
  56. *
  57. * 1. Determine module dependencies for any of the components passed to it
  58. * e.g. the views implementation iterates over each views' handlers and
  59. * plugins to determine which modules need to be added as dependencies.
  60. *
  61. * 2. Correctly add components to the export array. In general this is usually
  62. * adding all of the items in $data to $export['features']['my_key'], but
  63. * can become more complicated if components are shared between features
  64. * or modules.
  65. *
  66. * 3. Delegating further detection and export tasks to related or derivative
  67. * components.
  68. *
  69. * Each export processor can kickoff further export processors by returning a
  70. * keyed array (aka the "pipe") where the key is the next export processor hook
  71. * to call and the value is an array to be passed to that processor's $data
  72. * argument. This allows an export process to start simply at a few objects:
  73. *
  74. * [context]
  75. *
  76. * And then branch out, delegating each component to its appropriate hook:
  77. *
  78. * [context]--------+------------+
  79. * | | |
  80. * [node] [block] [views]
  81. * |
  82. * [CCK]
  83. * |
  84. * [imagecache]
  85. *
  86. * @param array $data
  87. * An array of machine names for the component in question to be exported.
  88. * @param array &$export
  89. * By reference. An array of all components to be exported with a given
  90. * feature. Component objects that should be exported should be added to
  91. * this array.
  92. * @param string $module_name
  93. * The name of the feature module to be generated.
  94. * @return array
  95. * The pipe array of further processors that should be called.
  96. */
  97. function hook_features_export($data, &$export, $module_name) {
  98. // The following is the simplest implementation of a straight object export
  99. // with no further export processors called.
  100. foreach ($data as $component) {
  101. $export['mycomponent'][$component] = $component;
  102. }
  103. return array();
  104. }
  105. /**
  106. * Component hook. The hook should be implemented using the name ot the
  107. * component, not the module, eg. [component]_features_export() rather than
  108. * [module]_features_export().
  109. *
  110. * List all objects for a component that may be exported.
  111. *
  112. * @return array
  113. * A keyed array of items, suitable for use with a FormAPI select or
  114. * checkboxes element.
  115. */
  116. function hook_features_export_options() {
  117. $options = array();
  118. foreach (mycomponent_load() as $mycomponent) {
  119. $options[$mycomponent->name] = $mycomponent->title;
  120. }
  121. return $options;
  122. }
  123. /**
  124. * Component hook. The hook should be implemented using the name ot the
  125. * component, not the module, eg. [component]_features_export() rather than
  126. * [module]_features_export().
  127. *
  128. * Render one or more component objects to code.
  129. *
  130. * @param string $module_name
  131. * The name of the feature module to be exported.
  132. * @param array $data
  133. * An array of machine name identifiers for the objects to be rendered.
  134. * @param array $export
  135. * The full export array of the current feature being exported. This is only
  136. * passed when hook_features_export_render() is invoked for an actual feature
  137. * update or recreate, not during state checks or other operations.
  138. * @return array
  139. * An associative array of rendered PHP code where the key is the name of the
  140. * hook that should wrap the PHP code. The hook should not include the name
  141. * of the module, e.g. the key for `hook_example` should simply be `example`.
  142. */
  143. function hook_features_export_render($module_name, $data, $export = NULL) {
  144. $code = array();
  145. $code[] = '$mycomponents = array();';
  146. foreach ($data as $name) {
  147. $code[] = " \$mycomponents['{$name}'] = " . features_var_export(mycomponent_load($name)) .";";
  148. }
  149. $code[] = "return \$mycomponents;";
  150. $code = implode("\n", $mycomponents);
  151. return array('mycomponent_defaults' => $code);
  152. }
  153. /**
  154. * Component hook. The hook should be implemented using the name ot the
  155. * component, not the module, eg. [component]_features_export() rather than
  156. * [module]_features_export().
  157. *
  158. * Revert all component objects for a given feature module.
  159. *
  160. * @param string $module_name
  161. * The name of the feature module whose components should be reverted.
  162. * @return boolean
  163. * TRUE or FALSE for whether the components were successfully reverted.
  164. */
  165. function hook_features_export_revert($module_name) {
  166. $mycomponents = module_invoke_all($module_name, 'mycomponent_defaults');
  167. if (!empty($$mycomponents)) {
  168. foreach ($mycomponents as $mycomponent) {
  169. mycomponent_delete($mycomponent);
  170. }
  171. }
  172. }
  173. /**
  174. * Component hook. The hook should be implemented using the name ot the
  175. * component, not the module, eg. [component]_features_export() rather than
  176. * [module]_features_export().
  177. *
  178. * Rebuild all component objects for a given feature module. Should only be
  179. * implemented for 'faux-exportable' components.
  180. *
  181. * This hook is called at points where Features determines that it is safe
  182. * (ie. the feature is in state `FEATURES_REBUILDABLE`) for your module to
  183. * replace objects in the database with defaults that you collect from your
  184. * own defaults hook. See API.txt for how Features determines whether a
  185. * rebuild of components is possible.
  186. *
  187. * @param string $module_name
  188. * The name of the feature module whose components should be rebuilt.
  189. */
  190. function hook_features_export_rebuild($module_name) {
  191. $mycomponents = module_invoke_all($module_name, 'mycomponent_defaults');
  192. if (!empty($$mycomponents)) {
  193. foreach ($mycomponents as $mycomponent) {
  194. mycomponent_save($mycomponent);
  195. }
  196. }
  197. }
  198. /**
  199. * Alter the final export array just prior to the rendering of defaults. Allows
  200. * modules a final say in altering what component objects are exported.
  201. *
  202. * @param array &$export
  203. * By reference. An array of all components to be exported with a given
  204. * feature.
  205. * @param array $module_name
  206. * The name of the feature module to be generated.
  207. */
  208. function hook_features_export_alter(&$export, $module_name) {
  209. // Example: do not allow the page content type to be exported, ever.
  210. if (!empty($export['node']['page'])) {
  211. unset($export['node']['page']);
  212. }
  213. }
  214. /**
  215. * Alter the pipe array for a given component. This hook should be implemented
  216. * with the name of the component type in place of `component` in the function
  217. * name, e.g. `features_pipe_views_alter()` will alter the pipe for the Views
  218. * component.
  219. *
  220. * @param array &$pipe
  221. * By reference. The pipe array of further processors that should be called.
  222. * @param array $data
  223. * An array of machine names for the component in question to be exported.
  224. * @param array &$export
  225. * By reference. An array of all components to be exported with a given
  226. * feature.
  227. * @param string $module_name
  228. * The name of the feature module to be generated.
  229. */
  230. function hook_features_pipe_component_alter(&$pipe, $data, $export, $module_name) {
  231. }
  232. /**
  233. * @defgroup features_component_alter_hooks Feature's component alter hooks
  234. * @{
  235. * Hooks to modify components defined by other features. These come in the form
  236. * hook_COMPONENT_alter where COMPONENT is the default_hook declared by any of
  237. * components within features.
  238. *
  239. * CTools also has a variety of hook_FOO_alters.
  240. *
  241. * Note: While views is a component of features, it declares it's own alter
  242. * function which takes a similar form:
  243. * hook_views_default_views_alter(&$views)
  244. */
  245. /**
  246. * Alter the default cck fields right before they are cached into the database.
  247. *
  248. * @param &$fields
  249. * By reference. The fields that have been declared by another feature.
  250. */
  251. function hook_content_default_fields_alter(&$fields) {
  252. }
  253. /**
  254. * Alter the default fieldgroup groups right before they are cached into the
  255. * database.
  256. *
  257. * @param &$groups
  258. * By reference. The fieldgroup groups that have been declared by another
  259. * feature.
  260. */
  261. function hook_fieldgroup_default_groups_alter(&$groups) {
  262. }
  263. /**
  264. * Alter the default filter formats right before they are cached into the
  265. * database.
  266. *
  267. * @param &$formats
  268. * By reference. The formats that have been declared by another feature.
  269. */
  270. function hook_filter_default_formats_alter(&$formats) {
  271. }
  272. /**
  273. * Alter the default menus right before they are cached into the database.
  274. *
  275. * @param &$menus
  276. * By reference. The menus that have been declared by another feature.
  277. */
  278. function hook_menu_default_menu_custom_alter(&$menus) {
  279. }
  280. /**
  281. * Alter the default menu links right before they are cached into the database.
  282. *
  283. * @param &$links
  284. * By reference. The menu links that have been declared by another feature.
  285. */
  286. function hook_menu_default_menu_links_alter(&$links) {
  287. }
  288. /**
  289. * Alter the default menu items right before they are cached into the database.
  290. *
  291. * @param &$items
  292. * By reference. The menu items that have been declared by another feature.
  293. */
  294. function hook_menu_default_items_alter(&$items) {
  295. }
  296. /**
  297. * Alter the default vocabularies right before they are cached into the
  298. * database.
  299. *
  300. * @param &$vocabularies
  301. * By reference. The vocabularies that have been declared by another feature.
  302. */
  303. function hook_taxonomy_default_vocabularies_alter(&$vocabularies) {
  304. }
  305. /**
  306. * Alter the default permissions right before they are cached into the
  307. * database.
  308. *
  309. * @param &$permissions
  310. * By reference. The permissions that have been declared by another feature.
  311. */
  312. function hook_user_default_permissions_alter(&$permissions) {
  313. }
  314. /**
  315. * Alter the default roles right before they are cached into the database.
  316. *
  317. * @param &$roles
  318. * By reference. The roles that have been declared by another feature.
  319. */
  320. function hook_user_default_roles_alter(&$roles) {
  321. }
  322. /**
  323. * @}
  324. */