events.inc 5.7 KB

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