-
Notifications
You must be signed in to change notification settings - Fork 2
/
eck.module
388 lines (336 loc) · 10.3 KB
/
eck.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
* @file
*
* ENTITY CONSTRUCTION KIT
*
* This module is a fairly simple module. It exposes an administration section
* for creating entity types, bundles and entities. This module also keeps track
* in the database of the entities that have been created with eck (eck db
* table), and the different bundles that have been creates (eck_type db table).
*
* If you want to follow the flow of this modules functionality I suggest
* starting with the hook menu, where all of the paths for the administration
* section are defined.
*/
/**
* Implements hook_features_api().
*/
function eck_features_api() {
return array(
'eck' => array(
'name' => t('Entity types'),
'feature_source' => TRUE,
'default_hook' => 'eck_info',
),
);
}
/**
* Implements hook_entity_info().
*
* The Entity information for all the entity types created with eck.
*/
function eck_entity_info() {
module_load_include('inc', 'eck', 'eck.entity_type');
$info = array();
// Get all the names of all the entity types from the eck table
// for each of the created entity types add its info to the $info array.
foreach (eck__get_entity_types() as $entity_type) {
// eck__entity_info creates the entity_info for each entity type.
$info = array_merge($info, eck__entity_type__info($entity_type));
}
return $info;
}
/**
* Implements hook_entity_property_info_alter().
*/
function eck_entity_property_info_alter(&$info) {
module_load_include('inc', 'eck', 'eck.entity_type');
// Create property infos for all defined entites.
foreach (eck__get_entity_types () as $entity_type) {
eck__entity_type__property_info($info[$entity_type], $entity_type);
}
}
/**
* Implements hook_menu().
*
* Define the paths for the administration section of the Entity Factory
* The menues get created in three functions
* eck__entity_type__mene()
* eck__bundle__menu()
* eck__entity__menu()
* This approach made sense thanks to the recursive nature of the menu itmes
* that need to be created
*/
function eck_menu() {
module_load_include('inc', 'eck', 'eck.entity_type');
$menu = eck__entity_type__menu();
return $menu;
}
/**
* Implements hook_permission().
*
* A pretty through set of permission
* you can set permissiona at each level: entity_type, bundle, entity
* and for each action of the CRUD
*/
function eck_permission() {
$perms = array();
$perms['administer entity types'] =
array(
'title' => t('Administer Entity Types'),
'restrict access' => TRUE
);
$perms['add entity types'] =
array(
'title' => t('Add Entity Types'),
'restrict access' => TRUE
);
foreach (eck__get_entity_types() as $entity_type) {
$entity_type_label = eck__get_entity_type_label($entity_type);
foreach (array('administer' => 'Administer', 'add' => "Add") as $op => $op_label) {
$perms["{$op} {$entity_type} bundles"] = array(
'title' => "{$op_label} {$entity_type_label} Bundles"
);
}
foreach (eck__get_bundles($entity_type) as $bundle) {
$bundle_label = eck__get_bundle_label($entity_type, $bundle);
foreach (array('administer' => 'Administer', 'add' => "Add", 'view' => 'View', 'edit' => 'Edit', 'delete' => 'Delete') as $op => $op_label) {
$perms["{$op} {$entity_type} {$bundle} entities"] = array(
'title' => "{$op_label} {$entity_type_label} {$bundle_label} Entities"
);
}
}
}
return $perms;
}
/**
* This function creates and entity
* @param $entity_type
* A string witht the type of entity to be created (node, user, etc)
* @param $bundle
* The bundle for the entity to be created (String)
*/
function eck__entity_create($entity_type, $bundle) {
global $user;
$values = array(
'entity_name' => $entity_type,
'type' => $bundle,
'uid' => $user->uid,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
return entity_create($entity_type, $values);
}
/**
* Get all the bundles for a given entity_type.
*/
function eck__get_bundles($entity_types) {
$data = db_select('eck_types', 't')->fields('t', array('type'))->condition('entity', $entity_types)->execute();
$bundles = array();
foreach ($data as $bundle_obj) {
$bundles[] = $bundle_obj->type;
}
return $bundles;
}
/**
* Get the label for a given entity type.
*/
function eck__get_bundle_label($entity_type, $bundle) {
$data = db_select('eck_types', 'e')->fields('e', array('label'))
->condition('entity', $entity_type, '=')
->condition('type', $bundle, '=')->execute();
foreach ($data as $bundle_obj) {
return $bundle_obj->label;
}
return NULL;
}
/**
* Get all defined entity types.
*/
function eck__get_entity_types() {
$data = db_select('eck', 'e')->fields('e', array('name'))->execute();
$entity_types = array();
foreach ($data as $entity_type_obj) {
$entity_types[] = $entity_type_obj->name;
}
return $entity_types;
}
/**
* Get the label for a given entity type.
* @param $entity_type
* The entity_type that we want the label for (String)
*/
function eck__get_entity_type_label($entity_type) {
$data = db_select('eck', 'e')->fields('e', array('label'))
->condition('name', $entity_type, '=')->execute();
foreach ($data as $entity_type_obj) {
return $entity_type_obj->label;
}
return NULL;
}
/**
* Creates a table showing a group of entities.
*
* @param $entities
* the entities to create the table from
* @param $select
* a boolean value that will determine whether the
* table is a select table or a regular table
*/
function entities_table($entities, $select = FALSE) {
$rows = array();
$header = array(t('Name'), array('data' => t('Operations'), 'colspan' => '1'));
foreach ($entities as $entity) {
$entity_type = $entity->entityType();
$bundle = $entity->type;
$id = $entity->id;
$uri = entity_uri($entity_type, $entity);
$row = array(l(entity_label($entity_type, $entity), $uri['path'], $uri['options']));
$row[] = array('data' => l(t('delete'), "admin/structure/eck/{$entity_type}/{$bundle}/{$id}/delete"));
drupal_alter("entity_tr", $row, $entity_type, $entity);
drupal_alter("entity_{$entity_type}_tr", $row, $entity);
$rows[$id] = $row;
}
if ($select) {
if (!isset($entity_type)) {
return array('#theme' => 'table', '#header' => $header, '#rows' => $rows);
}
else {
return drupal_get_form("entity_table_select", $entity_type, $bundle, $header, $rows);
}
}
else {
return array('#theme' => 'table', '#header' => $header, '#rows' => $rows);
}
}
/**
* Helper function for the entities_table.
* This function creates a select table.
*
* @param $form
* A form arrary as returned by drupal_get_form
* @param $state
* The form state, this is also provided by the Form API
* @param $enttiy_type
* (String) the type of the entities that will be in the table
* @param $bundle
* (String) The bundle of the entity that will be in the table
* @param $header
* An array for the table header for more info look at theme_table
* @param $rows
* The rows of the table for more info on what this should look like look
* at theme_table
*/
function entity_table_select($form, &$state, $entity_type, $bundle, $header, $rows) {
$form['entity_type'] = array('#type' => 'value', '#value' => $entity_type);
$form['bundle'] = array('#type' => 'value', '#value' => $bundle);
$form['do'] = array(
'#type' => 'submit',
'#value' => t('Do'),
);
$form['entity_table'] = array(
// '#theme' => 'table',
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
);
return $form;
}
/**
* The submit callback for the select table
* This function simply implements an alter hook
* so other modules can define the functionality of the
* select table
*/
function entity_table_select_submit($form, &$state) {
drupal_alter('entity_table_select_submit', $state);
}
/**
* When an entity form is submitted, field for which no information was inputed
* are still returned, then if we submit that data, empty rows are created in
* those field databases cluttering them. This function checks and makes sure
* that the data returned for a field is not empty and unsets it if it is, so no
* empty data will be added to the database.
*
* @param $field_name
* The name of the field.
* @param $data
* The data for the field: It usually has this format
* array(lang => array( 0 => array( <field stuff> ), 1 => ...));
*/
function _field_unset_empty($field_name, $data) {
// If there is a value we need to check that it is not empty.
$info = field_info_field($field_name);
foreach ($data[LANGUAGE_NONE] as $key => $values) {
$empty = TRUE;
foreach (array_keys($info['columns']) as $index) {
if (!empty($values[$index])) {
$empty = FALSE;
}
}
if ($empty) {
unset($data[LANGUAGE_NONE][$key]);
}
}
return $data;
}
/**
* As you can see this is just use to comply with requirements
* probably no one will use this
*/
function _eck_fake_exists() {
return FALSE;
}
/**
* Generates an upper camel case class name from a machine name.
*
* @params $name
* The machine name to convert to camel case.
* @params $suffix
* Optional class name suffix.
*/
function eck_get_class_name($name, $suffix = '') {
$parts = array_map('ucfirst', explode('_', $name));
if ($suffix) {
$parts[] = $suffix;
}
return implode('', $parts);
}
/**
* Retrieve the entity label
*/
function eck_get_entity_label($entity_type, $entity_id) {
if (!is_numeric($entity_id)) {
return FALSE;
}
$entity = entity_load($entity_type, array($entity_id));
$entity = $entity[$entity_id];
if (is_object($entity) && !empty($entity)) {
return entity_label($entity_type, $entity);
}
else {
return NULL;
}
}
/**
* Base controller class for ECK entites.
*/
class EckController extends EntityAPIController {
public function create(array $values = array()) {
global $user;
// Set default values.
$values += array(
'type' => $this->entityType,
'uid' => $user->uid,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
// Allow to create custom per-bundle specific class implementations.
$class_name = eck_get_class_name($values['type'], 'EntityType');
if (drupal_autoload_class($class_name)) {
$this->entityInfo['entity class'] = $class_name;
}
return parent::create($values);
}
}