-
Notifications
You must be signed in to change notification settings - Fork 0
/
entityreference_add_child_to_parent.module
275 lines (240 loc) · 10.5 KB
/
entityreference_add_child_to_parent.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
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
<?php
/**
* @file
* Entity Reference add child to parent.
*/
/**
* Implements hook_form_FORMID_alter().
*/
function entityreference_add_child_to_parent_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
if ($form['#field']['type'] != 'entityreference') {
return;
}
$form['instance']['add_child_to_parent_link'] = array(
'#type' => 'checkbox',
'#title' => 'Provide a pseudofield link to add the referenced entity and reference parent entity',
'#description' => 'Check this option if you want a pseudo field to appear in the display settings of this entity that allows to create a referenced entity that will automatically be referenced by this (parent) entity. If this option is set exactly one bundle must be specified in the field settings.',
'#default_value' => isset($form['#instance']['add_child_to_parent_link']) ? $form['#instance']['add_child_to_parent_link'] : FALSE,
'#weight' => -9,
);
$form['#validate'][] = '_entityreference_add_child_to_parent_field_settings_form_validate';
$form['#submit'][] = '_entityreference_add_child_to_parent_field_settings_form_submit';
return $form;
}
/**
* Extra validate function for field_ui_field_edit_form.
*
* Ensure that exactly one bundle is set.
*/
function _entityreference_add_child_to_parent_field_settings_form_validate($form, &$form_state) {
if ($form_state['values']['instance']['add_child_to_parent_link'] !== 1) {
// Only validate if the add_child_to_parent_link is set.
return;
}
$bundles = $form_state['values']['field']['settings']['handler_settings']['target_bundles'];
if (count($bundles) != 1) {
form_set_error('add_child_to_parent_link', t('A create entity link needs exactly one bundle to be specified.'));
}
}
/**
* Extra submit function for field_ui_field_edit_form.
*
* Populate the entityreference_add_child_to_parent_entity_types variable.
*/
function _entityreference_add_child_to_parent_field_settings_form_submit($form, &$form_state) {
$entity = $form_state['values']['instance']['entity_type'];
$bundle = $form_state['values']['instance']['bundle'];
$field = $form_state['values']['instance']['field_name'];
$add_child_to_parent_link = $form_state['values']['instance']['add_child_to_parent_link'];
$mymodule_types = variable_get('entityreference_add_child_to_parent_entity_types', array());
// Decide whether to add the setting to the variable
// entityreference_add_child_to_parent_entity_types.
if (isset($mymodule_types[$entity][$bundle]) && in_array($field, $mymodule_types[$entity][$bundle])) {
if ($add_child_to_parent_link !== 1) {
// Is in variable but not in form: Remove it and flush cache.
$mymodule_types[$entity][$bundle] = array_diff($mymodule_types[$entity][$bundle], array($field));
// Cleanup array if necessary.
$mymodule_types[$entity] = array_filter($mymodule_types[$entity]);
$mymodule_types = array_filter($mymodule_types);
variable_set('entityreference_add_child_to_parent_entity_types', $mymodule_types);
}
}
else {
if ($add_child_to_parent_link === 1) {
// Is not in variable but is in form: Add it and flush cache.
$mymodule_types[$entity][$bundle][] = $field;
variable_set('entityreference_add_child_to_parent_entity_types', $mymodule_types);
}
}
// dpm($mymodule_types, '$mymodule_types');
}
/**
* Implements hook_field_extra_fields().
*/
function entityreference_add_child_to_parent_field_extra_fields() {
$extra = array();
$mymodule_types = variable_get('entityreference_add_child_to_parent_entity_types', array());
$add_link = array(
'label' => t('Add entity link'),
'description' => t("A link to add an entity and populate the parent's reference field."),
'weight' => -115,
);
foreach ($mymodule_types as $entity => $bundles) {
foreach ($bundles as $bundle => $fields) {
foreach ($fields as $field) {
$extra[$entity][$bundle]['display']['entityreference_add_child_link_' . $field] = $add_link;
$extra[$entity][$bundle]['display']['entityreference_add_child_link_' . $field]['label'] .= ' for ' . $field;
}
}
}
return $extra;
}
/**
* Implements hook_entity_view().
*
* Generates markup for the pseudofields.
*/
function entityreference_add_child_to_parent_entity_view($entity, $type, $view_mode, $langcode) {
$entity_type = $entity->content['#entity_type'];
$entity_bundle = $entity->content['#bundle'];
$title = entity_label($entity_type, $entity);
$extrafields = field_extra_fields_get_display($entity_type, $entity_bundle, $view_mode);
foreach ($extrafields as $extrafield_name => $extrafield) {
if (strpos($extrafield_name, 'entityreference_add_child_link') !== FALSE) {
if ($extrafield['visible']) {
$weight = $extrafield['weight'];
// Get field name.
$pos = strlen('entityreference_add_child_link');
$field_name = substr($extrafield_name, $pos + 1);
$field_info = field_info_field($field_name);
$field_entity = $field_info['settings']['target_type'];
$field_bundle = reset($field_info['settings']['handler_settings']['target_bundles']);
$entity_info = entity_get_info($field_entity);
$bundle_label = $entity_info['bundles'][$field_bundle]['label'];
$entity_label = $entity_info['label'];
// Get path. Every entity type needs special handling as there is no API
// that returns the entitie's add entity path.
// Currently it only supports node and ECK entities.
if ($field_entity == 'node') {
$path = 'node/add/' . str_replace('_', '-', $field_bundle);
}
// ECK entities.
elseif (isset($entity_info['bundles'][$field_bundle]['crud']['add']['path'])) {
$path = $entity_info['bundles'][$field_bundle]['crud']['add']['path'];
}
else {
$path = '';
// @todo for other entity types.
}
// Access control.
if (drupal_valid_path($path)) {
$entity_ids = entity_extract_ids($entity_type, $entity);
// Prepare link.
$query = array(
'type' => $entity_type,
'bundle' => $entity_bundle,
'eid' => $entity_ids[0],
'field' => $field_name,
);
$query += drupal_get_destination();
$add_link = l(t('Add @bundle_label for @title', array(
'@bundle_label' => $bundle_label,
'@title' => $title,
)),
$path, array(
'attributes' => array(),
'query' => $query,
));
$entity->content[$extrafield_name] = array(
'#markup' => $add_link,
'#weight' => $weight,
);
}
}
}
}
}
/**
* Implements hook_entity_insert().
*
* Adds the reference to the parent entity and saves it.
*/
function entityreference_add_child_to_parent_entity_insert($entity, $type) {
$params = drupal_get_query_parameters();
$type = (isset($params['type'])) ? check_plain($params['type']) : '';
$bundle = (isset($params['bundle'])) ? check_plain($params['bundle']) : '';
$eid = (isset($params['eid'])) ? check_plain($params['eid']) : '';
$field = (isset($params['field'])) ? check_plain($params['field']) : '';
if (empty($type) || empty($eid) || empty($field)) {
return;
}
$entity_ids = entity_extract_ids($type, $entity);
$entity_id = $entity_ids[0];
// Check that the field can be found in the variable:
// entityreference_add_child_to_parent_entity_types. If not provide an error
// message and do not reference this entity in the parent entity.
$mymodule_types = variable_get('entityreference_add_child_to_parent_entity_types', array());
if (!in_array($field, $mymodule_types[$type][$bundle])) {
drupal_set_message(t('The field instance settings for field %field in bundle %bundle for entity %entity do not allow referencing parent entity. The newly created entity is not referenced by its parent.', array(
'%field' => $field,
'%bundle' => $bundle,
'%entity' => $type,
)), 'error');
return;
}
// Check that the created entity is of the same type and bundle as defined in
// the parents entity referenced field allowed type.
// @todo check entity type. There is no easy way given an entity
// to find its type.
$original_enity_bundle = $entity->type;
$field_info = field_info_field($field);
$field_type = $field_info['settings']['target_type'];
$field_bundle = reset($field_info['settings']['handler_settings']['target_bundles']);
if ($original_enity_bundle != $field_bundle) {
drupal_set_message(t("The entitie's bundle does not match the referenced field's bundle. The newly created entity is not referenced by its parent."), 'error');
return;
}
if (!empty($type) && !empty($eid) && !empty($field)) {
if (!is_numeric($eid)) {
drupal_set_message(t('Invalid Entity id. The newly created entity is not referenced by its parent.'), 'error');
return;
}
$entity_wrapper = entity_metadata_wrapper($type, $eid);
if ($entity_wrapper->value()) {
// dpm('have identifier');
if (isset($entity_wrapper->{$field}) && $entity_wrapper->{$field}->validate(array($entity_id))) {
// dpm('does validate');
$entity_wrapper->{$field}[] = $entity_id;
$entity_wrapper->save();
}
}
else {
drupal_set_message(t('Invalid Entity id. The newly created entity is not referenced by its parent.'), 'error');
return;
}
}
// throw new Exception("Custom Exception to stop code execution");
}
/**
* Implements hook_field_delete_instance().
*
* Removes the entityreference_add_child_to_parent value from the
* entityreference_add_child_to_parent_entity_types variable.
*/
function entityreference_add_child_to_parent_field_delete_instance($instance) {
// Only act on entity reference fields.
$field = field_read_field($instance['field_name']);
if ($field['type'] != 'entityreference') {
return;
}
$entity_type = $instance['entity_type'];
$bundle = $instance['bundle'];
$field_name = $instance['field_name'];
$mymodule_types = variable_get('entityreference_add_child_to_parent_entity_types', array());
if (isset($mymodule_types[$entity_type][$bundle]) && in_array($field_name, $mymodule_types[$entity_type][$bundle])) {
$mymodule_types[$entity_type][$bundle] = array_diff($mymodule_types[$entity_type][$bundle], array($field_name));
$mymodule_types[$entity_type] = array_filter($mymodule_types[$entity_type]);
$mymodule_types = array_filter($mymodule_types);
variable_set('entityreference_add_child_to_parent_entity_types', $mymodule_types);
}
}