-
Notifications
You must be signed in to change notification settings - Fork 2
/
references_integrity.module
250 lines (223 loc) · 8.66 KB
/
references_integrity.module
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
<?php
/**
* @file
* This module provides a method to enforce referential integrity rules for References.
*/
/**
* Referenced types.
*/
define('REFERENCES_INTEGRITY_REFERENCE_NODE', 'node_reference');
define('REFERENCES_INTEGRITY_REFERENCE_USER', 'user_reference');
define('REFERENCES_INTEGRITY_REFERENCE_ENTITY', 'entityreference');
/**
* Referential integrity behaviors.
*/
define('REFERENCES_INTEGRITY_BEHAVIOR_NONE', '');
define('REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_REFERENCE', 'remove reference');
define('REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_ENTITY', 'remove entity');
/**
* Implements hook_theme().
*/
function references_integrity_theme() {
return array(
'references_integrity_status_icon' => array(
'arguments' => array('status' => NULL, 'title' => NULL),
'file' => 'references_integrity.admin.inc',
),
);
}
/**
* Implements hook_perm().
*/
function references_integrity_permission() {
return array(
'administer references integrity' => array(
'title' => t('Administer References Integrity'),
'description' => t('Allow administration of several tasks related to references integrity such as orphan references.'),
),
);
}
/**
* Implements hook_menu().
*/
function references_integrity_menu() {
$items = array();
$items['admin/content/orphan-references/%references_integrity_entity_type/%references_integrity_bundle/%references_integrity_field_instance/%references_integrity_type'] = array(
'title' => 'Instance with orphan references',
'description' => 'Handle orphan references of a specific instance.',
'page callback' => 'drupal_get_form',
'page arguments' => array('references_integrity_orphan_references_check_field', 3, 4, 5, 6),
'access arguments' => array('administer references integrity'),
'load arguments' => array('%map'),
'file' => 'references_integrity.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 11,
);
$items['admin/content/orphan-references'] = array(
'title' => 'Orphan references',
'description' => 'Referential integrity report to monitor orphan records in node and user reference.',
'page callback' => 'references_integrity_orphan_references_check_all',
'access arguments' => array('administer references integrity'),
'file' => 'references_integrity.admin.inc',
'type' => MENU_LOCAL_TASK | MENU_NORMAL,
'weight' => 10,
);
// Tabs begin here.
$items['admin/content/orphan-references/all'] = array(
'title' => 'All references',
'type' => MENU_DEFAULT_LOCAL_TASK,
'page callback' => 'references_integrity_orphan_references_check_all',
'page arguments' => array('all'),
'access arguments' => array('administer references integrity'),
'file' => 'references_integrity.admin.inc',
'weight' => -10,
);
$items['admin/content/orphan-references/node'] = array(
'title' => 'Node references',
'type' => MENU_LOCAL_TASK,
'page callback' => 'references_integrity_orphan_references_check_all',
'page arguments' => array('node_reference'),
'access arguments' => array('administer references integrity'),
'file' => 'references_integrity.admin.inc',
'weight' => -10,
);
$items['admin/content/orphan-references/user'] = array(
'title' => 'User references',
'page callback' => 'references_integrity_orphan_references_check_all',
'page arguments' => array('user_reference'),
'access arguments' => array('administer references integrity'),
'file' => 'references_integrity.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/content/orphan-references/entity'] = array(
'title' => 'Entity references',
'page callback' => 'references_integrity_orphan_references_check_all',
'page arguments' => array('entityreference'),
'access arguments' => array('administer references integrity'),
'file' => 'references_integrity.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Menu loader for %references_integrity_entity_type.
*/
function references_integrity_entity_type_load($entity_type, $map) {
$entity_info = entity_get_info($entity_type);
return empty($entity_info) ? FALSE : ($entity_info + array('entity_type' => $entity_type));
}
/**
* Menu loader for %references_integrity_bundle.
*/
function references_integrity_bundle_load($bundle_name, $map) {
return empty($map[3]['bundles'][$bundle_name]) ? FALSE : ($map[3]['bundles'][$bundle_name] + array('bundle' => $bundle_name));
}
/**
* Menu loader for %references_integrity_field_instance.
*/
function references_integrity_field_instance_load($field, $map) {
$instance_info = field_info_instance($map[3]['entity_type'], $field, $map[4]['bundle']);
return empty($instance_info) ? FALSE : ($instance_info + array('field_info' => field_info_field($field)));
}
/**
* Menu loader for %references_integrity_type.
*/
function references_integrity_type_load($type, $map) {
return in_array($type, references_integrity_references()) ? $type : FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Add the reference integrity settings to the field settings.
*
*/
function references_integrity_form_field_ui_field_settings_form_alter(&$form, $form_state, $form_id) {
if (!in_array($form['field']['type']['#value'], references_integrity_references())) {
return;
}
module_load_include('inc', 'references_integrity');
module_load_include('inc', 'references_integrity', 'references_integrity.admin');
_references_integrity_field_settings_form_alter($form, $form_state, $form_id);
}
/**
* Implementation of hook_form_FORM_ID_alter().
*
* Provide a warning that referencing entities or entity references will also
* be removed.
*/
function references_integrity_form_node_delete_confirm_alter(&$form, $form_state) {
module_load_include('inc', 'references_integrity');
$items = references_integrity_apply(REFERENCES_INTEGRITY_REFERENCE_NODE, $form['#node'], FALSE);
$items += references_integrity_apply(REFERENCES_INTEGRITY_REFERENCE_ENTITY, $form['#node'], FALSE);
if (count($items)){
$message = '<div class="references_integrity_delete_message">';
foreach($items as $item) {
$message .= references_integrity_get_delete_message($item['field_name']);
$link = l(t('View referencing ' . $item['entity_type'] . '.'), 'node/' . $item['entity_id']);
$message .= ' ' . $link . '<br />';
}
$message .= '</div>';
$form['description']['#markup'] = $message . $form['description']['#markup'];
}
}
/**
* Implements hook_node_delete().
*/
function references_integrity_node_delete($node) {
module_load_include('inc', 'references_integrity');
references_integrity_apply(REFERENCES_INTEGRITY_REFERENCE_NODE, $node, TRUE);
}
/**
* Implements hook_user_delete().
*/
function references_integrity_user_delete($account) {
module_load_include('inc', 'references_integrity');
references_integrity_apply(REFERENCES_INTEGRITY_REFERENCE_USER, $account, TRUE);
}
/**
* Implements hook_entity_delete().
*/
function references_integrity_entity_delete($entity, $type) {
module_load_include('inc', 'references_integrity');
references_integrity_apply(REFERENCES_INTEGRITY_REFERENCE_ENTITY, $entity, TRUE);
}
/**
* implements hook_cron().
*/
function references_integrity_cron() {
// Consume the queue.
module_load_include('cron.inc', 'references_integrity');
references_integrity_fetch();
}
/**
* Return referenceable entities.
*
* @return
* Array with the referenceable field types.
*/
function references_integrity_references() {
static $references = array(REFERENCES_INTEGRITY_REFERENCE_NODE, REFERENCES_INTEGRITY_REFERENCE_USER, REFERENCES_INTEGRITY_REFERENCE_ENTITY);
return $references;
}
/**
* Get the ID key based on reference type.
*
* @param $reference_type
* String with the reference type. Can be "node_reference" or "user_reference" or "entityreference".
*
* @return
* String: "nid" or "uid" or "target_id".
*/
function references_integrity_get_id_key($reference_type) {
static $keys = array(REFERENCES_INTEGRITY_REFERENCE_NODE => 'nid', REFERENCES_INTEGRITY_REFERENCE_USER => 'uid', REFERENCES_INTEGRITY_REFERENCE_ENTITY => 'target_id');
return in_array($reference_type, array_keys($keys)) ? $keys[$reference_type] : FALSE;
}
/**
* Additional submit handler for reference fields.
*/
function _references_integrity_field_settings_form_alter_submit($form, &$form_state) {
$field =& $form_state['values']['field'];
variable_set('references_integrity_behavior_' . $field['field_name'], $field['references_integrity_behavior']);
// Unset the message if the user selected the empty setting. Otherwise, save it.
variable_set('references_integrity_delete_message_' . $field['field_name'], $field['references_integrity_behavior'] == '' ? '' : $field['references_integrity_delete_message']);
}