-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcer.module
More file actions
318 lines (277 loc) · 8.72 KB
/
cer.module
File metadata and controls
318 lines (277 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* @file
* Main module file.
*/
/**
* Implements hook_menu().
*/
function cer_menu() {
$items = array();
$items['admin/config/system/cer'] = array(
'title' => 'Corresponding entity references',
'page callback' => 'drupal_get_form',
'page arguments' => array('cer_settings_form'),
'access arguments' => array('administer cer settings'),
'file' => 'cer.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/system/cer/references'] = array(
'title' => 'References',
'page callback' => 'drupal_get_form',
'page arguments' => array('cer_settings_form'),
'access arguments' => array('administer cer settings'),
'file' => 'cer.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/system/cer/update'] = array(
'title' => 'Update existing entities',
'page callback' => 'drupal_get_form',
'page arguments' => array('cer_update_form'),
'access arguments' => array('administer cer settings'),
'file' => 'cer.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function cer_permission() {
return array(
'administer cer settings' => array(
'title' => t('Administer corresponding entity reference settings'),
)
);
}
/**
* Implements hook_help().
*/
function cer_help($path, $arg) {
$output = '';
if ($path == 'admin/config/system/cer') {
$output .= t('Check which entity references should listen to each other. When checking a check box a reference on entity type A to entity B will automatically update the entity reference field on entity B adding an entry which points to entity A.');
}
elseif ($path == 'admin/config/system/cer/update') {
$output .= t('This will update all the existing entities for the selected content types so that their entity reference fields are in sync.');
$output .= '<br />';
$output .= t('This process may take a long time depending on the number of entities you are updating.');
$output .= '<br /><br />';
$output .= t('When the process is finished you will see a count of the number of entities that were updated.');
}
return $output;
}
/**
* Implements hook_field_delete_instance().
*/
function cer_field_delete_instance($instance) {
foreach (cer_preset_load_enabled() as $row) {
$keys = explode('*', $row->entity_types_content_fields);
if (($keys[0] == $instance['entity_type'] && $keys[1] == $instance['bundle'] && $keys[2] == $instance['field_name']) || ($keys[3] == $instance['entity_type'] && $keys[4] == $instance['bundle'] && $keys[5] == $instance['field_name'])) {
cer_preset_delete($row->entity_types_content_fields);
}
}
}
/**
* Implements hook_field_delete_field().
*/
function cer_field_delete_field($field) {
foreach (cer_preset_load_enabled() as $row) {
$keys = explode('*', $row->entity_types_content_fields);
if ($keys[2] == $field['field_name'] || $keys[5] == $field['field_name']) {
cer_preset_delete($row->entity_types_content_fields);
}
}
}
/**
* Implements hook_theme().
*/
function cer_theme() {
return array(
'cer_label' => array(
'variables' => array('key' => ''),
),
);
}
function theme_cer_label($variables) {
$key = explode(' ', $variables['key']);
$local = field_info_instance($key[0], $key[2], $key[1]);
$remote = field_info_instance($key[3], $key[5], $key[4]);
$message = 'Correspond <span title="!local_field">%local_label</span> on !local_entity(s) of type %local_bundle with <span title="!remote_field">%remote_label</span> on !remote_entity(s) of type %remote_bundle.';
$variables = array(
'%local_label' => $local['label'],
'!local_field' => $local['field_name'],
'!local_entity' => $local['entity_type'],
'%local_bundle' => $local['bundle'],
'%remote_label' => $remote['label'],
'!remote_field' => $remote['field_name'],
'!remote_entity' => $remote['entity_type'],
'%remote_bundle' => $remote['bundle'],
);
return t($message, $variables);
}
/**
* Implements hook_entity_insert().
*/
function cer_entity_insert($entity, $type) {
cer_processing_entity('insert', $entity, $type);
}
/**
* Implements hook_entity_update().
*/
function cer_entity_update($entity, $type) {
cer_processing_entity('update', $entity, $type);
}
/**
* Implements hook_entity_delete().
*/
function cer_entity_delete($entity, $type) {
cer_processing_entity('delete', $entity, $type);
}
/**
* Load enabled CER presets.
*/
function cer_preset_load_enabled() {
ctools_include('export');
return ctools_export_load_object('cer', 'conditions', array('enabled' => 1));
}
/**
* Return CER preset by key.
*/
function cer_preset_load($key) {
ctools_include('export');
return ctools_export_crud_load('cer', $key);
}
/**
* Return 1 if CER preset specified by given key is enabled.
*/
function cer_preset_enabled($key) {
$preset = cer_preset_load($key);
return empty($preset) ? 0 : $preset->enabled;
}
/**
* Deletes or disables a given CER preset.
*/
function cer_preset_delete($key) {
ctools_include('export');
ctools_export_crud_delete('cer', $key);
ctools_export_crud_disable('cer', $key);
ctools_export_load_object_reset('cer');
}
/**
* Process a entity's corresponding entity references.
*
* @param string $op
* The operation being performed on the entity (insert, update, or delete).
*
* @param object $entity
* The entity or the entity's id.
*
* @param string $entity_type
* The entity type.
*
* @param array $context
* Either the Batch API context (since this is the callback function used
* during bulk update) or NULL if we're not in a batch job.
*/
function cer_processing_entity($op, $entity, $entity_type, &$context = NULL) {
// Load the entity if we're given an ID rather than an entity.
if (!is_object($entity)) {
$entity = entity_load($entity_type, array($entity));
$entity = reset($entity);
}
// If the entity is of the wrong type, entity_extract_IDs() will throw
// EntityMalformedException and rightfully bail out here.
list (, , $bundle) = entity_extract_IDs($entity_type, $entity);
$result = cer_preset_load_enabled();
foreach ($result as $row) {
$keys = explode('*', $row->entity_types_content_fields);
if ($keys[0] == $entity_type && $keys[1] == $bundle) {
try {
$handler = new CerHandler($row->entity_types_content_fields, $entity);
call_user_func(array($handler, $op));
}
catch (CerException $e) {
if (isset($context)) {
$context['results']['errors'][] = $e;
}
else {
throw $e;
}
}
}
if ($keys[3] == $entity_type && $keys[4] == $bundle) {
$preset = implode('*', array($keys[3], $keys[4], $keys[5], $keys[0], $keys[1], $keys[2]));
try {
$handler = new CerHandler($preset, $entity);
call_user_func(array($handler, $op));
}
catch (CerException $e) {
if (isset($context)) {
$context['results']['errors'][] = $e;
}
else {
throw $e;
}
}
}
}
if (isset($context)) {
$context['results']['count']++;
}
}
/**
* Batch 'finished' callback.
*/
function cer_batch_update_existing_finished($success, $results, $operations) {
if ($success) {
$message = format_plural($results['count'], '1 entity processed.', '@count entities processed.');
if (isset($results['errors'])) {
$type = 'warning';
foreach ($results['errors'] as $e) {
drupal_set_message($e->getMessage(), 'error');
}
}
else {
$type = 'status';
}
drupal_set_message($message, $type);
}
else {
// An error occurred. $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments:' . print_r($error_operation[0], TRUE);
drupal_set_message($message, 'error');
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function cer_ctools_plugin_api($owner, $api) {
if ($owner == 'cer' && $api == 'default_cer_presets') {
return array('version' => 1);
}
}
/**
* Update field data.
*
* @param $node the referenced node to be updated.
*/
function _cer_update($entity_type, $entity) {
$entity->original = isset($entity->original) ? $entity->original : NULL;
$extract_ids = entity_extract_IDs($entity_type, $entity);
$id = array_shift($extract_ids);
field_attach_presave($entity_type, $entity);
field_attach_update($entity_type, $entity);
// Issue #2212499.
if ($entity_type == 'node') {
$entity->changed = time();
db_update('node')
->fields(array(
'changed' => $entity->changed,
))
->condition('nid', $id)
->execute();
}
entity_get_controller($entity_type)->resetCache(array($id));
}