rules.api.php 44 KB

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