action_example.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * @file
  4. * Action definition example module.
  5. */
  6. /**
  7. * @defgroup action_example Example: Action
  8. * @ingroup examples
  9. * @{
  10. * Creating actions in Drupal 7
  11. *
  12. * Triggers and actions are a matched pair of Drupal features allowing some
  13. * Drupal programming without using PHP. Using the appropriate action in a
  14. * specific event, a site administrator can add new functionality.
  15. *
  16. * Examples are:
  17. * - Send an email after a node is published or edited.
  18. * - Display a message after a user has logged in.
  19. * - Display a message and send an email after a node has been deleted.
  20. *
  21. * A trigger is a special function which can enqueue actions. The trigger module
  22. * provides the interface allowing us to associate certain actions with certain
  23. * triggers.
  24. *
  25. * Actions are the functions designed to be run by triggers.
  26. *
  27. * A trigger should build the appropriate context for the action to be fired.
  28. * Actions are very often grouped by functionality: examples are 'user', 'node',
  29. * 'taxonomy'. When actions are grouped it is because they expect the same
  30. * arguments. This way, you can enqueue as many actions understanding the 'user'
  31. * object as you want.
  32. *
  33. * Not all actions can be used in all triggers because they require different
  34. * contexts. But some actions are generic enough that they do not require
  35. * special objects in their contexts, and so can be used on every available
  36. * trigger. This 'group' type is used by actions to be available for this
  37. * trigger.
  38. *
  39. * What are good candidates to be triggers? Any function can be a trigger, as
  40. * long as it has the code to call the enqueued actions, but to make Drupal
  41. * more extensible, you will find hooks (from Drupal and contributed modules)
  42. * very good candidates. A trigger should build the arguments, ask for enqueued
  43. * actions and run them. You may define a function being a trigger, and run it
  44. * through a button in the front page, or you may prepare a trigger for a hook,
  45. * and everytime that hook is fired, your trigger will be.
  46. *
  47. * What are good candidates to be actions? any function is a possible action,
  48. * the only problem is finding a trigger able to run it.
  49. *
  50. * This module describes how to create actions for Drupal. In this
  51. * example we are providing three actions:
  52. *
  53. * - A generic action that can be used in any trigger, which is the most
  54. * basic example of an action.
  55. *
  56. * - An action which which extends the capabilities of User triggers, even if
  57. * associated with node or comment events.
  58. *
  59. * - An action which extends the capabilities of node triggers, but limited
  60. * to certain events only, and using a customizable option.
  61. *
  62. * @link http://drupal.org/node/172152 Writing Actions in Drupal 6 @endlink
  63. * @link http://drupal.org/node/199254 Triggers and Actions in Drupal 6 @endlink
  64. *
  65. * @see trigger_example
  66. * @see hook_action_info()
  67. */
  68. /**
  69. * Implements hook_action_info().
  70. *
  71. * We call hook_action_info when we are defining the actions we provide.
  72. * Actions are the actions fired by the associated triggers. In this example,
  73. * we are registering our three new actions, providing the unique name (using
  74. * Drupal's convention modulename_description_action), an easy to understand
  75. * description of what the action does, the 'object' expected by this action
  76. * (default options from core are node, user, comment and system, however other
  77. * trigger modules may declare new object types), which are the triggers allowed
  78. * to use these action, and if some customization is available. Please, note
  79. * that the function name is not required to finish as _action to be declared as
  80. * a Drupal action, and that only information provided by hook_trigger_info()
  81. * will be considered for valid actions creation.
  82. *
  83. * These are the actions being provided in hook_action_info()
  84. *
  85. * - action_example_basic_action: this action is a dummy function which can be
  86. * used by any trigger. The label describes that the action will do nothing,
  87. * but is enough for a basic example. Type is set to system, so users will not
  88. * be confused about the scope of this action (expecting a node, user, or any
  89. * other object). This action is not configurable, and will appear as
  90. * available in the list of action under the menu entry:
  91. * 'admin/config/system/actions.
  92. * - action_example_unblock_user_action: Unblocks a user.
  93. * - action_example_node_sticky_action: This action is a complex action that is
  94. * only available to Node type triggers, and can only be associated with the
  95. * events node presave, node insert and node update. The action does not
  96. * exist by default and it has to be created by user configuration. This makes
  97. * it an "advanced action" in Drupal, so-called because it requires
  98. * configuration or customization.
  99. * In this example action, the action will promote nodes and make them sticky
  100. * during presave, insert, or update, but only for particular users. As an
  101. * advanced action, it first needs to be created in the actions management
  102. * page (admin/config/system/actions). At the bottom of that page a selection
  103. * list shows a list of advanced actions that will includes the option
  104. * 'Promote to frontpage and sticky on top any content created by :'
  105. * Selecting this option and clicking the 'Create' button, a configuration
  106. * form will ask for an author name. When this action is associated to any
  107. * of the possible Node trigger events, it will only be effective if the
  108. * author of the content matches the author configured by the action.
  109. *
  110. * We return an associative array of action descriptions. The keys of the array
  111. * are the names of the action functions, and each corresponding value
  112. * is an associative array with the following key-value pairs:
  113. *
  114. * - 'type': The type of object this action acts upon. Core actions have types
  115. * 'node', 'user', 'comment', and 'system', but additional types can be
  116. * used, as long as the trigger and action agree on them.
  117. * - 'label': The human-readable name of the action, which should be passed
  118. * through the t() function for translation.
  119. * - 'configurable': If FALSE, then the action doesn't require any extra
  120. * configuration. If TRUE, then your module must define a form function with
  121. * the same name as the action function with '_form' appended (e.g., the
  122. * form for 'node_assign_owner_action' is 'node_assign_owner_action_form'.)
  123. * This function takes $context as its only parameter, and is paired with
  124. * the usual _submit function, and possibly an _validate function.
  125. * - 'triggers': An array of the triggers that can trigger this
  126. * action. For example: array('node_insert', 'user_update'). You can also
  127. * declare support for any trigger by returning array('any') for this value.
  128. * - 'behavior': (optional) A machine-readable array of behaviors of this
  129. * action, used to signal additionally required actions that may need to be
  130. * triggered. Currently recognized behaviors by Trigger module:
  131. * - 'changes_property': If an action with this behavior is assigned to a
  132. * trigger other than a "presave" hook, any save actions also assigned to
  133. * this trigger are moved later in the list. If no save action is present,
  134. * one will be added.
  135. * Modules that are processing actions (like Trigger module) should take
  136. * special care in the "presave" hook, in which case a dependent "save"
  137. * action should NOT be invoked.
  138. *
  139. * @see hook_action_info()
  140. */
  141. function action_example_action_info() {
  142. return array(
  143. 'action_example_basic_action' => array(
  144. 'label' => t('Action Example: A basic example action that does nothing'),
  145. 'type' => 'system',
  146. 'configurable' => FALSE,
  147. 'triggers' => array('any'),
  148. ),
  149. 'action_example_unblock_user_action' => array(
  150. 'label' => t('Action Example: Unblock a user'),
  151. 'type' => 'user',
  152. 'configurable' => FALSE,
  153. 'triggers' => array('any'),
  154. ),
  155. 'action_example_node_sticky_action' => array(
  156. 'type' => 'node',
  157. 'label' => t('Action Example: Promote to frontpage and sticky on top any content created by :'),
  158. 'configurable' => TRUE,
  159. 'behavior' => array('changes_property'),
  160. 'triggers' => array('node_presave', 'node_insert', 'node_update'),
  161. ),
  162. );
  163. }
  164. /**
  165. * Implements hook_menu().
  166. *
  167. * Provides a menu entry which explains what the module does.
  168. */
  169. function action_example_menu() {
  170. $items['examples/action_example'] = array(
  171. 'title' => 'Action Example',
  172. 'description' => 'Provides a basic information page.',
  173. 'page callback' => '_action_example_page',
  174. 'access callback' => TRUE,
  175. );
  176. return $items;
  177. }
  178. /**
  179. * A simple page to explain to the developer what to do.
  180. */
  181. function _action_example_page() {
  182. return t("The Action Example provides three example actions which can be configured on the <a href='@actions_url'>Actions configuration page</a> and assigned to triggers on the <a href='@triggers_url'>Triggers configuration page</a>.", array('@actions_url' => url('admin/config/system/actions'), '@triggers_url' => url('admin/structure/trigger/node')));
  183. }
  184. /**
  185. * Action function for action_example_basic_action.
  186. *
  187. * This action is not expecting any type of entity object, and can be used with
  188. * any trigger type or any event.
  189. *
  190. * @param object $entity
  191. * An optional entity object.
  192. * @param array $context
  193. * Array with parameters for this action: depends on the trigger.
  194. *
  195. * @see action_example_action_info()
  196. */
  197. function action_example_basic_action(&$entity, $context = array()) {
  198. // In this case we are ignoring the entity and the context. This case of
  199. // action is useful when your action does not depend on the context, and
  200. // the function must do something regardless the scope of the trigger.
  201. // Simply announces that the action was executed using a message.
  202. drupal_set_message(t('action_example_basic_action fired'));
  203. watchdog('action_example', 'action_example_basic_action fired.');
  204. }
  205. /**
  206. * Action function for action_example_unblock_user_action.
  207. *
  208. * This action is expecting an entity object user, node or comment. If none of
  209. * the above is provided (because it was not called from an user/node/comment
  210. * trigger event), then the action will be taken on the current logged in user.
  211. *
  212. * Unblock an user. This action can be fired from different trigger types:
  213. * - User trigger: this user will be unblocked.
  214. * - Node/Comment trigger: the author of the node or comment will be unblocked.
  215. * - Other: (including system or custom defined types), current user will be
  216. * unblocked. (Yes, this seems like an incomprehensible use-case.)
  217. *
  218. * @param object $entity
  219. * An optional user object (could be a user, or an author if context is
  220. * node or comment)
  221. * @param array $context
  222. * Array with parameters for this action: depends on the trigger. The context
  223. * is not used in this example.
  224. */
  225. function action_example_unblock_user_action(&$entity, $context = array()) {
  226. // First we check that entity is a user object. If this is the case, then this
  227. // is a user-type trigger.
  228. if (isset($entity->uid)) {
  229. $uid = $entity->uid;
  230. }
  231. elseif (isset($context['uid'])) {
  232. $uid = $context['uid'];
  233. }
  234. // If neither of those are valid, then block the current user.
  235. else {
  236. $uid = $GLOBALS['user']->uid;
  237. }
  238. $account = user_load($uid);
  239. $account = user_save($account, array('status' => 1));
  240. watchdog('action_example', 'Unblocked user %name.', array('%name' => $account->name));
  241. drupal_set_message(t('Unblocked user %name', array('%name' => $account->name)));
  242. }
  243. /**
  244. * Form function for action_example_node_sticky_action.
  245. *
  246. * Since we defined action_example_node_sticky_action as 'configurable' => TRUE,
  247. * this action requires a configuration form to create/configure the action.
  248. * In this circumstance, Drupal will attempt to call a function named by
  249. * combining the action name (action_example_node_sticky_action) and _form, in
  250. * this case yielding action_example_node_sticky_action_form.
  251. *
  252. * In Drupal, actions requiring creation and configuration are called 'advanced
  253. * actions', because they must be customized to define their functionality.
  254. *
  255. * The 'action_example_node_sticky_action' allows creating rules to promote and
  256. * set sticky content created by selected users on certain events. A form is
  257. * used to configure which user is affected by this action, and this form
  258. * includes the standard _validate and _submit hooks.
  259. */
  260. /**
  261. * Generates settings form for action_example_node_sticky_action().
  262. *
  263. * @param array $context
  264. * An array of options of this action (in case it is being edited)
  265. *
  266. * @return array
  267. * Settings form as Form API array.
  268. *
  269. * @see action_example_action_info()
  270. */
  271. function action_example_node_sticky_action_form($context) {
  272. /*
  273. * We return a configuration form to set the requirements that will
  274. * match this action before being executed. This is a regular Drupal form and
  275. * may include any type of information you want, but all the fields of the
  276. * form will be saved into the $context variable.
  277. *
  278. * In this case we are promoting all content types submitted by this user, but
  279. * it is possible to extend these conditions providing more options in the
  280. * settings form.
  281. */
  282. $form['author'] = array(
  283. '#title' => t('Author name'),
  284. '#type' => 'textfield',
  285. '#description' => t('Any content created, presaved or updated by this user will be promoted to front page and set as sticky.'),
  286. '#default_value' => isset($context['author']) ? $context['author'] : '',
  287. );
  288. // Verify user permissions and provide an easier way to fill this field.
  289. if (user_access('access user profiles')) {
  290. $form['author']['#autocomplete_path'] = 'user/autocomplete';
  291. }
  292. // No more options, return the form.
  293. return $form;
  294. }
  295. /**
  296. * Validates settings form for action_example_node_sticky_action().
  297. *
  298. * Verifies that user exists before continuing.
  299. */
  300. function action_example_node_sticky_action_validate($form, $form_state) {
  301. if (!$account = user_load_by_name($form_state['values']['author'])) {
  302. form_set_error('author', t('Please, provide a valid username'));
  303. }
  304. }
  305. /**
  306. * Submit handler for action_example_node_sticky_action.
  307. *
  308. * Returns an associative array of values which will be available in the
  309. * $context when an action is executed.
  310. */
  311. function action_example_node_sticky_action_submit($form, $form_state) {
  312. return array('author' => $form_state['values']['author']);
  313. }
  314. /**
  315. * Action function for action_example_node_sticky_action.
  316. *
  317. * Promote and set sticky flag. This is the special action that has been
  318. * customized using the configuration form, validated with the validation
  319. * function, and submitted with the submit function.
  320. *
  321. * @param object $node
  322. * A node object provided by the associated trigger.
  323. * @param array $context
  324. * Array with the following elements:
  325. * - 'author': username of the author's content this function will promote and
  326. * set as sticky.
  327. */
  328. function action_example_node_sticky_action($node, $context) {
  329. if (function_exists('dsm')) {
  330. dsm($node, 'action_example_node_sticky_action is firing. Here is the $node');
  331. dsm($context, 'action_example_node_sticky_action is firing. Here is the $context');
  332. }
  333. // Get the user configured for this special action.
  334. $account = user_load_by_name($context['author']);
  335. // Is the node created by this user? then promote and set as sticky.
  336. if ($account->uid == $node->uid) {
  337. $node->promote = NODE_PROMOTED;
  338. $node->sticky = NODE_STICKY;
  339. watchdog('action',
  340. 'Set @type %title to sticky and promoted by special action for user %username.',
  341. array(
  342. '@type' => node_type_get_name($node),
  343. '%title' => $node->title,
  344. '%username' => $account->name,
  345. )
  346. );
  347. drupal_set_message(
  348. t('Set @type %title to sticky and promoted by special action for user %username.',
  349. array(
  350. '@type' => node_type_get_name($node),
  351. '%title' => $node->title,
  352. '%username' => $account->name,
  353. )
  354. )
  355. );
  356. }
  357. }
  358. /**
  359. * @} End of "defgroup action_example".
  360. */