Mercurial > hg > rr-repo
comparison sites/all/modules/bundle_inherit/bundle_inherit.module @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:b28be78d8160 | 4:ce11bbd8f642 |
---|---|
1 <?php | |
2 /** | |
3 * @file | |
4 * Bundle Inherit module. | |
5 */ | |
6 | |
7 /** | |
8 * Perform necesary inherit operations. | |
9 */ | |
10 function bundle_inherit_perform($entity_type, $bundle, $bundle_parent, $strict = TRUE) { | |
11 // Get fields from parent bundle. | |
12 $instances = field_info_instances($entity_type, $bundle_parent); | |
13 foreach ($instances as $instance) { | |
14 $new_instance = $instance; | |
15 $new_instance['bundle'] = $bundle; | |
16 | |
17 if ($strict) { | |
18 $new_instance['locked'] = TRUE; | |
19 } | |
20 | |
21 $new_instance = field_create_instance($new_instance); | |
22 $query = db_select('field_config_instance', 'fci'); | |
23 $query->addField('fci', 'id'); | |
24 $query->condition('fci.bundle', $bundle); | |
25 $new_instance['id'] = $query->execute()->fetchField(); | |
26 } | |
27 // Check if we perform strict inheritance. | |
28 if ($strict) { | |
29 db_insert('bundle_hierarchy') | |
30 ->fields(array( | |
31 'entity_type' => $entity_type, | |
32 'bundle' => $bundle, | |
33 'bundle_parent' => $bundle_parent | |
34 )) | |
35 ->execute(); | |
36 watchdog('bundle_inherit', 'The %bundle bundle of the entity %type was STRICTLY inherited from %parent_bundle bundle.', array('%bundle' => $bundle, '%bundle_parent' => $bundle_parent, '%type' => $entity_type)); | |
37 drupal_static_reset('bundle_inherit_bundle_get_children'); | |
38 } | |
39 else{ | |
40 watchdog('bundle_inherit', 'The %bundle bundle of the entity %type was SOFTLY inherited from %parent_bundle bundle.', array('%bundle' => $bundle, '%bundle_parent' => $bundle_parent, '%type' => $entity_type)); | |
41 } | |
42 } | |
43 | |
44 /** | |
45 * Implements hook_field_create_instance(). | |
46 */ | |
47 function bundle_inherit_field_create_instance($instance) { | |
48 $children = bundle_inherit_bundle_get_children($instance['entity_type'], $instance['bundle']); | |
49 foreach ($children as $bundle) { | |
50 $new_instance = $instance; | |
51 unset($new_instance['id']); | |
52 $new_instance['bundle'] = $bundle; | |
53 $new_instance['locked'] = TRUE; | |
54 field_create_instance($new_instance); | |
55 } | |
56 } | |
57 | |
58 /** | |
59 * Implements hook_field_update_instance(). | |
60 */ | |
61 function bundle_inherit_field_update_instance($instance, $prior_instance) { | |
62 $children = bundle_inherit_bundle_get_children($prior_instance['entity_type'], $prior_instance['bundle']); | |
63 | |
64 foreach ($children as $bundle) { | |
65 $old_instance = field_info_instance($instance['entity_type'], $instance['field_name'], $bundle); | |
66 | |
67 $new_instance = array( | |
68 'id' => $old_instance['id'], | |
69 'bundle' => $old_instance['bundle'], | |
70 'locked' => TRUE | |
71 ); | |
72 $new_instance += $instance; | |
73 | |
74 field_update_instance($new_instance); | |
75 } | |
76 } | |
77 | |
78 /** | |
79 * Implements hook_field_delete_instance(). | |
80 */ | |
81 function bundle_inherit_field_delete_instance($instance) { | |
82 $children = bundle_inherit_bundle_get_children($instance['entity_type'], $instance['bundle']); | |
83 foreach ($children as $bundle) { | |
84 $new_instance = $instance; | |
85 $new_instance['bundle'] = $bundle; | |
86 $new_instance['locked'] = FALSE; | |
87 try { | |
88 field_update_instance($new_instance); | |
89 } | |
90 catch (Exception $e) { | |
91 drupal_set_message($e->getMessage(), 'error'); | |
92 } | |
93 } | |
94 } | |
95 | |
96 /** | |
97 * Implements hook_form_FORMID_alter(). | |
98 * | |
99 * Attach additional validation callback to the field_ui_field_overview_form. | |
100 * When adding new field instance to the parent we should check that all of it | |
101 * childrens hase not that field instances. | |
102 */ | |
103 function bundle_inherit_form_field_ui_field_overview_form_alter(&$form, &$form_instance, $form_id) { | |
104 $form['#validate'][] = 'bundle_inherit_validate_field_instance_creation'; | |
105 } | |
106 | |
107 /** | |
108 * Additional validation function to the field_ui_field_overview_form. | |
109 * | |
110 * While adding existing field instance, get this form is created for and set | |
111 * form error if any of this children has instance of this field. | |
112 */ | |
113 function bundle_inherit_validate_field_instance_creation($form, &$form_state) { | |
114 $form_values = $form_state['values']['fields']; | |
115 if (!empty($form_values['_add_existing_field']['field_name'])) { | |
116 $children = bundle_inherit_bundle_get_children_all($form['#entity_type'], $form['#bundle']); | |
117 $bundles_with_instance = array(); | |
118 foreach ($children as $child) { | |
119 $prior_instance = field_info_instance($form['#entity_type'], $form_values['_add_existing_field']['field_name'], $child); | |
120 if (!empty($prior_instance)) { | |
121 $bundles_with_instance[] = $prior_instance['bundle']; | |
122 } | |
123 } | |
124 if (count($bundles_with_instance) > 0) { | |
125 $string = implode(", ", $bundles_with_instance); | |
126 form_set_error('fields][_add_existing_field', t("Instance of the field %field can't be attached to %bundle bundle because this field instances are already attached to some of this bundle children bundles: %children", array('%bundle' => $form['#bundle'], '%field' => $form_values['_add_existing_field']['field_name'], '%children' => $string))); | |
127 } | |
128 } | |
129 } | |
130 | |
131 /** | |
132 * Get direct children bundles of the selected entity bundle. | |
133 */ | |
134 function bundle_inherit_bundle_get_children($entity_type, $bundle_parent) { | |
135 $children = &drupal_static(__FUNCTION__); | |
136 if (!isset($children[$entity_type][$bundle_parent])) { | |
137 $children[$entity_type][$bundle_parent] = db_select('bundle_hierarchy', 'bh') | |
138 ->fields('bh', array('bundle')) | |
139 ->condition('bundle_parent', $bundle_parent) | |
140 ->condition('entity_type', $entity_type) | |
141 ->execute() | |
142 ->fetchCol(); | |
143 } | |
144 return $children[$entity_type][$bundle_parent]; | |
145 } | |
146 | |
147 /** | |
148 * Get all children bundles of the selected entity bundle. | |
149 */ | |
150 function bundle_inherit_bundle_get_children_all($entity_type, $bundle_parent) { | |
151 $children = array(); | |
152 $children = bundle_inherit_bundle_get_children($entity_type, $bundle_parent); | |
153 foreach ($children as $child) { | |
154 $children = array_merge($children, bundle_inherit_bundle_get_children_all($entity_type, $child)); | |
155 } | |
156 return $children; | |
157 } | |
158 | |
159 /** | |
160 * Get parent of the selected entity bundle. | |
161 * | |
162 * @return | |
163 * Entity type parent type. | |
164 */ | |
165 function bundle_inherit_bundle_get_parent($entity_type, $bundle) { | |
166 $parent = &drupal_static(__FUNCTION__); | |
167 if (!isset($parent[$entity_type][$bundle])) { | |
168 $parent[$entity_type][$bundle] = db_select('bundle_hierarchy', 'bh') | |
169 ->fields('bh', array('bundle_parent')) | |
170 ->condition('bh.bundle', $bundle) | |
171 ->execute() | |
172 ->fetchField(); | |
173 if (!$parent[$entity_type][$bundle]) $parent[$entity_type][$bundle] = ''; | |
174 } | |
175 return $parent[$entity_type][$bundle]; | |
176 } | |
177 | |
178 /** | |
179 * Attach ineritance form to selected form element. | |
180 * | |
181 * @param $form | |
182 * Parent form element to attach inheritance form to. | |
183 * @param $form_state | |
184 * From state from the parent form. | |
185 * @param $entity_type | |
186 * Entity for which bundle is creating for. | |
187 * @param $bundle | |
188 * If editing existing bundle value for this argument should be provided. | |
189 */ | |
190 function bundle_inherit_attach_inherit_form(&$form, &$form_state, $entity_type, $bundle = '') { | |
191 $entity = entity_get_info($entity_type); | |
192 if (count($entity['bundles']) > 0) { | |
193 if (empty($bundle)) { | |
194 $form['bundle_inherit'] = array( | |
195 '#type' => 'fieldset', | |
196 '#tree' => TRUE, | |
197 '#title' => t('Inheritance') | |
198 ); | |
199 $form['bundle_inherit']['entity_type'] = array('#type' => 'value', '#value' => $entity_type); | |
200 $form['bundle_inherit']['#parents'] = array('bundle_inherit'); | |
201 $form['bundle_inherit']['inherit'] = array( | |
202 '#type' => 'checkbox', | |
203 '#title' => t('Inherit from other') | |
204 ); | |
205 | |
206 foreach ($entity['bundles'] as $bundle_name => $bundle) { | |
207 $options[$bundle_name] = $bundle['label']; | |
208 } | |
209 $form['bundle_inherit']['parent_type'] = array( | |
210 '#type' => 'select', | |
211 '#options' => $options, | |
212 '#title' => t('Parent'), | |
213 '#states' => array( | |
214 // Hide the inheritance settings when inherit checkbox is disabled. | |
215 'invisible' => array( | |
216 'input[name="bundle_inherit[inherit]"]' => array('checked' => FALSE), | |
217 ), | |
218 ), | |
219 ); | |
220 $form['bundle_inherit']['mode'] = array( | |
221 '#type' => 'radios', | |
222 '#options' => array( | |
223 'strict' => t('Strict inherit'), | |
224 'soft' => t('Soft inherit'), | |
225 ), | |
226 '#default_value' => 'strict', | |
227 '#required' => TRUE, | |
228 '#states' => array( | |
229 // Hide the inheritance settings when inherit checkbox is disabled. | |
230 'invisible' => array( | |
231 'input[name="bundle_inherit[inherit]"]' => array('checked' => FALSE), | |
232 ), | |
233 ), | |
234 '#title' => t('Inheritance mode') | |
235 ); | |
236 } | |
237 else { | |
238 $parent_bundle_name = bundle_inherit_bundle_get_parent($entity_type, $bundle); | |
239 if (!empty($parent_bundle_name)) { | |
240 $form['bundle_inherit'] = array( | |
241 '#type' => 'fieldset', | |
242 '#tree' => TRUE, | |
243 '#title' => t('Inheritance') | |
244 ); | |
245 $form['bundle_inherit']['message'] = array( | |
246 '#markup' => t('This bundle was inherited from !parent_bundle bundle.', array('!parent_bundle' => l($entity['bundles'][$parent_bundle_name]['label'], $entity['bundles'][$parent_bundle_name]['admin']['real path'].'/fields'))) | |
247 ); | |
248 } | |
249 } | |
250 } | |
251 } | |
252 | |
253 /** | |
254 * Should be executed when entity creation form is submiting. | |
255 * | |
256 * @param $bundle | |
257 * Newly created bundle name. | |
258 */ | |
259 function bundle_inherit_attach_inherit_form_submit($bundle, &$form, &$form_state) { | |
260 if (isset($form_state['values']['bundle_inherit']) && $form_state['values']['bundle_inherit']['inherit']) { | |
261 $bundle_inherit_values = $form_state['values']['bundle_inherit']; | |
262 bundle_inherit_perform($bundle_inherit_values['entity_type'], $bundle, $bundle_inherit_values['parent_type'], $bundle_inherit_values['mode'] == 'strict' ? TRUE : FALSE); | |
263 } | |
264 } | |
265 | |
266 /** | |
267 * Implements hook_field_attach_delete_bundle(). | |
268 */ | |
269 function bundle_inherit_field_attach_delete_bundle($entity_type, $bundle, $instances) { | |
270 db_delete('bundle_hierarchy') | |
271 ->condition('entity_type', $entity_type) | |
272 ->condition(db_or()->condition('bundle_parent', $bundle)->condition('bundle', $bundle)) | |
273 ->execute(); | |
274 } | |
275 | |
276 /** | |
277 * Implements hook_field_attach_rename_bundle(). | |
278 */ | |
279 function bundle_inherit_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { | |
280 db_update('bundle_hierarchy') | |
281 ->condition('entity_type', $entity_type) | |
282 ->condition('bundle', $bundle_old) | |
283 ->fields(array( | |
284 'bundle' => $bundle_new | |
285 )) | |
286 ->execute(); | |
287 db_update('bundle_hierarchy') | |
288 ->condition('entity_type', $entity_type) | |
289 ->condition('bundle_parent', $bundle_old) | |
290 ->fields(array( | |
291 'bundle_parent' => $bundle_new | |
292 )) | |
293 ->execute(); | |
294 } |