-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle_inheritance.module
More file actions
304 lines (285 loc) · 9.71 KB
/
Copy pathbundle_inheritance.module
File metadata and controls
304 lines (285 loc) · 9.71 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
<?php
define( 'BUNDLE_INHERIT_KEY', 'inherit bundle');
function bundle_inheritance_entity_info ()
{
$entities = array();
$entities[BundleInheritanceRelation::EntityType] = array(
'label' => t('inheritance relation'),
'controller class' => 'BundleInheritanceController',
'entity class' => 'BundleInheritanceRelation',
'base table' => 'bundle_inheritances',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'id',
'label' => 'label',
'bundle' => 'bundle'
),
'bundle keys' => array(
'bundle' => 'bundle'
)
);
return $entities;
}
/**
*
* @param unknown $entities
*/
function bundle_inheritance_entity_info_alter (&$entities)
{
foreach( $entities as $entityName=>$entity)
{
if( array_key_exists( 'bundles', $entity) )
{
foreach( $entity['bundles'] as $bundleName=>$bundle )
{
if( array_key_exists(BUNDLE_INHERIT_KEY, $bundle) )
{
$module = NULL;
if( $entityName == 'node')
{
$type = node_type_load($bundleName);
$module = $type->module;
}
bundle_inheritance_do_inheritance($entityName, $bundle[BUNDLE_INHERIT_KEY], $bundleName, $module );
}
}
}
}
}
/**
* makes inheritance
*
* @param string $entity_type
* @param string $parent
* @param string $child
*/
function bundle_inheritance_do_inheritance ($entity_type, $parent, $child, $module = 'bundle_inheritance')
{
watchdog('debug', 'bundle_inheritance_do_inheritance between '.$parent.' and '.$child );
$relation = new BundleInheritanceRelation();
// store inheritance relation in a table
$relation->parent = $parent;
$relation->child = $child;
$relation->entity_type = $entity_type;
if ($relation->save()) {
foreach (bundle_inheritance_copy_field_instances($entity_type, $parent, $child) as $instance) {
if (! field_read_instance($entity_type, $instance['field_name'], $child)) {
field_create_instance($instance);
}
}
}
}
function bundle_inheritance_break_inheritance($entity_type, $parent, $child)
{
watchdog( "bundle_inheritance", "breaks inheritance link : " .$entity_type." :: ".$parent." :: ". $child );
$ctrl = new BundleInheritanceController();
$relations = $ctrl->load( FALSE, array( 'parent' => $parent, 'child' => $child) );
$parentInstances = field_info_instances($entity_type, $parentBundle);
foreach( $relations as $relation )
{
$childInstances = field_info_instances( $entity_type, $relation->child);
foreach( $parentInstances as $key=>$instance )
{
if( array_key_exists( $key, $childInstances) && !$childInstances[$key]['settings']['bundle_inheritance']['modified'] )
{
field_delete_instance( $childInstances[$key] );
}
}
$relation->delete();
}
}
function bundle_inheritance_copy_field_instances ($entity_type, $parentBundle, $newBundle)
{
try {
// liste des instances à récupérer
$parentInstances = field_info_instances($entity_type, $parentBundle);
if ($parentInstances) {
foreach ($parentInstances as $key => $instance) {
$parentInstances[$key] = bundle_inheritance_prepare_instance($parentInstances[$key], $newBundle);
}
}
} catch (Exception $e) {
$parentInstances = array();
}
return $parentInstances;
}
/**
*
* @param array $instance
* @param string $newBundle
*/
function bundle_inheritance_prepare_instance ($instance, $newBundle)
{
unset($instance['id']);
$instance['bundle'] = $newBundle;
$instance['label'] .= " [Inherited]";
$instance['settings']['bundle_inheritance']['inherited'] = true;
$instance['settings']['bundle_inheritance']['modified'] = FALSE;
return $instance;
}
/**
* implementation of hook_field_create_instance
*
* when an instance is created on an identified superclass
* the instance have to be copied
*
* @param unknown $instance
*/
function bundle_inheritance_field_create_instance ($instance)
{
$ctrl = new BundleInheritanceController(BundleInheritanceRelation::EntityType);
// retrieving all childs from
$chain = $ctrl->getChilds($instance['bundle']);
if (! empty($chain)) {
foreach ($chain as $bundle) {
$newInstance = bundle_inheritance_prepare_instance($instance, $bundle);
field_create_instance($newInstance);
}
}
}
/**
* implementation of hook_field_update_instance
*
* when an instance is updated on an identified superclass
* (only if the child hasn't been modified)
* the instance have to be copied
*
* @param unknown $instance
*
*/
function bundle_inheritance_field_update_instance ($instance, $prior_instance)
{
$updateFlag =& drupal_static( __FUNCTION__.'flag' );
$ctrl = new BundleInheritanceController(BundleInheritanceRelation::EntityType);
$isOverrided = isset($instance['settings']['bundle_inheritance']) && $instance['settings']['bundle_inheritance']['modified'];
// lookin' for subclass childs bundle
$chain = $ctrl->getChilds($instance['bundle']);
if (! empty($chain)) {
foreach ($chain as $bundle) {
$existing = field_read_instance($instance['entity_type'], $instance['field_name'], $bundle);
if (! $existing['settings']['bundle_inheritance']['modified']) {
$newInstance = bundle_inheritance_prepare_instance($instance, $bundle);
// we forbid the override check for the updated child
$updateFlag = FALSE;
field_update_instance($newInstance);
$updateFlag = TRUE;
}
}
} else {
if (! $isOverrided && $updateFlag) {
// case of an inherited which is modified
// so we flag it to modified
$diff = NULL;
$justWeight = FALSE;
if( is_array( $instance) && is_array( $prior_instance ) )
{
$diff = array_diff_assoc_recursive( $instance, $prior_instance);
dsm( $diff, "diff");
if( is_array($diff) && array_keys( $diff ) == array( 'widget') && array_keys( $diff['widget']) == array( 'weight') )
{
$justWeight = TRUE;
}
}
if( !$justWeight ) {
$instance['settings']['bundle_inheritance']['modified'] = TRUE;
$instance['label'] = str_replace(" [Inherited]", "", $instance['label'] );
field_update_instance($instance);
}
}
}
}
/**
* implementation of hook_field_delete_instance
*
* when an instance is deleted on an identified superclass
* the instance have to be copied
*
* @param unknown $instance
*/
function bundle_inheritance_field_delete_instance ($instance)
{
$ctrl = new BundleInheritanceController(BundleInheritanceRelation::EntityType);
$chain = $ctrl->getChilds($instance['bundle']);
if (! empty($chain)) {
foreach ($chain as $bundle) {
$existing = field_read_instance($instance['entity_type'], $instance['field_name'], $bundle);
if (! $existing['settings']['bundle_inheritance']['modified']) {
$newInstance = bundle_inheritance_prepare_instance($instance, $bundle);
field_delete_instance($newInstance);
}
}
}
}
/**
* implementation of hook_module_implements_alter
* we need to be sure that this module is the last to execute entity_info_alter
* in order to guarantee that other modules may have filled inheritance metadata
* from entity_info_alter
*
* @param array $implementations
* @param string $hook
*/
function bundle_inheritance_module_implements_alter (&$implementations, $hook)
{
if (in_array($hook, array(
'entity_info_alter'
))) {
// Move our hook implementation to the bottom.
$group = $implementations['bundle_inheritance'];
unset($implementations['bundle_inheritance']);
$implementations['bundle_inheritance'] = $group;
}
}
function bundle_inheritance_node_type_delete( $info )
{
watchdog( 'bundle_inheritance', "deleted content type : ".$info->type );
$ctrl = new BundleInheritanceController(BundleInheritanceRelation::EntityType);
$chain = $ctrl->getInheritanceChain( $info->type );
if( !is_null( $chain ) )
{
foreach( $chain as $bundle)
{
bundle_inheritance_break_inheritance( 'node', $bundle, $info->type);
}
}
$chain = $ctrl->getChilds( $info->type );
if( !is_null( $chain ) )
{
foreach( $chain as $bundle)
{
bundle_inheritance_break_inheritance( 'node', $info->type, $bundle);
}
}
}
function array_diff_assoc_recursive($array1, $array2)
{
$difference = NULL;
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!array_key_exists($key, $array2))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!array_key_exists($key, $array2) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
?>