-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheck.features.inc
242 lines (205 loc) · 6.59 KB
/
eck.features.inc
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
<?php
/**
* @file
* Integration with the Features module.
*/
/**
* @{
* ECK Entity Types.
*/
/**
* Implements hook_features_export_options().
*/
function eck_entity_type_features_export_options() {
module_load_include('inc', 'eck', 'eck.entity_type');
$entity_types = array();
foreach (EntityType::loadAll() as $entity_type) {
$entity_types[$entity_type->name] = $entity_type->label;
}
return $entity_types;
}
/**
* Implements hook_features_export().
*/
function eck_entity_type_features_export($data, &$export, $module_name = '') {
$pipe = array();
foreach ($data as $entity_type) {
// Export the entity type.
$export['features']['eck_entity_type'][$entity_type] = $entity_type;
$export['dependencies']['eck_entity_type'] = 'eck';
// @todo We need to add dependencies on the modules implementing the
// property behaviors currently they are all implemented by ECK but in the
// future, people might have their custom behaviors.. or we might have
// behaviors provided by contrib.
$export['dependencies']['features'] = 'features';
}
return $pipe;
}
/**
* Implements hook_features_export_render().
*/
function eck_entity_type_features_export_render($module, $data, $export = NULL) {
// I am guessing that using FALSE and TRUE to initialize the array is
// arbritrary, rigth?
$elements = array(
'name' => FALSE,
'label' => TRUE,
'properties' => FALSE,
);
$output = array();
$output[] = ' $items = array(';
foreach ($data as $entity_type_name) {
$entity_type = EntityType::loadByName($entity_type_name);
$elements['name'] = $entity_type->name;
$elements['label'] = $entity_type->label;
$elements['properties'] = $entity_type->properties;
$output[] = ' ' . "'{$entity_type->name}' => " . features_var_export($elements, ' ') . ",";
}
$output[] = ' );';
$output[] = ' return $items;';
return array('eck_entity_type_info' => implode("\n", $output));
}
/**
* Implements hook_features_revert().
*/
function eck_entity_type_features_revert($module) {
eck_entity_type_features_rebuild($module);
}
/**
* Implements hook_features_rebuild().
*/
function eck_entity_type_features_rebuild($module) {
if ($default_entities = features_get_default('eck_entity_type', $module)) {
foreach ($default_entities as $entity_type_name => $entity_type_info) {
// Load the existing entity type, if one exists.
$entity_type = EntityType::loadByName($entity_type_name);
if (empty($entity_type->id)) {
$entity_type = new EntityType();
}
// Look at all of the existing entities properties, and if one exists
// that does not exist in the code definition, delete it.
foreach ($entity_type->properties as $property_key => $property) {
if (!isset($entity_type_info['properties'][$property_key])) {
$entity_type->removeProperty($property_key);
}
}
// Look at all properties as defined in code, and add or change them
// Depending on wether they already exist or not.
foreach ($entity_type_info as $key => $value) {
if ($key == 'properties') {
// Loop through the new properties.
foreach ($entity_type_info['properties'] as $property_key => $property) {
// If the property already exists, update the behavior.
if (isset($entity_type->properties[$property_key])) {
$entity_type->changeBehavior($property_key, $property['behavior']);
}
else {
// Property didn't already exist, so lets create it.
$entity_type->addProperty($property_key, $property['label'], $property['type'], $property['behavior']);
}
}
}
else {
$entity_type->{$key} = $value;
}
}
$entity_type->save();
}
eck_clean_up();
}
}
/**
* @} End of ECK Entity Types.
*/
/**
* @{
* ECK Bundles
*/
/**
* Implements hook_features_export_options().
*/
function eck_bundle_features_export_options() {
$bundles = array();
foreach (Bundle::loadAll() as $bundle) {
// @todo Ideally, all bundles should be accessible to features, but
// we're currently just targeting those created through ECK.
$entity_type = EntityType::loadByName($bundle->entity_type);
$bundles[$bundle->machine_name] = "{$entity_type->label}:{$bundle->label}";
}
return $bundles;
}
/**
* Implements hook_features_export().
*/
function eck_bundle_features_export($data, &$export, $module_name = '') {
$pipe = array();
// What is this for?
$map = features_get_default_map('eck');
foreach ($data as $type) {
// Export the entity type.
$export['features']['eck_bundle'][$type] = $type;
$export['dependencies']['eck'] = 'eck';
$export['dependencies']['features'] = 'features';
// Export fields.
// @TODO: Features already supports this, just gotta find the right calls.
$bundle = Bundle::loadByMachineName($type);
$fields = field_info_instances($bundle->entity_type, $bundle->name);
foreach ($fields as $field) {
$pipe['field'][] = "{$bundle->entity_type}-{$field['bundle']}-{$field['field_name']}";
}
}
return $pipe;
}
/**
* Implements hook_features_export_render().
*/
function eck_bundle_features_export_render($module, $data, $export = NULL) {
$elements = array(
'machine_name' => NULL,
'entity_type' => NULL,
'name' => NULL,
'label' => NULL,
'config' => NULL,
);
$output = array();
$output[] = ' $items = array(';
foreach ($data as $bundle_machine_name) {
$bundle = Bundle::loadByMachineName($bundle_machine_name);
unset($bundle->id);
foreach ($elements as $key => $value) {
$elements[$key] = $bundle->{$key};
}
$output[] = " '{$bundle->machine_name}' => " . features_var_export($elements, ' ') . ",";
}
$output[] = ' );';
$output[] = ' return $items;';
return array('eck_bundle_info' => implode("\n", $output));
}
/**
* Implements hook_features_revert().
*/
function eck_bundle_features_revert($module) {
eck_bundle_features_rebuild($module);
}
/**
* Implements hook_features_rebuild().
*
* Rebuilds eck entities from code defaults.
*/
function eck_bundle_features_rebuild($module) {
if ($default_types = features_get_default('eck_bundle', $module)) {
foreach ($default_types as $bundle_machine_name => $bundle_info) {
$bundle = Bundle::loadByMachineName($bundle_machine_name);
if (empty($bundle->id)) {
$bundle = new Bundle();
}
foreach ($bundle_info as $key => $value) {
$bundle->{$key} = $value;
}
$bundle->save();
}
}
}
/**
* @} End of ECK Bundles
*/