Projet

Général

Profil

Paste
Télécharger (5,69 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / events.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Invokes events on behalf core modules.
6
 *
7
 * For non-core modules, the code to invoke events should be found in the
8
 * module providing rules integration.
9
 *
10
 * @addtogroup rules
11
 *
12
 * @{
13
 */
14

    
15
/**
16
 * Gets an unchanged entity that doesn't contain any recent changes.
17
 *
18
 * This handler assumes the name of the variable for the changed entity is the
19
 * same as for the unchanged entity but without the trailing "_unchanged"; e.g.,
20
 * for the "node_unchanged" variable the handler assumes there is a "node"
21
 * variable.
22
 */
23
function rules_events_entity_unchanged($arguments, $name, $info) {
24
  // Cut of the trailing _unchanged.
25
  $var_name = substr($name, 0, -10);
26
  $entity = $arguments[$var_name];
27
  if (isset($entity->original)) {
28
    return $entity->original;
29
  }
30
}
31

    
32
/*
33
 * Generic entity events, used for core-entities for which we provide Rules
34
 * integration only. We are implementing the generic-entity hooks instead of
35
 * the entity-type specific hooks to ensure we come last.
36
 * See https://www.drupal.org/node/1211946 for details.
37
 */
38

    
39
/**
40
 * Implements hook_entity_view().
41
 */
42
function rules_entity_view($entity, $type, $view_mode, $langcode) {
43
  switch ($type) {
44
    case 'comment':
45
      rules_invoke_event('comment_view--' . $entity->node_type, $entity, $view_mode);
46
      rules_invoke_event('comment_view', $entity, $view_mode);
47
      break;
48

    
49
    case 'node':
50
      rules_invoke_event('node_view--' . $entity->type, $entity, $view_mode);
51
      rules_invoke_event('node_view', $entity, $view_mode);
52
      break;
53

    
54
    case 'user':
55
      rules_invoke_event('user_view', $entity, $view_mode);
56
      break;
57
  }
58
}
59

    
60
/**
61
 * Implements hook_entity_presave().
62
 */
63
function rules_entity_presave($entity, $type) {
64
  switch ($type) {
65
    case 'comment':
66
      rules_invoke_event('comment_presave--' . $entity->node_type, $entity);
67
      rules_invoke_event('comment_presave', $entity);
68
      break;
69

    
70
    case 'node':
71
      rules_invoke_event('node_presave--' . $entity->type, $entity);
72
      rules_invoke_event('node_presave', $entity);
73
      break;
74

    
75
    case 'taxonomy_term':
76
      rules_invoke_event('taxonomy_term_presave--' . $entity->vocabulary_machine_name, $entity);
77
      rules_invoke_event('taxonomy_term_presave', $entity);
78
      break;
79

    
80
    case 'taxonomy_vocabulary':
81
    case 'user':
82
      rules_invoke_event($type . '_presave', $entity);
83
      break;
84
  }
85
}
86

    
87
/**
88
 * Implements hook_entity_update().
89
 */
90
function rules_entity_update($entity, $type) {
91
  switch ($type) {
92
    case 'comment':
93
      rules_invoke_event('comment_update--' . $entity->node_type, $entity);
94
      rules_invoke_event('comment_update', $entity);
95
      break;
96

    
97
    case 'node':
98
      rules_invoke_event('node_update--' . $entity->type, $entity);
99
      rules_invoke_event('node_update', $entity);
100
      break;
101

    
102
    case 'taxonomy_term':
103
      rules_invoke_event('taxonomy_term_update--' . $entity->vocabulary_machine_name, $entity);
104
      rules_invoke_event('taxonomy_term_update', $entity);
105
      break;
106

    
107
    case 'taxonomy_vocabulary':
108
    case 'user':
109
      rules_invoke_event($type . '_update', $entity);
110
      break;
111
  }
112
}
113

    
114
/**
115
 * Implements hook_entity_insert().
116
 */
117
function rules_entity_insert($entity, $type) {
118
  switch ($type) {
119
    case 'comment':
120
      rules_invoke_event('comment_insert--' . $entity->node_type, $entity);
121
      rules_invoke_event('comment_insert', $entity);
122
      break;
123

    
124
    case 'node':
125
      rules_invoke_event('node_insert--' . $entity->type, $entity);
126
      rules_invoke_event('node_insert', $entity);
127
      break;
128

    
129
    case 'taxonomy_term':
130
      rules_invoke_event('taxonomy_term_insert--' . $entity->vocabulary_machine_name, $entity);
131
      rules_invoke_event('taxonomy_term_insert', $entity);
132
      break;
133

    
134
    case 'taxonomy_vocabulary':
135
    case 'user':
136
      rules_invoke_event($type . '_insert', $entity);
137
      break;
138
  }
139
}
140

    
141
/**
142
 * Implements hook_entity_delete().
143
 */
144
function rules_entity_delete($entity, $type) {
145
  switch ($type) {
146
    case 'comment':
147
      rules_invoke_event('comment_delete--' . $entity->node_type, $entity);
148
      rules_invoke_event('comment_delete', $entity);
149
      break;
150

    
151
    case 'node':
152
      rules_invoke_event('node_delete--' . $entity->type, $entity);
153
      rules_invoke_event('node_delete', $entity);
154
      break;
155

    
156
    case 'taxonomy_term':
157
      rules_invoke_event('taxonomy_term_delete--' . $entity->vocabulary_machine_name, $entity);
158
      rules_invoke_event('taxonomy_term_delete', $entity);
159
      break;
160

    
161
    case 'taxonomy_vocabulary':
162
    case 'user':
163
      rules_invoke_event($type . '_delete', $entity);
164
      break;
165
  }
166
}
167

    
168
/**
169
 * Implements hook_user_login().
170
 */
171
function rules_user_login(&$edit, $account) {
172
  rules_invoke_event('user_login', $account);
173
}
174

    
175
/**
176
 * Implements hook_user_logout().
177
 */
178
function rules_user_logout($account) {
179
  rules_invoke_event('user_logout', $account);
180
}
181

    
182
/*
183
 * System events.
184
 *
185
 * Note that rules_init() is the main module file is used to
186
 * invoke the init event.
187
 */
188

    
189
/**
190
 * Implements hook_cron().
191
 */
192
function rules_cron() {
193
  rules_invoke_event('cron');
194
}
195

    
196
/**
197
 * Implements hook_watchdog().
198
 */
199
function rules_watchdog($log_entry) {
200
  rules_invoke_event('watchdog', $log_entry);
201
}
202

    
203
/**
204
 * Getter callback for the log entry message property.
205
 */
206
function rules_system_log_get_message($log_entry) {
207
  return t($log_entry['message'], (array) $log_entry['variables']);
208
}
209

    
210
/**
211
 * Gets all view modes of an entity for an entity_view event.
212
 */
213
function rules_get_entity_view_modes($name, $var_info) {
214
  // Read the entity type from a special key out of the variable info.
215
  $entity_type = $var_info['options list entity type'];
216
  $info = entity_get_info($entity_type);
217
  foreach ($info['view modes'] as $mode => $mode_info) {
218
    $modes[$mode] = $mode_info['label'];
219
  }
220
  return $modes;
221
}
222

    
223
/**
224
 * @}
225
 */