actions_loop_test.module 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Implements hook_trigger_info().
  4. */
  5. function actions_loop_test_trigger_info() {
  6. return array(
  7. 'actions_loop_test' => array(
  8. 'watchdog' => array(
  9. 'label' => t('When a message is logged'),
  10. ),
  11. ),
  12. );
  13. }
  14. /**
  15. * Implements hook_watchdog().
  16. */
  17. function actions_loop_test_watchdog(array $log_entry) {
  18. // If the triggering actions are not explicitly enabled, abort.
  19. if (empty($_GET['trigger_actions_on_watchdog'])) {
  20. return;
  21. }
  22. // Get all the action ids assigned to the trigger on the watchdog hook's
  23. // "run" event.
  24. $aids = trigger_get_assigned_actions('watchdog');
  25. // We can pass in any applicable information in $context. There isn't much in
  26. // this case, but we'll pass in the hook name as the bare minimum.
  27. $context = array(
  28. 'hook' => 'watchdog',
  29. );
  30. // Fire the actions on the associated object ($log_entry) and the context
  31. // variable.
  32. actions_do(array_keys($aids), $log_entry, $context);
  33. }
  34. /**
  35. * Implements hook_init().
  36. */
  37. function actions_loop_test_init() {
  38. if (!empty($_GET['trigger_actions_on_watchdog'])) {
  39. watchdog_skip_semaphore('actions_loop_test', 'Triggering action loop');
  40. }
  41. }
  42. /**
  43. * Implements hook_action_info().
  44. */
  45. function actions_loop_test_action_info() {
  46. return array(
  47. 'actions_loop_test_log' => array(
  48. 'label' => t('Write a message to the log.'),
  49. 'type' => 'system',
  50. 'configurable' => FALSE,
  51. 'triggers' => array('any'),
  52. ),
  53. );
  54. }
  55. /**
  56. * Write a message to the log.
  57. */
  58. function actions_loop_test_log() {
  59. $count = &drupal_static(__FUNCTION__, 0);
  60. $count++;
  61. watchdog_skip_semaphore('actions_loop_test', "Test log #$count");
  62. }
  63. /**
  64. * Replacement of the watchdog() function that eliminates the use of semaphores
  65. * so that we can test the abortion of an action loop.
  66. */
  67. function watchdog_skip_semaphore($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {
  68. global $user, $base_root;
  69. // Prepare the fields to be logged
  70. $log_entry = array(
  71. 'type' => $type,
  72. 'message' => $message,
  73. 'variables' => $variables,
  74. 'severity' => $severity,
  75. 'link' => $link,
  76. 'user' => $user,
  77. 'uid' => isset($user->uid) ? $user->uid : 0,
  78. 'request_uri' => $base_root . request_uri(),
  79. 'referer' => $_SERVER['HTTP_REFERER'],
  80. 'ip' => ip_address(),
  81. 'timestamp' => REQUEST_TIME,
  82. );
  83. // Call the logging hooks to log/process the message
  84. foreach (module_implements('watchdog') as $module) {
  85. module_invoke($module, 'watchdog', $log_entry);
  86. }
  87. }