-
Notifications
You must be signed in to change notification settings - Fork 3
/
eck.bundle.inc
497 lines (424 loc) · 15 KB
/
eck.bundle.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* @file
* All of the menu, pages, and forms related to bundle administration.
*/
module_load_include('inc', 'eck', 'eck.classes');
/**
* This function creates the menu items relevant to bundle administration.
*
* @param /EntityType $entity_type
* As returned by eck__entity_type__load().
*
* @see eck_menu()
*/
function eck__bundle__menu($entity_type) {
// Create the menus relavant to types.
$path = eck__entity_type__path();
$menu = array();
// Dummy menu items so that entity translation will not complain about there
// not being a default.
// @todo: Remove these dummy items once issue https://drupal.org/node/2273189
// is resolved.
$menu["$entity_type->name/%"] = array(
'page callback' => FALSE,
'page arguments' => array(),
'access callback' => FALSE,
'access arguments' => array(),
);
$menu["$entity_type->name/%/edit"] = array(
'page callback' => FALSE,
'page arguments' => array(),
'access callback' => FALSE,
'access arguments' => array(),
);
// DELETE Entity Types.
$menu["{$path}/{$entity_type->name}/delete"] = array(
'title' => "Delete",
'description' => "Delete the '{$entity_type->label}' Entity Type",
// "eck__entity_type__delete",
'page callback' => "drupal_get_form",
'page arguments' => array("eck__entity_type__delete_form", $entity_type->name),
'access callback' => 'eck__multiple_access_check',
'access arguments' => array(array('eck administer entity types', 'eck delete entity types')),
'file' => 'eck.entity_type.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
// MANAGE Entity Type Properties.
$menu["{$path}/{$entity_type->name}/properties"] = array(
'title' => "Manage properties",
'description' => "Manage the properties of the {$entity_type->label} entity type",
// "eck__entity_type__delete",
'page callback' => "drupal_get_form",
'page arguments' => array("eck__properties__form", $entity_type->name),
'access callback' => 'eck__multiple_access_check',
'access arguments' => array(
array(
'eck administer entity types',
"manage {$entity_type->name} properties",
),
),
'file' => 'eck.properties.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 4,
);
// LIST Bundles.
$menu["{$path}/{$entity_type->name}"] = array(
'title' => "{$entity_type->label}",
'description' => "View all the bundles for '{$entity_type->label}'",
'page callback' => "eck__bundle__list",
'page arguments' => array(3),
'access callback' => 'eck__multiple_access_check',
'access arguments' => array(
array(
'eck administer bundles',
'eck list bundles',
"eck administer {$entity_type->name} bundles",
"eck list {$entity_type->name} bundles",
),
),
'file' => 'eck.bundle.inc',
);
$menu["{$path}/{$entity_type->name}/list"] = array(
'title' => 'Bundle List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 100,
);
// EDIT Entity Types.
$menu["{$path}/{$entity_type->name}/edit"] = array(
'title' => "Edit",
'description' => "Edit the '{$entity_type->label}' entity type",
'page callback' => 'drupal_get_form',
'page arguments' => array("eck__entity_type__form", $entity_type->name),
'access callback' => 'eck__multiple_access_check',
'access arguments' => array(array('eck administer entity types', 'eck edit entity types')),
'file' => 'eck.entity_type.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
// ADD Bundles.
$menu["{$path}/{$entity_type->name}/add"] = array(
'title' => "Add bundle ",
'description' => "Add a(n) new '{$entity_type->label} Bundle'",
'page callback' => "drupal_get_form",
'page arguments' => array('eck__bundle__add', 3),
'access callback' => 'eck__multiple_access_check',
'access arguments' => array(
array(
'eck administer bundles',
'eck add bundles',
"eck administer {$entity_type->name} bundles",
"eck add {$entity_type->name} bundles",
),
),
'type' => MENU_LOCAL_ACTION,
'weight' => 0,
'file' => 'eck.bundle.inc',
);
module_load_include('inc', 'eck', 'eck.entity');
foreach (Bundle::loadByEntityType($entity_type) as $bundle) {
$menu = array_merge($menu, eck__entity__menu($entity_type, $bundle));
}
return $menu;
}
/**
* Page call back for the bundle overview table.
*
* to see and manipulate all created label of a given type.
*
* @param string $entity_type
* The name of the entity type.
*/
function eck__bundle__list($entity_type) {
$path = eck__entity_type__path();
$entity_type = entity_type_load($entity_type);
// Check that the user has permissions to view bundle lists.
// @todo Do we really need this, can they be here without the right
// permissions in the first place?
if (eck__multiple_access_check(array(
'eck administer bundles',
'eck list bundles',
"eck administer {$entity_type->name} bundles",
"eck list {$entity_type->name} bundles",
)
)) {
$header = array(t('Type'), array(
'data' => t('Operations'),
'colspan' => '1'));
$rows = array();
$bundles = Bundle::loadByEntityType($entity_type);
usort($bundles, 'eck_alphabetical_cmp');
foreach ($bundles as $bundle) {
$bundle_label = $bundle->label;
$admin_info = get_bundle_admin_info($entity_type->name, $bundle->name);
$uri = $admin_info['path'];
$allowed_operations = '';
// Check that the user has permissions to delete:
$perm_check = array(
'eck administer bundles',
'eck delete bundles',
"eck administer {$entity_type->name} bundles",
"eck delete {$entity_type->name} bundles",
);
if (eck__multiple_access_check($perm_check)) {
$allowed_operations = l(t('delete'), $uri . "/delete");
}
// Check that the user can view lists of entities.
$perm_check = array(
'eck administer entities',
'eck list entities',
"eck administer {$entity_type->name} {$bundle->name} entities",
"eck list {$entity_type->name} {$bundle->name} entities",
);
if (eck__multiple_access_check($perm_check)) {
$label = l($bundle_label, url($uri, array('absolute' => TRUE)));
}
else {
$label = $bundle_label;
}
$rows[] = array($label, $allowed_operations);
}
$build['bundle_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
}
return $build;
}
/**
* Bundle add from callback.
*/
function eck__bundle__add($form, &$form_state, $entity_type) {
$entity_type = entity_type_load($entity_type);
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['bundle_label'] = array(
'#type' => 'textfield',
'#title' => t('Type'),
'#description' => "A Human readable name for the bundle",
);
$form['bundle_name'] = array(
'#type' => 'machine_name',
'#maxlength' => 32,
'#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores. Maximum length 32 characters.'),
'#required' => FALSE,
'#machine_name' => array(
'exists' => '_eck_fake_exists',
'source' => array('bundle_label'),
),
);
$form['#validate'][] = 'eck__bundle__add_validate';
$form['submit'] = array(
'#type' => 'submit',
'#weight' => 10000,
'#value' => t('Save'),
);
return $form;
}
/**
* Bundle add form validation callback.
*/
function eck__bundle__add_validate($form, &$form_state) {
$entity_type = $form_state['values']['entity_type'];
// The type does not have to be unique in the table, but it should be unique
// to its entity so we will check that here.
foreach (Bundle::loadByEntityType($entity_type) as $bundle) {
if ($bundle->name == $form_state['values']['bundle_name']) {
form_set_error('bundle', t("bundle @bundle_name already exists for this entity type",
array("@bundle_name" => $bundle->name)));
}
}
}
/**
* Add bundle form callback.
*/
function eck__bundle__add_submit($form, &$form_state) {
$path = eck__entity_type__path();
$entity_type = $form_state['values']['entity_type'];
$bundle_name = $form_state['values']['bundle_name'];
$bundle_label = $form_state['values']['bundle_label'];
$bundle = new Bundle();
$bundle->name = $bundle_name;
$bundle->label = $bundle_label;
$bundle->entity_type = $entity_type->name;
$bundle->save();
$msg = 'the bundle @bundle for entity type @entity_type has been created.';
$args = array(
'@bundle' => $bundle->label,
'@entity_type' => $entity_type->label,
);
$context = array('bundle' => $bundle, 'form_state' => $form_state);
drupal_alter('eck_bundle_save_message', $msg, $args, $context);
if ($msg) {
drupal_set_message(t($msg, $args));
}
$form_state['redirect'] = "{$path}/{$entity_type->name}";
}
/**
* Form constructor for the entity bundle editing form.
*
* @param string $entity_type_name
* Entity type name.
* @param string $bundle_name
* Entity bundle name.
*/
function eck__bundle__edit_form($form, &$form_state, $entity_type_name, $bundle_name) {
$path = eck__entity_type__path();
$entity_type = entity_type_load($entity_type_name);
$bundle = bundle_load($entity_type_name, $bundle_name);
$form = array();
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['bundle'] = array(
'#type' => 'value',
'#value' => $bundle,
);
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $bundle->label,
'#required' => TRUE,
);
// Create a list of properties for this bundle.
$properties = array();
foreach ($entity_type->properties as $property => $info) {
$properties[$property] = $property . ' (' . $info['label'] . ')';
}
// Determine which managed fields should be checked by default.
$default_properties = (!empty($bundle->config['managed_properties']) && is_array($bundle->config['managed_properties'])) ? array_intersect_key($bundle->config['managed_properties'], $properties) : array();
// Add configuration to manage property display through the manage field
// forms.
$form['config_managed_properties'] = array(
'#type' => 'checkboxes',
'#options' => $properties,
'#default_value' => $default_properties,
'#title' => t('Managed Properties'),
'#description' => t('Select the properties you would like to be able to manage like fields through the "Manage Fields" and "Display Fields" tabs on this Bundle.'),
);
// Let the behaviors to modify form.
$vars = eck_property_behavior_invoke_plugin_alter($entity_type, 'bundle_form', array('form' => $form, 'form_state' => $form_state));
$form = $vars['form'];
$form_state = $vars['form_state'];
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save bundle'),
'#weight' => 40,
);
return $form;
}
/**
* Budle edit form submit callback.
*/
function eck__bundle__edit_form_submit($form, &$form_state) {
$entity_type = $form_state['values']['entity_type'];
$bundle = $form_state['values']['bundle'];
// Save all form values starting from 'config_' to the $bundle->config array.
$bundle_config = array();
foreach ($form_state['values'] as $name => $value) {
if (strpos($name, 'config_') === 0) {
// We don't need 'config_' prefix.
$bundle_config[substr($name, 7)] = $form_state['values'][$name];
}
}
$bundle->config = $bundle_config;
$bundle->label = $form_state['values']['label'];
// Save the bundle object to the database.
$bundle->save();
drupal_set_message(t('the bundle %bundle for entity type %entity_type has been updated.',
array('%bundle' => $bundle->label, '%entity_type' => $entity_type->label)));
}
/**
* Delete the bundle of a given entity type.
*
* @param \EntityType $entity_type
* The entity type of the bundle that will be deleted.
* @param \Bundle $bundle
* The bundle to be deleted.
*
* @see eck.classes.inc
*/
function eck__bundle__delete($entity_type, $bundle) {
// And finally we delete the bundle from the eck_type table.
$bundle->delete();
drupal_set_message(t('The bundle %bundle from the entity type %entity_type has been deleted.',
array('%bundle' => $bundle->label, '%entity_type' => $entity_type->label)));
}
/**
* Bundle delete form callback.
*/
function eck__bundle__delete_form($form, &$form_state, $entity_type_name, $bundle_name) {
$path = eck__entity_type__path();
$entity_type = entity_type_load($entity_type_name);
$bundle = bundle_load($entity_type_name, $bundle_name);
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['bundle'] = array(
'#type' => 'value',
'#value' => $bundle,
);
$form['submit_redirect'] = array(
'#type' => 'value',
'#value' => "{$path}/{$entity_type->name}",
);
$message = t('Are you sure that you want to delete the bundle %bundle?',
array('%bundle' => $bundle->label));
$caption = t("All of the data (entities) associated with this bundle
will be deleted. This action cannot be undone.");
return confirm_form($form, $message, "{$path}/{$entity_type->name}", $caption, t('Delete'));
}
/**
* Submit form callback to delete bundles.
*/
function eck__bundle__delete_form_submit($form, &$form_state) {
$entity_type = $form_state['values']['entity_type'];
$bundle = $form_state['values']['bundle'];
$submit_redirect = $form_state['values']['submit_redirect'];
eck__bundle__delete($entity_type, $bundle);
// Ok, lets delete the entity.
$form_state['redirect'] = $submit_redirect;
}
/**
* Autocomplete functionality for entities using field labels.
*/
function eck__bundle__field_autocomplete($entity_type, $bundle, $string = "") {
$field_label = get_bundle_field_label_info($entity_type, $bundle);
$matches = array();
if ($field_label) {
$field = $field_label['field'];
$language = $field_label['language'];
$delta = $field_label['delta'];
$column = $field_label['column'];
$query = new EntityFieldQuery();
// @todo Remove the addmetadata() call below once
// http://drupal.org/node/997394 is fixed.
$query->addMetadata('account', user_load(1));
$query->entityCondition('entity_type', $entity_type->name, '=')->entityCondition('bundle', $bundle->name, '=')->fieldCondition($field, $column, $string, 'CONTAINS');
$results = $query->execute();
$entities = entity_load($entity_type->name, array_keys($results[$entity_type->name]));
foreach ($entities as $id => $entity) {
$matches[$id] = $entity->{$field}[$language][$delta][$column];
}
}
drupal_json_output($matches);
}
/**
* Helper function to determine if an entity wants to define a field as lable.
*/
function get_bundle_field_label_info($entity_type, $bundle) {
$info = entity_get_info();
if (array_key_exists('field label', $info[$entity_type->name]['bundles'][$bundle->name])) {
return $info[$entity_type->name]['bundles'][$bundle->name]['field label'];
}
else {
return NULL;
}
}