events.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. switch ($type) {
  35. case 'comment':
  36. rules_invoke_event('comment_view--' . $entity->node_type, $entity, $view_mode);
  37. rules_invoke_event('comment_view', $entity, $view_mode);
  38. break;
  39. case 'node':
  40. rules_invoke_event('node_view--' . $entity->type, $entity, $view_mode);
  41. rules_invoke_event('node_view', $entity, $view_mode);
  42. break;
  43. case 'user':
  44. rules_invoke_event('user_view', $entity, $view_mode);
  45. break;
  46. }
  47. }
  48. /**
  49. * Implements hook_entity_presave().
  50. */
  51. function rules_entity_presave($entity, $type) {
  52. switch ($type) {
  53. case 'comment':
  54. rules_invoke_event('comment_presave--' . $entity->node_type, $entity);
  55. rules_invoke_event('comment_presave', $entity);
  56. break;
  57. case 'node':
  58. rules_invoke_event('node_presave--' . $entity->type, $entity);
  59. rules_invoke_event('node_presave', $entity);
  60. break;
  61. case 'taxonomy_term':
  62. rules_invoke_event('taxonomy_term_presave--' . $entity->vocabulary_machine_name, $entity);
  63. rules_invoke_event('taxonomy_term_presave', $entity);
  64. break;
  65. case 'taxonomy_vocabulary':
  66. case 'user':
  67. rules_invoke_event($type . '_presave', $entity);
  68. break;
  69. }
  70. }
  71. /**
  72. * Implements hook_entity_update().
  73. */
  74. function rules_entity_update($entity, $type) {
  75. switch ($type) {
  76. case 'comment':
  77. rules_invoke_event('comment_update--' . $entity->node_type, $entity);
  78. rules_invoke_event('comment_update', $entity);
  79. break;
  80. case 'node':
  81. rules_invoke_event('node_update--' . $entity->type, $entity);
  82. rules_invoke_event('node_update', $entity);
  83. break;
  84. case 'taxonomy_term':
  85. rules_invoke_event('taxonomy_term_update--' . $entity->vocabulary_machine_name, $entity);
  86. rules_invoke_event('taxonomy_term_update', $entity);
  87. break;
  88. case 'taxonomy_vocabulary':
  89. case 'user':
  90. rules_invoke_event($type . '_update', $entity);
  91. break;
  92. }
  93. }
  94. /**
  95. * Implements hook_entity_insert().
  96. */
  97. function rules_entity_insert($entity, $type) {
  98. switch ($type) {
  99. case 'comment':
  100. rules_invoke_event('comment_insert--' . $entity->node_type, $entity);
  101. rules_invoke_event('comment_insert', $entity);
  102. break;
  103. case 'node':
  104. rules_invoke_event('node_insert--' . $entity->type, $entity);
  105. rules_invoke_event('node_insert', $entity);
  106. break;
  107. case 'taxonomy_term':
  108. rules_invoke_event('taxonomy_term_insert--' . $entity->vocabulary_machine_name, $entity);
  109. rules_invoke_event('taxonomy_term_insert', $entity);
  110. break;
  111. case 'taxonomy_vocabulary':
  112. case 'user':
  113. rules_invoke_event($type . '_insert', $entity);
  114. break;
  115. }
  116. }
  117. /**
  118. * Implements hook_entity_delete().
  119. */
  120. function rules_entity_delete($entity, $type) {
  121. switch ($type) {
  122. case 'comment':
  123. rules_invoke_event('comment_delete--' . $entity->node_type, $entity);
  124. rules_invoke_event('comment_delete', $entity);
  125. break;
  126. case 'node':
  127. rules_invoke_event('node_delete--' . $entity->type, $entity);
  128. rules_invoke_event('node_delete', $entity);
  129. break;
  130. case 'taxonomy_term':
  131. rules_invoke_event('taxonomy_term_delete--' . $entity->vocabulary_machine_name, $entity);
  132. rules_invoke_event('taxonomy_term_delete', $entity);
  133. break;
  134. case 'taxonomy_vocabulary':
  135. case 'user':
  136. rules_invoke_event($type . '_delete', $entity);
  137. break;
  138. }
  139. }
  140. /**
  141. * Implements hook_user_login().
  142. */
  143. function rules_user_login(&$edit, $account) {
  144. rules_invoke_event('user_login', $account);
  145. }
  146. /**
  147. * Implements hook_user_logout().
  148. */
  149. function rules_user_logout($account) {
  150. rules_invoke_event('user_logout', $account);
  151. }
  152. /**
  153. * System events. Note that rules_init() is the main module file is used to
  154. * invoke the init event.
  155. */
  156. /**
  157. * Implements hook_cron().
  158. */
  159. function rules_cron() {
  160. rules_invoke_event('cron');
  161. }
  162. /**
  163. * Implements hook_watchdog().
  164. */
  165. function rules_watchdog($log_entry) {
  166. rules_invoke_event('watchdog', $log_entry);
  167. }
  168. /**
  169. * Getter callback for the log entry message property.
  170. */
  171. function rules_system_log_get_message($log_entry) {
  172. return t($log_entry['message'], (array)$log_entry['variables']);
  173. }
  174. /**
  175. * Gets all view modes of an entity for an entity_view event.
  176. */
  177. function rules_get_entity_view_modes($name, $var_info) {
  178. // Read the entity type from a special key out of the variable info.
  179. $entity_type = $var_info['options list entity type'];
  180. $info = entity_get_info($entity_type);
  181. foreach ($info['view modes'] as $mode => $mode_info) {
  182. $modes[$mode] = $mode_info['label'];
  183. }
  184. return $modes;
  185. }
  186. /**
  187. * @}
  188. */