ctools.api.php 10 KB

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