ctools.api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Chaos Tool Suite.
  5. *
  6. * This file is divided into static hooks (hooks with string literal names) and
  7. * dynamic hooks (hooks with pattern-derived string names).
  8. */
  9. /**
  10. * @addtogroup hooks
  11. * @{
  12. */
  13. /**
  14. * Inform CTools about plugin types.
  15. *
  16. * @return array
  17. * An array of plugin types, keyed by the type name.
  18. * See the advanced help topic 'plugins-creating' for details of the array
  19. * properties.
  20. */
  21. function hook_ctools_plugin_type() {
  22. $plugins['my_type'] = array(
  23. 'load themes' => TRUE,
  24. );
  25. return $plugins;
  26. }
  27. /**
  28. * Tells CTools where to find module-defined plugins.
  29. *
  30. * This hook is used to inform the CTools plugin system about the location of a
  31. * directory that should be searched for files containing plugins of a
  32. * particular type. CTools invokes this same hook for all plugins, using the
  33. * two passed parameters to indicate the specific type of plugin for which it
  34. * is searching.
  35. *
  36. * The $plugin_type parameter is self-explanatory - it is the string name of the
  37. * plugin type (e.g., Panels' 'layouts' or 'styles'). The $owner parameter is
  38. * necessary because CTools internally namespaces plugins by the module that
  39. * owns them. This is an extension of Drupal best practices on avoiding global
  40. * namespace pollution by prepending your module name to all its functions.
  41. * Consequently, it is possible for two different modules to create a plugin
  42. * type with exactly the same name and have them operate in harmony. In fact,
  43. * this system renders it impossible for modules to encroach on other modules'
  44. * plugin namespaces.
  45. *
  46. * Given this namespacing, it is important that implementations of this hook
  47. * check BOTH the $owner and $plugin_type parameters before returning a path.
  48. * If your module does not implement plugins for the requested module/plugin
  49. * combination, it is safe to return nothing at all (or NULL). As a convenience,
  50. * it is also safe to return a path that does not exist for plugins your module
  51. * does not implement - see form 2 for a use case.
  52. *
  53. * Note that modules implementing a plugin also must implement this hook to
  54. * instruct CTools as to the location of the plugins. See form 3 for a use case.
  55. *
  56. * The conventional structure to return is "plugins/$plugin_type" - that is, a
  57. * 'plugins' subdirectory in your main module directory, with individual
  58. * directories contained therein named for the plugin type they contain.
  59. *
  60. * @param string $owner
  61. * The system name of the module owning the plugin type for which a base
  62. * directory location is being requested.
  63. * @param string $plugin_type
  64. * The name of the plugin type for which a base directory is being requested.
  65. *
  66. * @return string
  67. * The path where CTools' plugin system should search for plugin files,
  68. * relative to your module's root. Omit leading and trailing slashes.
  69. */
  70. function hook_ctools_plugin_directory($owner, $plugin_type) {
  71. // Form 1 - for a module implementing only the 'content_types' plugin owned
  72. // by CTools, this would cause the plugin system to search the
  73. // <moduleroot>/plugins/content_types directory for .inc plugin files.
  74. if ($owner == 'ctools' && $plugin_type == 'content_types') {
  75. return 'plugins/content_types';
  76. }
  77. // Form 2 - if your module implements only Panels plugins, and has 'layouts'
  78. // and 'styles' plugins but no 'cache' or 'display_renderers', it is OK to be
  79. // lazy and return a directory for a plugin you don't actually implement (so
  80. // long as that directory doesn't exist). This lets you avoid ugly in_array()
  81. // logic in your conditional, and also makes it easy to add plugins of those
  82. // types later without having to change this hook implementation.
  83. if ($owner == 'panels') {
  84. return "plugins/$plugin_type";
  85. }
  86. // Form 3 - CTools makes no assumptions about where your plugins are located,
  87. // so you still have to implement this hook even for plugins created by your
  88. // own module.
  89. if ($owner == 'mymodule') {
  90. // Yes, this is exactly like Form 2 - just a different reasoning for it.
  91. return "plugins/$plugin_type";
  92. }
  93. // Finally, if nothing matches, it's safe to return nothing at all (== NULL).
  94. }
  95. /**
  96. * Alter a plugin before it has been processed.
  97. *
  98. * This hook is useful for altering flags or other information that will be
  99. * used or possibly overriden by the process hook if defined.
  100. *
  101. * @param array $plugin
  102. * An associative array defining a plugin.
  103. * @param array $info
  104. * An associative array of plugin type info.
  105. */
  106. function hook_ctools_plugin_pre_alter(array &$plugin, array &$info) {
  107. // Override a function defined by the plugin.
  108. if ($info['type'] == 'my_type') {
  109. $plugin['my_flag'] = 'new_value';
  110. }
  111. }
  112. /**
  113. * Alter a plugin after it has been processed.
  114. *
  115. * This hook is useful for overriding the final values for a plugin after it
  116. * has been processed.
  117. *
  118. * @param array $plugin
  119. * An associative array defining a plugin.
  120. * @param array $info
  121. * An associative array of plugin type info.
  122. */
  123. function hook_ctools_plugin_post_alter(array &$plugin, array &$info) {
  124. // Override a function defined by the plugin.
  125. if ($info['type'] == 'my_type') {
  126. $plugin['my_function'] = 'new_function';
  127. }
  128. }
  129. /**
  130. * Alter the list of modules/themes which implement a certain api.
  131. *
  132. * The hook named here is just an example, as the real existing hooks are named
  133. * for example 'hook_views_api_alter'.
  134. *
  135. * @param array $list
  136. * An array of informations about the implementors of a certain api.
  137. * The key of this array are the module names/theme names.
  138. */
  139. function hook_ctools_api_hook_alter(array &$list) {
  140. // Alter the path of the node implementation.
  141. $list['node']['path'] = drupal_get_path('module', 'node');
  142. }
  143. /**
  144. * Alter the available functions to be used in ctools math expression api.
  145. *
  146. * One use case would be to create your own function in your module and
  147. * allow to use it in the math expression api.
  148. *
  149. * @param array $functions
  150. * An array which has the functions as value.
  151. * @param array $context
  152. * An array containing an item 'final' whose value is a reference to the
  153. * definitions for multiple-arg functions. Use this to add in functions that
  154. * require more than one arg.
  155. */
  156. function hook_ctools_math_expression_functions_alter(array &$functions, array $context) {
  157. // Allow to convert from degrees to radians.
  158. $functions[] = 'deg2rad';
  159. $multiarg = $context['final'];
  160. $multiarg['pow'] = array(
  161. 'function' => 'pow',
  162. 'arguments' => 2,
  163. );
  164. }
  165. /**
  166. * Alter the available functions to be used in ctools math expression api.
  167. *
  168. * One usecase would be to create your own function in your module and
  169. * allow to use it in the math expression api.
  170. *
  171. * @param array $constants
  172. * An array of name:value pairs, one for each named constant. Values added
  173. * to this array become read-only variables with the value assigned here.
  174. */
  175. function hook_ctools_math_expression_constants_alter(array &$constants) {
  176. // Add the speed of light as constant 'c':
  177. $constants['c'] = 299792458;
  178. }
  179. /**
  180. * Alter everything.
  181. *
  182. * @param array $info
  183. * An associative array containing the following keys:
  184. * - content: The rendered content.
  185. * - title: The content's title.
  186. * - no_blocks: A boolean to decide if blocks should be displayed.
  187. * @param bool $page
  188. * If TRUE then this renderer owns the page and can use theme('page')
  189. * for no blocks; if false, output is returned regardless of any no
  190. * blocks settings.
  191. * @param array $context
  192. * An associative array containing the following keys:
  193. * - args: The raw arguments behind the contexts.
  194. * - contexts: The context objects in use.
  195. * - task: The task object in use.
  196. * - subtask: The subtask object in use.
  197. * - handler: The handler object in use.
  198. */
  199. function hook_ctools_render_alter(array &$info, &$page, array &$context) {
  200. if ($context['handler']->name == 'my_handler') {
  201. ctools_add_css('my_module.theme', 'my_module');
  202. }
  203. }
  204. /**
  205. * Alter a content plugin subtype.
  206. *
  207. * While content types can be altered via hook_ctools_plugin_pre_alter() or
  208. * hook_ctools_plugin_post_alter(), the subtypes that content types rely on
  209. * are special and require their own hook.
  210. *
  211. * This hook can be used to add things like 'render last' or change icons
  212. * or categories or to rename content on specific sites.
  213. */
  214. function hook_ctools_content_subtype_alter($subtype, $plugin) {
  215. // Force a particular subtype of a particular plugin to render last.
  216. if ($plugin['module'] === 'some_plugin_module'
  217. && $plugin['name'] === 'some_plugin_name'
  218. && $subtype['subtype_id'] === 'my_subtype_id'
  219. ) {
  220. $subtype['render last'] = TRUE;
  221. }
  222. }
  223. /**
  224. * Alter the definition of an entity context plugin.
  225. *
  226. * @param array $plugin
  227. * An associative array defining a plugin.
  228. * @param array $entity
  229. * The entity info array of a specific entity type.
  230. * @param string $plugin_id
  231. * The plugin ID, in the format NAME:KEY.
  232. */
  233. function hook_ctools_entity_context_alter(array &$plugin, array &$entity, $plugin_id) {
  234. ctools_include('context');
  235. switch ($plugin_id) {
  236. case 'entity_id:taxonomy_term':
  237. $plugin['no ui'] = TRUE;
  238. case 'entity:user':
  239. $plugin = ctools_get_context('user');
  240. unset($plugin['no ui']);
  241. unset($plugin['no required context ui']);
  242. break;
  243. }
  244. }
  245. /**
  246. * Alter the conversion of context items by ctools context plugin convert()s.
  247. *
  248. * @param ctools_context $context
  249. * The current context plugin object. If this implemented a 'convert'
  250. * function, the value passed in has been processed by that function.
  251. * @param string $converter
  252. * A string associated with the plugin type, identifying the operation.
  253. * @param string $value
  254. * The value being converted; this is the only return from the function.
  255. * @param array $converter_options
  256. * Array of key-value pairs to pass to a converter function from higher
  257. * levels.
  258. *
  259. * @see ctools_context_convert_context()
  260. */
  261. function hook_ctools_context_converter_alter(ctools_context $context, $converter, &$value, array $converter_options) {
  262. if ($converter === 'mystring') {
  263. $value = 'fixed';
  264. }
  265. }
  266. /**
  267. * Alter the definition of entity context plugins.
  268. *
  269. * @param array $plugins
  270. * An associative array of plugin definitions, keyed by plugin ID.
  271. *
  272. * @see hook_ctools_entity_context_alter()
  273. */
  274. function hook_ctools_entity_contexts_alter(array &$plugins) {
  275. $plugins['entity_id:taxonomy_term']['no ui'] = TRUE;
  276. }
  277. /**
  278. * Change cleanstring settings.
  279. *
  280. * @param array $settings
  281. * An associative array of cleanstring settings.
  282. *
  283. * @see ctools_cleanstring()
  284. */
  285. function hook_ctools_cleanstring_alter(array &$settings) {
  286. // Convert all strings to lower case.
  287. $settings['lower case'] = TRUE;
  288. }
  289. /**
  290. * Change cleanstring settings for a specific clean ID.
  291. *
  292. * @param array $settings
  293. * An associative array of cleanstring settings.
  294. *
  295. * @see ctools_cleanstring()
  296. */
  297. function hook_ctools_cleanstring_CLEAN_ID_alter(array &$settings) {
  298. // Convert all strings to lower case.
  299. $settings['lower case'] = TRUE;
  300. }
  301. /**
  302. * Let other modules modify the context handler before it is rendered.
  303. *
  304. * @param object $handler
  305. * A handler for a current task and subtask.
  306. * @param array $contexts
  307. * An associative array of contexts.
  308. * @param array $args
  309. * An array for current args.
  310. *
  311. * @see ctools_context_handler_pre_render()
  312. */
  313. function ctools_context_handler_pre_render($handler, array $contexts, array $args) {
  314. $handler->conf['css_id'] = 'my-id';
  315. }
  316. /**
  317. * @} End of "addtogroup hooks".
  318. */