events.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file Invokes events on behalf core modules. Usually this should be
  4. * directly in the module providing rules integration instead.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * Gets an unchanged entity that doesn't contain any recent changes. This
  11. * handler assumes the name of the variable for the changed entity is the same
  12. * as for the unchanged entity but without the trailing "_unchanged"; e.g., for
  13. * the "node_unchanged" variable the handler assumes there is a "node" variable.
  14. */
  15. function rules_events_entity_unchanged($arguments, $name, $info) {
  16. // Cut of the trailing _unchanged.
  17. $var_name = substr($name, 0, -10);
  18. $entity = $arguments[$var_name];
  19. if (isset($entity->original)) {
  20. return $entity->original;
  21. }
  22. }
  23. /**
  24. * Generic entity events, used for core-entities for which we provide Rules
  25. * integration only.
  26. * We are implementing the generic-entity hooks instead of the entity-type
  27. * specific hooks to ensure we come last. See http://drupal.org/node/1211946
  28. * for details.
  29. */
  30. /**
  31. * Implements hook_entity_view().
  32. */
  33. function rules_entity_view($entity, $type, $view_mode, $langcode) {
  34. $entity_types = array(
  35. 'comment' => TRUE,
  36. 'node' => TRUE,
  37. 'user' => TRUE,
  38. );
  39. if (isset($entity_types[$type])) {
  40. rules_invoke_event($type . '_view', $entity, $view_mode);
  41. }
  42. }
  43. /**
  44. * Implements hook_entity_presave().
  45. */
  46. function rules_entity_presave($entity, $type) {
  47. $entity_types = array(
  48. 'comment' => TRUE,
  49. 'node' => TRUE,
  50. 'taxonomy_term' => TRUE,
  51. 'taxonomy_vocabulary' => TRUE,
  52. 'user' => TRUE,
  53. );
  54. if (isset($entity_types[$type])) {
  55. rules_invoke_event($type . '_presave', $entity);
  56. }
  57. }
  58. /**
  59. * Implements hook_entity_update().
  60. */
  61. function rules_entity_update($entity, $type) {
  62. $entity_types = array(
  63. 'comment' => TRUE,
  64. 'node' => TRUE,
  65. 'taxonomy_term' => TRUE,
  66. 'taxonomy_vocabulary' => TRUE,
  67. 'user' => TRUE,
  68. );
  69. if (isset($entity_types[$type])) {
  70. rules_invoke_event($type . '_update', $entity);
  71. }
  72. }
  73. /**
  74. * Implements hook_entity_insert().
  75. */
  76. function rules_entity_insert($entity, $type) {
  77. $entity_types = array(
  78. 'comment' => TRUE,
  79. 'node' => TRUE,
  80. 'taxonomy_term' => TRUE,
  81. 'taxonomy_vocabulary' => TRUE,
  82. 'user' => TRUE,
  83. );
  84. if (isset($entity_types[$type])) {
  85. rules_invoke_event($type . '_insert', $entity);
  86. }
  87. }
  88. /**
  89. * Implements hook_entity_delete().
  90. */
  91. function rules_entity_delete($entity, $type) {
  92. $entity_types = array(
  93. 'comment' => TRUE,
  94. 'node' => TRUE,
  95. 'taxonomy_term' => TRUE,
  96. 'taxonomy_vocabulary' => TRUE,
  97. 'user' => TRUE,
  98. );
  99. if (isset($entity_types[$type])) {
  100. rules_invoke_event($type . '_delete', $entity);
  101. }
  102. }
  103. /**
  104. * Implements hook_user_login().
  105. */
  106. function rules_user_login(&$edit, $account) {
  107. rules_invoke_event('user_login', $account);
  108. }
  109. /**
  110. * Implements hook_user_logout().
  111. */
  112. function rules_user_logout($account) {
  113. rules_invoke_event('user_logout', $account);
  114. }
  115. /**
  116. * System events. Note that rules_init() is the main module file is used to
  117. * invoke the init event.
  118. */
  119. /**
  120. * Implements hook_cron().
  121. */
  122. function rules_cron() {
  123. rules_invoke_event('cron');
  124. }
  125. /**
  126. * Implements hook_watchdog().
  127. */
  128. function rules_watchdog($log_entry) {
  129. rules_invoke_event('watchdog', $log_entry);
  130. }
  131. /**
  132. * Getter callback for the log entry message property.
  133. */
  134. function rules_system_log_get_message($log_entry) {
  135. return t($log_entry['message'], (array)$log_entry['variables']);
  136. }
  137. /**
  138. * Gets all view modes of an entity for an entity_view event.
  139. */
  140. function rules_get_entity_view_modes($name, $var_info) {
  141. // Read the entity type from a special key out of the variable info.
  142. $entity_type = $var_info['options list entity type'];
  143. $info = entity_get_info($entity_type);
  144. foreach ($info['view modes'] as $mode => $mode_info) {
  145. $modes[$mode] = $mode_info['label'];
  146. }
  147. return $modes;
  148. }
  149. /**
  150. * @}
  151. */