rules.api.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. <?php
  2. /**
  3. * @file
  4. * This file contains no working PHP code; it exists to provide additional
  5. * documentation for doxygen as well as to document hooks in the standard
  6. * Drupal manner.
  7. */
  8. /**
  9. * @defgroup rules Rules module integrations.
  10. *
  11. * Module integrations with the rules module.
  12. *
  13. * The Rules developer documentation describes how modules can integrate with
  14. * rules: http://drupal.org/node/298486.
  15. */
  16. /**
  17. * @defgroup rules_hooks Rules' hooks
  18. * @{
  19. * Hooks that can be implemented by other modules in order to extend rules.
  20. */
  21. /**
  22. * Define rules compatible actions.
  23. *
  24. * This hook is required in order to add a new rules action. It should be
  25. * placed into the file MODULENAME.rules.inc, which gets automatically included
  26. * when the hook is invoked.
  27. *
  28. * However, as an alternative to implementing this hook, class based plugin
  29. * handlers may be provided by implementing RulesActionHandlerInterface. See
  30. * the interface for details.
  31. *
  32. * @return
  33. * An array of information about the module's provided rules actions.
  34. * The array contains a sub-array for each action, with the action name as
  35. * the key. Actions names may only contain lowercase alpha-numeric characters
  36. * and underscores and should be prefixed with the providing module name.
  37. * Possible attributes for each sub-array are:
  38. * - label: The label of the action. Start capitalized. Required.
  39. * - group: A group for this element, used for grouping the actions in the
  40. * interface. Should start with a capital letter and be translated.
  41. * Required.
  42. * - parameter: (optional) An array describing all parameter of the action
  43. * with the parameter's name as key. Each parameter has to be
  44. * described by a sub-array with possible attributes as described
  45. * afterwards, whereas the name of a parameter needs to be a lowercase,
  46. * valid PHP variable name.
  47. * - provides: (optional) An array describing the variables the action
  48. * provides to the evaluation state with the variable name as key. Each
  49. * variable has to be described by a sub-array with possible attributes as
  50. * described afterwards, whereas the name of a parameter needs to be a
  51. * lowercase, valid PHP variable name.
  52. * - 'named parameter': (optional) If set to TRUE, the arguments will be
  53. * passed as a single array with the parameter names as keys. This emulates
  54. * named parameters in PHP and is in particular useful if the number of
  55. * parameters can vary. Defaults to FALSE.
  56. * - base: (optional) The base for action implementation callbacks to use
  57. * instead of the action's name. Defaults to the action name.
  58. * - callbacks: (optional) An array which allows to set specific function
  59. * callbacks for the action. The default for each callback is the actions
  60. * base appended by '_' and the callback name.
  61. * - 'access callback': (optional) A callback which has to return whether the
  62. * currently logged in user is allowed to configure this action. See
  63. * rules_node_integration_access() for an example callback.
  64. * Each 'parameter' array may contain the following properties:
  65. * - label: The label of the parameter. Start capitalized. Required.
  66. * - type: The rules data type of the parameter, which is to be passed to the
  67. * action. All types declared in hook_rules_data_info() may be specified, as
  68. * well as an array of possible types. Also lists and lists of a given type
  69. * can be specified by using the notating list<integer> as introduced by
  70. * the entity metadata module, see hook_entity_property_info(). The special
  71. * keyword '*' can be used when all types should be allowed. Required.
  72. * - bundles: (optional) An array of bundle names. When the specified type is
  73. * set to a single entity type, this may be used to restrict the allowed
  74. * bundles.
  75. * - description: (optional) If necessary, a further description of the
  76. * parameter.
  77. * - options list: (optional) A callback that returns an array of possible
  78. * values for this parameter. The callback has to return an array as used
  79. * by hook_options_list(). For an example implementation see
  80. * rules_data_action_type_options().
  81. * - save: (optional) If this is set to TRUE, the parameter will be saved by
  82. * rules when the rules evaluation ends. This is only supported for savable
  83. * data types. If the action returns FALSE, saving is skipped.
  84. * - optional: (optional) May be set to TRUE, when the parameter isn't
  85. * required.
  86. * - 'default value': (optional) The value to pass to the action, in case the
  87. * parameter is optional and there is no specified value.
  88. * - 'allow null': (optional) Usually Rules will not pass any NULL values as
  89. * argument, but abort the evaluation if a NULL value is present. If set to
  90. * TRUE, Rules will not abort and pass the NULL value through. Defaults to
  91. * FALSE.
  92. * - restriction: (optional) Restrict how the argument for this parameter may
  93. * be provided. Supported values are 'selector' and 'input'.
  94. * - default mode: (optional) Customize the default mode for providing the
  95. * argument value for a parameter. Supported values are 'selector' and
  96. * 'input'. The default depends on the required data type.
  97. * - sanitize: (optional) Allows parameters of type 'text' to demand an
  98. * already sanitized argument. If enabled, any user specified value won't be
  99. * sanitized itself, but replacements applied by input evaluators are as
  100. * well as values retrieved from selected data sources.
  101. * - translatable: (optional) If set to TRUE, the provided argument value
  102. * of the parameter is translatable via i18n String translation. This is
  103. * applicable for textual parameters only, i.e. parameters of type 'text',
  104. * 'token', 'list<text>' and 'list<token>'. Defaults to FALSE.
  105. * - ui class: (optional) Allows overriding the UI class, which is used to
  106. * generate the configuration UI of a parameter. Defaults to the UI class of
  107. * the specified data type.
  108. * - cleaning callback: (optional) A callback that input evaluators may use
  109. * to clean inserted replacements; e.g. this is used by the token evaluator.
  110. * - wrapped: (optional) Set this to TRUE in case the data should be passed
  111. * wrapped. This only applies to wrapped data types, e.g. entities.
  112. * Each 'provides' array may contain the following properties:
  113. * - label: The label of the variable. Start capitalized. Required.
  114. * - type: The rules data type of the variable. All types declared in
  115. * hook_rules_data_info() may be specified. Types may be parametrized e.g.
  116. * the types node<page> or list<integer> are valid.
  117. * - save: (optional) If this is set to TRUE, the provided variable is saved
  118. * by rules when the rules evaluation ends. Only possible for savable data
  119. * types. Defaults to FALSE.
  120. *
  121. * The module has to provide an implementation for each action, being a
  122. * function named as specified in the 'base' key or for the execution callback.
  123. * All other possible callbacks are optional.
  124. * Supported action callbacks by rules are defined and documented in the
  125. * RulesPluginImplInterface. However any module may extend the action plugin
  126. * based upon a defined interface using hook_rules_plugin_info(). All methods
  127. * defined in those interfaces can be overridden by the action implementation.
  128. * The callback implementations for those interfaces may reside in any file
  129. * specified in hook_rules_file_info().
  130. *
  131. * @see hook_rules_file_info()
  132. * @see rules_action_execution_callback()
  133. * @see hook_rules_plugin_info()
  134. * @see RulesPluginImplInterface
  135. */
  136. function hook_rules_action_info() {
  137. return array(
  138. 'mail_user' => array(
  139. 'label' => t('Send a mail to a user'),
  140. 'parameter' => array(
  141. 'user' => array('type' => 'user', 'label' => t('Recipient')),
  142. ),
  143. 'group' => t('System'),
  144. 'base' => 'rules_action_mail_user',
  145. 'callbacks' => array(
  146. 'validate' => 'rules_action_custom_validation',
  147. 'help' => 'rules_mail_help',
  148. ),
  149. ),
  150. );
  151. }
  152. /**
  153. * Define categories for Rules items, e.g. actions, conditions or events.
  154. *
  155. * Categories are similar to the previously used 'group' key in e.g.
  156. * hook_rules_action_info(), but have a machine name and some more optional
  157. * keys like a weight, or an icon.
  158. *
  159. * For best compatibility, modules may keep using the 'group' key for referring
  160. * to categories. However, if a 'group' key and a 'category' is given the group
  161. * will be treated as grouping in the given category (e.g. group "paypal" in
  162. * category "commerce payment").
  163. *
  164. * @return
  165. * An array of information about the module's provided categories.
  166. * The array contains a sub-array for each category, with the category name as
  167. * the key. Names may only contain lowercase alpha-numeric characters
  168. * and underscores and should be prefixed with the providing module name.
  169. * Possible attributes for each sub-array are:
  170. * - label: The label of the category. Start capitalized. Required.
  171. * - weight: (optional) A weight for sorting the category. Defaults to 0.
  172. * - equals group: (optional) For BC, categories may be defined that equal
  173. * a previsouly used 'group'.
  174. * - icon: (optional) The file path of an icon to use, relative to the module
  175. * or specified icon path. The icon should be a transparent SVG containing
  176. * no colors (only #fff). See https://drupal.org/node/2090265 for
  177. * instructions on how to create a suiting icon.
  178. * Note that the icon is currently not used by Rules, however other UIs
  179. * building upon Rules (like fluxkraft) do, and future releases of Rules
  180. * might do as well. Consequently, the definition of an icon is optional.
  181. * However, if both an icon font and icon is given, the icon is preferred.
  182. * - icon path: (optional) The base path for the icon. Defaults to the
  183. * providing module's directory.
  184. * - icon font class: (optional) An icon font class referring to a suiting
  185. * icon. Icon font class names should map to the ones as defined by Font
  186. * Awesome, while themes might want to choose to provide another icon font.
  187. * See http://fortawesome.github.io/Font-Awesome/cheatsheet/.
  188. * - icon background color: (optional) The color used as icon background.
  189. * Should have a high contrast to white. Defaults to #ddd.
  190. */
  191. function hook_rules_category_info() {
  192. return array(
  193. 'rules_data' => array(
  194. 'label' => t('Data'),
  195. 'equals group' => t('Data'),
  196. 'weight' => -50,
  197. ),
  198. 'fluxtwitter' => array(
  199. 'label' => t('Twitter'),
  200. 'icon font class' => 'icon-twitter',
  201. 'icon font background color' => '#30a9fd',
  202. ),
  203. );
  204. }
  205. /**
  206. * Specify files containing rules integration code.
  207. *
  208. * All files specified in that hook will be included when rules looks for
  209. * existing callbacks for any plugin. Rules remembers which callback is found in
  210. * which file and automatically includes the right file before it is executing
  211. * a plugin method callback. The file yourmodule.rules.inc is added by default
  212. * and need not be specified here.
  213. * This allows you to add new include files only containing functions serving as
  214. * plugin method callbacks in any file without having to care about file
  215. * inclusion.
  216. *
  217. * @return
  218. * An array of file names without the file ending which defaults to '.inc'.
  219. */
  220. function hook_rules_file_info() {
  221. return array('yourmodule.rules-eval');
  222. }
  223. /**
  224. * Specifies directories for class-based plugin handler discovery.
  225. *
  226. * Implementing this hook is not a requirement, it is just one option to load
  227. * the files containing the classes during discovery - see
  228. * rules_discover_plugins().
  229. *
  230. * @return string|array
  231. * A directory relative to the module directory, which holds the files
  232. * containing rules plugin handlers, or multiple directories keyed by the
  233. * module the directory is contained in.
  234. * All files in those directories having a 'php' or 'inc' file extension will
  235. * be loaded during discovery. Optionally, wildcards ('*') may be used to
  236. * match multiple directories.
  237. *
  238. * @see rules_discover_plugins()
  239. */
  240. function hook_rules_directory() {
  241. return 'lib/Drupal/fluxtwitter/Rules/*';
  242. }
  243. /**
  244. * The execution callback for an action.
  245. *
  246. * It should be placed in any file included by your module or in a file
  247. * specified using hook_rules_file_info().
  248. *
  249. * @param
  250. * The callback gets arguments passed as described as parameter in
  251. * hook_rules_action_info() as well as an array containing the action's
  252. * configuration settings.
  253. * @return
  254. * The action may return an array containg parameter or provided variables
  255. * with their names as key. This is used update the value of a parameter or to
  256. * provdide the value for a provided variable.
  257. * Apart from that any parameters which have the key 'save' set to TRUE will
  258. * be remembered to be saved by rules unless the action returns FALSE.
  259. * Conditions have to return a boolean value in any case.
  260. *
  261. * @see hook_rules_action_info()
  262. * @see hook_rules_file_info()
  263. */
  264. function rules_action_execution_callback($node, $title, $settings) {
  265. $node->title = $title;
  266. return array('node' => $node);
  267. }
  268. /**
  269. * Define rules conditions.
  270. *
  271. * This hook is required in order to add a new rules condition. It should be
  272. * placed into the file MODULENAME.rules.inc, which gets automatically included
  273. * when the hook is invoked.
  274. *
  275. * However, as an alternative to implementing this hook, class based plugin
  276. * handlers may be provided by implementing RulesConditionHandlerInterface. See
  277. * the interface for details.
  278. *
  279. * Adding conditions works exactly the same way as adding actions, with the
  280. * exception that conditions can't provide variables and cannot save parameters.
  281. * Thus the 'provides' attribute is not supported. Furthermore the condition
  282. * implementation callback has to return a boolean value.
  283. *
  284. * @see hook_rules_action_info()
  285. */
  286. function hook_rules_condition_info() {
  287. return array(
  288. 'rules_condition_text_compare' => array(
  289. 'label' => t('Textual comparison'),
  290. 'parameter' => array(
  291. 'text1' => array('label' => t('Text 1'), 'type' => 'text'),
  292. 'text2' => array('label' => t('Text 2'), 'type' => 'text'),
  293. ),
  294. 'group' => t('Rules'),
  295. ),
  296. );
  297. }
  298. /**
  299. * Define rules events.
  300. *
  301. * This hook is required in order to add a new rules event. It should be
  302. * placed into the file MODULENAME.rules.inc, which gets automatically included
  303. * when the hook is invoked.
  304. * The module has to invoke the event when it occurs using rules_invoke_event().
  305. * This function call has to happen outside of MODULENAME.rules.inc,
  306. * usually it's invoked directly from the providing module but wrapped by a
  307. * module_exists('rules') check.
  308. *
  309. * However, as an alternative to implementing this hook, class based event
  310. * handlers may be provided by implementing RulesEventHandlerInterface. See
  311. * the interface for details.
  312. *
  313. * @return
  314. * An array of information about the module's provided rules events. The array
  315. * contains a sub-array for each event, with the event name as the key. The
  316. * name may only contain lower case alpha-numeric characters and underscores
  317. * and should be prefixed with the providing module name. Possible attributes
  318. * for each sub-array are:
  319. * - label: The label of the event. Start capitalized. Required.
  320. * - group: A group for this element, used for grouping the events in the
  321. * interface. Should start with a capital letter and be translated.
  322. * Required.
  323. * - class: (optional) An event handler class implementing the
  324. * RulesEventHandlerInterface. If none is specified the
  325. * RulesEventDefaultHandler class will be used. While the default event
  326. * handler has no settings, custom event handlers may be implemented to
  327. * to make an event configurable. See RulesEventHandlerInterface.
  328. * - access callback: (optional) An callback, which has to return whether the
  329. * currently logged in user is allowed to configure rules for this event.
  330. * Access should be only granted, if the user at least may access all the
  331. * variables provided by the event.
  332. * - help: (optional) A help text for rules reaction on this event.
  333. * - variables: (optional) An array describing all variables that are
  334. * available for elements reacting on this event. Each variable has to be
  335. * described by a sub-array with the possible attributes:
  336. * - label: The label of the variable. Start capitalized. Required.
  337. * - type: The rules data type of the variable. All types declared in
  338. * hook_rules_data_info() or supported by hook_entity_property_info() may
  339. * be specified.
  340. * - bundle: (optional) If the type is an entity type, the bundle of the
  341. * entity.
  342. * - description: (optional) A description for the variable.
  343. * - 'options list': (optional) A callback that returns an array of possible
  344. * values for this variable as specified for entity properties at
  345. * hook_entity_property_info().
  346. * - 'skip save': (optional) If the variable is saved after the event has
  347. * occurred anyway, set this to TRUE. So rules won't save the variable a
  348. * second time. Defaults to FALSE.
  349. * - handler: (optional) A handler to load the actual variable value. This
  350. * is useful for lazy loading variables. The handler gets all so far
  351. * available variables passed in the order as defined. Also see
  352. * http://drupal.org/node/884554.
  353. * Note that for lazy-loading entities just the entity id may be passed
  354. * as variable value, so a handler is not necessary in that case.
  355. *
  356. * @see rules_invoke_event()
  357. */
  358. function hook_rules_event_info() {
  359. $items = array(
  360. 'node_insert' => array(
  361. 'label' => t('After saving new content'),
  362. 'group' => t('Node'),
  363. 'variables' => rules_events_node_variables(t('created content')),
  364. ),
  365. 'node_update' => array(
  366. 'label' => t('After updating existing content'),
  367. 'group' => t('Node'),
  368. 'variables' => rules_events_node_variables(t('updated content'), TRUE),
  369. ),
  370. 'node_presave' => array(
  371. 'label' => t('Content is going to be saved'),
  372. 'group' => t('Node'),
  373. 'variables' => rules_events_node_variables(t('saved content'), TRUE),
  374. ),
  375. 'node_view' => array(
  376. 'label' => t('Content is going to be viewed'),
  377. 'group' => t('Node'),
  378. 'variables' => rules_events_node_variables(t('viewed content')) + array(
  379. 'view_mode' => array('type' => 'text', 'label' => t('view mode')),
  380. ),
  381. ),
  382. 'node_delete' => array(
  383. 'label' => t('After deleting content'),
  384. 'group' => t('Node'),
  385. 'variables' => rules_events_node_variables(t('deleted content')),
  386. ),
  387. );
  388. // Specify that on presave the node is saved anyway.
  389. $items['node_presave']['variables']['node']['skip save'] = TRUE;
  390. return $items;
  391. }
  392. /**
  393. * Define rules data types.
  394. *
  395. * This hook is required in order to add a new rules data type. It should be
  396. * placed into the file MODULENAME.rules.inc, which gets automatically included
  397. * when the hook is invoked.
  398. * Rules builds upon the entity metadata module, thus to improve the support of
  399. * your data in rules, make it an entity if possible and provide metadata about
  400. * its properties and CRUD functions by integrating with the entity metadata
  401. * module.
  402. * For a list of data types defined by rules see rules_rules_core_data_info().
  403. *
  404. *
  405. * @return
  406. * An array of information about the module's provided data types. The array
  407. * contains a sub-array for each data type, with the data type name as the
  408. * key. The name may only contain lower case alpha-numeric characters and
  409. * underscores and should be prefixed with the providing module name. Possible
  410. * attributes for each sub-array are:
  411. * - label: The label of the data type. Start uncapitalized. Required.
  412. * - parent: (optional) A parent type may be set to specify a sub-type
  413. * relationship, which will be only used for checking compatible types. E.g.
  414. * the 'entity' data type is parent of the 'node' data type, thus a node may
  415. * be also used for any action needing an 'entity' parameter. Can be set to
  416. * any known rules data type.
  417. * - ui class: (optional) Specify a class that is used to generate the
  418. * configuration UI to configure parameters of this type. The given class
  419. * must extend RulesDataUI and may implement the
  420. * RulesDataDirectInputFormInterface in order to allow the direct data input
  421. * configuration mode. For supporting selecting values from options lists,
  422. * the UI class may implement RulesDataInputOptionsListInterface also.
  423. * Defaults to RulesDataUI.
  424. * - wrap: (optional) If set to TRUE, the data is wrapped internally using
  425. * wrappers provided by the entity API module. This is required for entities
  426. * and data structures to support selecting a property via the data selector
  427. * and for intelligent saving.
  428. * - is wrapped: (optional) In case the data wrapper is already wrapped when
  429. * passed to Rules and Rules should not unwrap it when passing the data as
  430. * argument, e.g. to an action, set this to TRUE. The default FALSE is fine
  431. * in most cases.
  432. * - wrapper class: (optional) Allows the specification of a custom wrapper
  433. * class, which has to inherit from 'EntityMetadataWrapper'. If given Rules
  434. * makes use of the class for wrapping the data of the given type. However
  435. * note that if data is already wrapped when it is passed to Rules, the
  436. * existing wrappers will be kept.
  437. * For modules implementing identifiable data types being non-entites the
  438. * class RulesIdentifiableDataWrapper is provided, which can be used as base
  439. * for a custom wrapper class. See RulesIdentifiableDataWrapper for details.
  440. * - property info: (optional) May be used for non-entity data structures to
  441. * provide info about the data properties, such that data selectors via an
  442. * entity metadata wrapper are supported. Specify an array as expected by
  443. * the $info parameter of entity_metadata_wrapper().
  444. * - creation callback: (optional) If 'property info' is given, an optional
  445. * callback that makes use of the property info to create a new instance of
  446. * this data type. Entities should use hook_entity_info() to specify the
  447. * 'creation callback' instead, as utilized by the entity API module. See
  448. * rules_action_data_create_array() for an example callback.
  449. * - property defaults: (optional) May be used for non-entity data structures
  450. * to to provide property info defaults for the data properties. Specify an
  451. * array as expected by entity_metadata_wrapper().
  452. * - group: (optional) A group for this element, used for grouping the data
  453. * types in the interface. Should start with a capital letter and be
  454. * translated.
  455. * - token type: (optional) The type name as used by the token module.
  456. * Defaults to the type name as used by rules. Use FALSE to let token ignore
  457. * this type.
  458. * - cleaning callback: (optional) A callback that input evaluators may use
  459. * to clean inserted replacements; e.g. this is used by the token evaluator.
  460. *
  461. * @see entity_metadata_wrapper()
  462. * @see hook_rules_data_info_alter()
  463. * @see rules_rules_core_data_info()
  464. */
  465. function hook_rules_data_info() {
  466. return array(
  467. 'node' => array(
  468. 'label' => t('content'),
  469. 'parent' => 'entity',
  470. 'group' => t('Node'),
  471. ),
  472. // Formatted text as used by in hook_entity_property_info() for text fields.
  473. 'text_formatted' => array(
  474. 'label' => t('formatted text'),
  475. 'ui class' => 'RulesDataUITextFormatted',
  476. 'wrap' => TRUE,
  477. 'property info' => entity_property_text_formatted_info(),
  478. ),
  479. );
  480. }
  481. /**
  482. * Defines rules plugins.
  483. *
  484. * A rules configuration may consist of elements being instances of any rules
  485. * plugin. This hook can be used to define new or to extend rules plugins.
  486. *
  487. * @return
  488. * An array of information about the module's provided rules plugins. The
  489. * array contains a sub-array for each plugin, with the plugin name as the
  490. * key. The name may only contain lower case alpha-numeric characters,
  491. * underscores and spaces and should be prefixed with the providing module
  492. * name. Possible attributes for
  493. * each sub-array are:
  494. * - label: A label for the plugin. Start capitalized. Required only for
  495. * components (see below).
  496. * - class: The implementation class. Has to extend the RulesPlugin class.
  497. * - embeddable: A container class in which elements of those plugin may be
  498. * embedded via the UI or FALSE to disallow embedding it via the UI. This
  499. * has no implications on the API level though. Common classes that are
  500. * used here are RulesConditionContainer and RulesActionContainer.
  501. * - component: If set to TRUE, the rules admin UI will list elements of those
  502. * plugin in the components UI and allows the creation of new components
  503. * based upon this plugin. Optional.
  504. * - extenders: This allows one to specify faces extenders, which may be used
  505. * to dynamically implement interfaces. Optional. All extenders specified
  506. * here are setup automatically by rules once the object is created. To
  507. * specify set this to an array, where the keys are the implemented
  508. * interfaces pointing to another array with the keys:
  509. * - class: The class of the extender, implementing the FacesExtender
  510. * and the specified interface. Either 'class' or 'methods' has to exist.
  511. * - methods: An array of callbacks that implement the methods of the
  512. * interface where the method names are the keys and the callback names
  513. * the values. There has to be a callback for each defined method.
  514. * - file: An optional array describing the file to include when a method
  515. * of the interface is invoked. The array entries known are 'type',
  516. * 'module', and 'name' matching the parameters of module_load_include().
  517. * Only 'module' is required as 'type' defaults to 'inc' and 'name' to
  518. * NULL.
  519. * - overrides: An optional array, which may be used to specify callbacks to
  520. * override specific methods. For that the following keys are supported:
  521. * - methods: As in the extenders array, but you may specify as many methods
  522. * here as you like.
  523. * - file: Optionally an array specifying a file to include for a method.
  524. * For each method appearing in methods a file may be specified by using
  525. * the method name as key and another array as value, which describes the
  526. * file to include - looking like the file array supported by 'extenders'.
  527. * - import keys: (optional) Embeddable plugins may specify an array of import
  528. * keys, which the plugin make use for exporting. Defaults to the upper
  529. * case plugin name, thus the key 'OR' in an export triggers the creation
  530. * of the 'or' plugin. Note that only uppercase values are allowed, as
  531. * lower case values are treated as action or condition exports.
  532. *
  533. * @see class RulesPlugin
  534. * @see hook_rules_plugin_info_alter()
  535. */
  536. function hook_rules_plugin_info() {
  537. return array(
  538. 'or' => array(
  539. 'label' => t('Condition set (OR)'),
  540. 'class' => 'RulesOr',
  541. 'embeddable' => 'RulesConditionContainer',
  542. 'component' => TRUE,
  543. 'extenders' => array(
  544. 'RulesPluginUIInterface' => array(
  545. 'class' => 'RulesConditionContainerUI',
  546. ),
  547. ),
  548. ),
  549. 'rule' => array(
  550. 'class' => 'Rule',
  551. 'embeddable' => 'RulesRuleSet',
  552. 'extenders' => array(
  553. 'RulesPluginUIInterface' => array(
  554. 'class' => 'RulesRuleUI',
  555. ),
  556. ),
  557. 'import keys' => array('DO', 'IF'),
  558. ),
  559. );
  560. }
  561. /**
  562. * Declare provided rules input evaluators.
  563. *
  564. * The hook implementation should be placed into the file MODULENAME.rules.inc,
  565. * which gets automatically included when the hook is invoked.
  566. * For implementing an input evaluator a class has to be provided which
  567. * extends the abstract RulesDataInputEvaluator class. Therefore the abstract
  568. * methods prepare() and evaluate() have to be implemented, as well as access()
  569. * and help() could be overridden in order to control access permissions or to
  570. * provide some usage help.
  571. *
  572. * @return
  573. * An array of information about the module's provided input evaluators. The
  574. * array contains a sub-array for each evaluator, with the evaluator name as
  575. * the key. The name may only contain lower case alpha-numeric characters and
  576. * underscores and should be prefixed with the providing module name. Possible
  577. * attributes for each sub-array are:
  578. * - class: The implementation class, which has to extend the
  579. * RulesDataInputEvaluator class. Required.
  580. * - weight: A weight for controlling the evaluation order of multiple
  581. * evaluators. Required.
  582. * - type: Optionally, the data types for which the input evaluator should be
  583. * used. Defaults to 'text'. Multiple data types may be specified using an
  584. * array.
  585. *
  586. * @see class RulesDataInputEvaluator
  587. * @see hook_rules_evaluator_info_alter()
  588. */
  589. function hook_rules_evaluator_info() {
  590. return array(
  591. 'token' => array(
  592. 'class' => 'RulesTokenEvaluator',
  593. 'type' => array('text', 'uri'),
  594. 'weight' => 0,
  595. ),
  596. );
  597. }
  598. /**
  599. * Declare provided rules data processors.
  600. *
  601. * The hook implementation should be placed into the file MODULENAME.rules.inc,
  602. * which gets automatically included when the hook is invoked.
  603. * For implementing a data processors a class has to be provided which
  604. * extends the abstract RulesDataProcessor class. Therefore the abstract
  605. * method process() has to be implemented, but also the methods form() and
  606. * access() could be overridden in order to provide a configuration form or
  607. * to control access permissions.
  608. *
  609. * @return
  610. * An array of information about the module's provided data processors. The
  611. * array contains a sub-array for each processor, with the processor name as
  612. * the key. The name may only contain lower case alpha-numeric characters and
  613. * underscores and should be prefixed with the providing module name, whereas
  614. * 'select' is reserved as well.
  615. * Possible attributes for each sub-array are:
  616. * - class: The implementation class, which has to extend the
  617. * RulesDataProcessor class. Required.
  618. * - weight: A weight for controlling the processing order of multiple data
  619. * processors. Required.
  620. * - type: Optionally, the data types for which the data processor should be
  621. * used. Defaults to 'text'. Multiple data types may be specified using an
  622. * array.
  623. *
  624. * @see class RulesDataProcessor
  625. * @see hook_rules_data_processor_info_alter()
  626. */
  627. function hook_rules_data_processor_info() {
  628. return array(
  629. 'date_offset' => array(
  630. 'class' => 'RulesDateOffsetProcessor',
  631. 'type' => 'date',
  632. 'weight' => -2,
  633. ),
  634. );
  635. }
  636. /**
  637. * Alter rules compatible actions.
  638. *
  639. * The implementation should be placed into the file MODULENAME.rules.inc, which
  640. * gets automatically included when the hook is invoked.
  641. *
  642. * @param $actions
  643. * The items of all modules as returned from hook_rules_action_info().
  644. *
  645. * @see hook_rules_action_info().
  646. */
  647. function hook_rules_action_info_alter(&$actions) {
  648. // The rules action is more powerful, so hide the core action
  649. unset($actions['rules_core_node_assign_owner_action']);
  650. // We prefer handling saving by rules - not by the user.
  651. unset($actions['rules_core_node_save_action']);
  652. }
  653. /**
  654. * Alter rules conditions.
  655. *
  656. * The implementation should be placed into the file MODULENAME.rules.inc, which
  657. * gets automatically included when the hook is invoked.
  658. *
  659. * @param $conditions
  660. * The items of all modules as returned from hook_rules_condition_info().
  661. *
  662. * @see hook_rules_condition_info()
  663. */
  664. function hook_rules_condition_info_alter(&$conditions) {
  665. // Change conditions.
  666. }
  667. /**
  668. * Alter rules events.
  669. *
  670. * The implementation should be placed into the file MODULENAME.rules.inc, which
  671. * gets automatically included when the hook is invoked.
  672. *
  673. * @param $events
  674. * The items of all modules as returned from hook_rules_event_info().
  675. *
  676. * @see hook_rules_event_info().
  677. */
  678. function hook_rules_event_info_alter(&$events) {
  679. // Change events.
  680. }
  681. /**
  682. * Alter rules data types.
  683. *
  684. * The implementation should be placed into the file MODULENAME.rules.inc, which
  685. * gets automatically included when the hook is invoked.
  686. *
  687. * @param $data_info
  688. * The items of all modules as returned from hook_rules_data_info().
  689. *
  690. * @see hook_rules_data_info()
  691. */
  692. function hook_rules_data_info_alter(&$data_info) {
  693. // Change data types.
  694. }
  695. /**
  696. * Alter rules plugin info.
  697. *
  698. * The implementation should be placed into the file MODULENAME.rules.inc, which
  699. * gets automatically included when the hook is invoked.
  700. *
  701. * @param $plugin_info
  702. * The items of all modules as returned from hook_rules_plugin_info().
  703. *
  704. * @see hook_rules_plugin_info()
  705. */
  706. function hook_rules_plugin_info_alter(&$plugin_info) {
  707. // Change plugin info.
  708. }
  709. /**
  710. * Alter rules input evaluator info.
  711. *
  712. * The implementation should be placed into the file MODULENAME.rules.inc, which
  713. * gets automatically included when the hook is invoked.
  714. *
  715. * @param $evaluator_info
  716. * The items of all modules as returned from hook_rules_evaluator_info().
  717. *
  718. * @see hook_rules_evaluator_info()
  719. */
  720. function hook_rules_evaluator_info_alter(&$evaluator_info) {
  721. // Change evaluator info.
  722. }
  723. /**
  724. * Alter rules data_processor info.
  725. *
  726. * The implementation should be placed into the file MODULENAME.rules.inc, which
  727. * gets automatically included when the hook is invoked.
  728. *
  729. * @param $processor_info
  730. * The items of all modules as returned from hook_rules_data_processor_info().
  731. *
  732. * @see hook_rules_data_processor_info()
  733. */
  734. function hook_rules_data_processor_info_alter(&$processor_info) {
  735. // Change processor info.
  736. }
  737. /**
  738. * Act on rules configuration being loaded from the database.
  739. *
  740. * This hook is invoked during rules configuration loading, which is handled
  741. * by entity_load(), via classes RulesEntityController and EntityCRUDController.
  742. *
  743. * @param $configs
  744. * An array of rules configurations being loaded, keyed by id.
  745. */
  746. function hook_rules_config_load($configs) {
  747. $result = db_query('SELECT id, foo FROM {mytable} WHERE id IN(:ids)', array(':ids' => array_keys($configs)));
  748. foreach ($result as $record) {
  749. $configs[$record->id]->foo = $record->foo;
  750. }
  751. }
  752. /**
  753. * Respond to creation of a new rules configuration.
  754. *
  755. * This hook is invoked after the rules configuration is inserted into the
  756. * the database.
  757. *
  758. * @param RulesPlugin $config
  759. * The rules configuration that is being created.
  760. */
  761. function hook_rules_config_insert($config) {
  762. db_insert('mytable')
  763. ->fields(array(
  764. 'nid' => $config->id,
  765. 'plugin' => $config->plugin,
  766. ))
  767. ->execute();
  768. }
  769. /**
  770. * Act on a rules configuration being inserted or updated.
  771. *
  772. * This hook is invoked before the rules configuration is saved to the
  773. * database.
  774. *
  775. * @param RulesPlugin $config
  776. * The rules configuration that is being inserted or updated.
  777. */
  778. function hook_rules_config_presave($config) {
  779. if ($config->id && $config->owner == 'your_module') {
  780. // Add custom condition.
  781. $config->conditon(/* Your condition */);
  782. }
  783. }
  784. /**
  785. * Respond to updates to a rules configuration.
  786. *
  787. * This hook is invoked after the configuration has been updated in the
  788. * database.
  789. *
  790. * @param RulesPlugin $config
  791. * The rules configuration that is being updated.
  792. */
  793. function hook_rules_config_update($config) {
  794. db_update('mytable')
  795. ->fields(array('plugin' => $config->plugin))
  796. ->condition('id', $config->id)
  797. ->execute();
  798. }
  799. /**
  800. * Respond to rules configuration deletion.
  801. *
  802. * This hook is invoked after the configuration has been removed from the
  803. * database.
  804. *
  805. * @param RulesPlugin $config
  806. * The rules configuration that is being deleted.
  807. */
  808. function hook_rules_config_delete($config) {
  809. db_delete('mytable')
  810. ->condition('id', $config->id)
  811. ->execute();
  812. }
  813. /**
  814. * Respond to rules configuration execution.
  815. *
  816. * This hook is invoked right before the rules configuration is executed.
  817. *
  818. * @param RulesPlugin $config
  819. * The rules configuration that is being executed.
  820. */
  821. function hook_rules_config_execute($config) {
  822. }
  823. /**
  824. * Define default rules configurations.
  825. *
  826. * This hook is invoked when rules configurations are loaded. The implementation
  827. * should be placed into the file MODULENAME.rules_defaults.inc, which gets
  828. * automatically included when the hook is invoked.
  829. *
  830. * @return
  831. * An array of rules configurations with the configuration names as keys.
  832. *
  833. * @see hook_default_rules_configuration_alter()
  834. * @see hook_rules_config_defaults_rebuild()
  835. */
  836. function hook_default_rules_configuration() {
  837. $rule = rules_reaction_rule();
  838. $rule->label = 'example default rule';
  839. $rule->active = FALSE;
  840. $rule->event('node_update')
  841. ->condition(rules_condition('data_is', array('data:select' => 'node:status', 'value' => TRUE))->negate())
  842. ->condition('data_is', array('data:select' => 'node:type', 'value' => 'page'))
  843. ->action('drupal_message', array('message' => 'A node has been updated.'));
  844. $configs['rules_test_default_1'] = $rule;
  845. return $configs;
  846. }
  847. /**
  848. * Alter default rules configurations.
  849. *
  850. * The implementation should be placed into the file
  851. * MODULENAME.rules_defaults.inc, which gets automatically included when the
  852. * hook is invoked.
  853. *
  854. * @param $configs
  855. * The default configurations of all modules as returned from
  856. * hook_default_rules_configuration().
  857. *
  858. * @see hook_default_rules_configuration()
  859. */
  860. function hook_default_rules_configuration_alter(&$configs) {
  861. // Add custom condition.
  862. $configs['foo']->condition('bar');
  863. }
  864. /**
  865. * Act after rebuilding default configurations.
  866. *
  867. * This hook is invoked by the entity module after default rules configurations
  868. * have been rebuilt; i.e. defaults have been saved to the database.
  869. *
  870. * @param $rules_configs
  871. * The array of default rules configurations which have been inserted or
  872. * updated, keyed by name.
  873. * @param $originals
  874. * An array of original rules configurations keyed by name; i.e. the rules
  875. * configurations before the current defaults have been applied. For inserted
  876. * rules configurations no original is available.
  877. *
  878. * @see hook_default_rules_configuration()
  879. * @see entity_defaults_rebuild()
  880. */
  881. function hook_rules_config_defaults_rebuild($rules_configs, $originals) {
  882. // Once all defaults have been rebuilt, update all i18n strings at once. That
  883. // way we build the rules cache once the rebuild is complete and avoid
  884. // rebuilding caches for each updated rule.
  885. foreach ($rules_configs as $name => $rule_config) {
  886. if (empty($originals[$name])) {
  887. rules_i18n_rules_config_insert($rule_config);
  888. }
  889. else {
  890. rules_i18n_rules_config_update($rule_config, $originals[$name]);
  891. }
  892. }
  893. }
  894. /**
  895. * Alter rules components before execution.
  896. *
  897. * This hooks allows altering rules components before they are cached for later
  898. * re-use. Use this hook only for altering the component in order to prepare
  899. * re-use through rules_invoke_component() or the provided condition/action.
  900. * Note that this hook is only invoked for any components cached for execution,
  901. * but not for components that are programmatically created and executed on the
  902. * fly (without saving them).
  903. *
  904. * @param $plugin
  905. * The name of the component plugin.
  906. * @param $component
  907. * The component that is to be cached.
  908. *
  909. * @see rules_invoke_component()
  910. */
  911. function hook_rules_component_alter($plugin, RulesPlugin $component) {
  912. }
  913. /**
  914. * Alters event sets.
  915. *
  916. * This hooks allows altering rules event sets, which contain all rules that are
  917. * triggered upon a specific event. Rules internally caches all rules associated
  918. * to an event in an event set, which is cached for fast evaluation. This hook
  919. * is invoked just before any event set is cached, thus it allows altering of
  920. * the to be executed rules without the changes to appear in the UI, e.g. to add
  921. * a further condition to some rules.
  922. *
  923. * @param $event_name
  924. * The name of the event.
  925. * @param $event_set
  926. * The event set that is to be cached.
  927. *
  928. * @see rules_invoke_event()
  929. */
  930. function hook_rules_event_set_alter($event_name, RulesEventSet $event_set) {
  931. }
  932. /**
  933. * D6 to D7 upgrade procedure hook for mapping action or condition names.
  934. *
  935. * If for a module the action or condition name changed since Drupal 6, this
  936. * "hook" can be implemented in order to map to the new name of the action or
  937. * condition.
  938. *
  939. * This is no real hook, but a callback that is invoked for each Drupal 6
  940. * action or condition that is to be upgraded to Drupal 7. E.g. the function
  941. * name called for the action "rules_action_set_node_title" would be
  942. * "rules_action_set_node_title_upgrade_map_name".
  943. *
  944. * @param $element
  945. * The element array of a configured condition or action which is to be
  946. * upgraded.
  947. * @return
  948. * The name of the action or condition which should be used.
  949. */
  950. function hook_rules_action_base_upgrade_map_name($element) {
  951. return 'data_set';
  952. }
  953. /**
  954. * D6 to D7 upgrade procedure hook for mapping action or condition configuration.
  955. *
  956. * During upgrading Drupal 6 rule configurations to Drupal 7 Rules is taking
  957. * care of upgrading the configuration of all known parameters, which only works
  958. * if the parameter name has not changed.
  959. * If something changed, this callback can be used to properly apply the
  960. * configruation of the Drupal 6 action ($element) to the Drupal 7 version
  961. * ($target).
  962. *
  963. * This is no real hook, but a callback that is invoked for each Drupal 6
  964. * action or condition that is to be upgraded to Drupal 7. E.g. the function
  965. * name called for the action "rules_action_set_node_title" would be
  966. * "rules_action_set_node_title_upgrade".
  967. *
  968. * @param $element
  969. * The element array of a configured condition or action which is to be
  970. * upgraded.
  971. * @param $target
  972. * The Drupal 7 version of the configured element.
  973. *
  974. * @see hook_rules_element_upgrade_alter()
  975. */
  976. function hook_rules_action_base_upgrade($element, RulesPlugin $target) {
  977. $target->settings['data:select'] = $element['#settings']['#argument map']['node'] . ':title';
  978. $target->settings['value'] = $element['#settings']['title'];
  979. }
  980. /**
  981. * D6 to D7 upgrade procedure hook for mapping action or condition configuration.
  982. *
  983. * A alter hook that is called after the action/condition specific callback for
  984. * each element of a configuration that is upgraded.
  985. *
  986. * @param $element
  987. * The element array of a configured condition or action which is to be
  988. * upgraded.
  989. * @param $target
  990. * The Drupal 7 version of the configured element.
  991. *
  992. * @see hook_rules_action_base_upgrade()
  993. */
  994. function hook_rules_element_upgrade_alter($element, $target) {
  995. }
  996. /**
  997. * Allows modules to alter or to extend the provided Rules UI.
  998. *
  999. * Use this hook over the regular hook_menu_alter() as the Rules UI is re-used
  1000. * and embedded by modules. See rules_ui().
  1001. *
  1002. * @param $items
  1003. * The menu items to alter.
  1004. * @param $base_path
  1005. * The base path of the Rules UI.
  1006. * @param $base_count
  1007. * The count of the directories contained in the base path.
  1008. */
  1009. function hook_rules_ui_menu_alter(&$items, $base_path, $base_count) {
  1010. $items[$base_path . '/manage/%rules_config/schedule'] = array(
  1011. 'title callback' => 'rules_get_title',
  1012. 'title arguments' => array('Schedule !plugin "!label"', $base_count + 1),
  1013. 'page callback' => 'drupal_get_form',
  1014. 'page arguments' => array('rules_scheduler_schedule_form', $base_count + 1, $base_path),
  1015. 'access callback' => 'rules_config_access',
  1016. 'access arguments' => array('update', $base_count + 1),
  1017. 'file' => 'rules_scheduler.admin.inc',
  1018. 'file path' => drupal_get_path('module', 'rules_scheduler'),
  1019. );
  1020. }
  1021. /**
  1022. * Control access to Rules configurations.
  1023. *
  1024. * Modules may implement this hook if they want to have a say in whether or not
  1025. * a given user has access to perform a given operation on a Rules
  1026. * configuration.
  1027. *
  1028. * @param $op
  1029. * The operation being performed. One of 'view', 'create', 'update' or
  1030. * 'delete'.
  1031. * @param $rules_config
  1032. * (optional) A Rules configuration to check access for. If nothing is given,
  1033. * access for all Rules configurations is determined.
  1034. * @param $account
  1035. * (optional) The user to check for. If no account is passed, access is
  1036. * determined for the current user.
  1037. * @return boolean
  1038. * Return TRUE to grant access, FALSE to explicitly deny access. Return NULL
  1039. * or nothing to not affect the operation.
  1040. * Access is granted as soon as a module grants access and no one denies
  1041. * access. Thus if no module explicitly grants access, access will be denied.
  1042. *
  1043. * @see rules_config_access()
  1044. */
  1045. function hook_rules_config_access($op, $rules_config = NULL, $account = NULL) {
  1046. // Instead of returning FALSE return nothing, so others still can grant
  1047. // access.
  1048. if (isset($rules_config) && $rules_config->owner == 'mymodule' && user_access('my modules permission')) {
  1049. return TRUE;
  1050. }
  1051. }
  1052. /**
  1053. * @}
  1054. */