-
Notifications
You must be signed in to change notification settings - Fork 2
/
eck.bundle.inc
179 lines (153 loc) · 4.99 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
<?php
/**
* @file
* All of the menu, pages, and forms related to bundle administration.
*/
/**
* This function creates the menu items relevant to bundle administration
*
* @param $entity_type
* (String) entity type
*
* This function called is triggered from hook_menu()
* @see eck_menu()
*/
function eck__bundle__menu($entity_type) {
$entity_type_label = eck__get_entity_type_label($entity_type);
// Create the menus relavant to types.
$menu = array();
// OVERVIEW Entity types.
$menu["admin/structure/eck/{$entity_type}"] = array(
'title' => "{$entity_type_label} Bundles" ,
'description' => "View all the bundles for '{$entity_type_label}'",
'page callback' => "eck__bundle__overview",
'page arguments' => array($entity_type),
'access arguments' => array("administer {$entity_type} bundles"),
'weight' => 0,
'file' => 'eck.bundle.inc'
);
$menu["admin/structure/eck/{$entity_type}/add"] = array(
'title' => "Add a(n) '{$entity_type_label}' Bundle ",
'description' => "Add a(n) new '{$entity_type_label} Bundle'",
'page callback' => "drupal_get_form",
'page arguments' => array('eck__bundle__add', $entity_type),
'access arguments' => array("add {$entity_type} bundles"),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'eck.bundle.inc'
);
module_load_include('inc', 'eck', 'eck.entity');
foreach (eck__get_bundles($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 entity_type
* (String) entity type
*/
function eck__bundle__overview($entity_type) {
$header = array(t('Type'), array('data' => t('Operations'), 'colspan' => '1'));
$rows = array();
foreach (eck__get_bundles($entity_type) as $bundle) {
$bundle_label = eck__get_bundle_label($entity_type, $bundle);
$uri = "admin/structure/eck/{$entity_type}/{$bundle}";
$row[] = array(l($bundle_label, url($uri, array('absolute' => TRUE))));
}
$build['bundle_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$build['sample_code'] = array(
'#theme' => 'form_element',
'#title' => t('Optional entity class'),
'#description' => t('You may add this custom class the entity. To create a new instance of this class programmatically call: %code', array(
'%code' => "entity_create('{$entity_type}', array());"
)),
'#children' => "<pre>" . check_plain("class " . eck_get_class_name($entity_type, 'EntityType') . " extends Entity{\n\n}") . "</pre>"
);
return $build;
}
/**
* ADD Entity types.
*
* @param $form
* Form array provided by the Form API
* @param $form_state
* array provided by the Form API
* @param entity_type
* (String) entity type
*/
function eck__bundle__add($form, &$form_state, $entity_type) {
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['bundle_label'] = array(
'#type' => 'textfield',
'#title' => "Type",
'#description' => "A Human readable name for the bundle",
);
$form['bundle'] = array(
'#type' => 'machine_name',
'#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;
}
/**
* Validation for bundle creation (Make sure this bundle don't exist for thie entity type)
*
* @param $form
* Form array provided by the Form API
* @param $form_state
* array provided by the Form API
*/
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 (eck__get_bundles($entity_type) as $bundle) {
if ($bundle == $form_state['values']['bundle']) {
form_set_error('bundle', t("bundle '{$bundle} already exists for this entity"));
}
}
}
/**
* Submit function for add form
*
* @param $form
* Form array provided by the Form API
* @param $form_state
* array provided by the Form API
*/
function eck__bundle__add_submit($form, &$form_state) {
$entity_type = $form_state['values']['entity_type'];
$bundle = $form_state['values']['bundle'];
$bundle_label = $form_state['values']['bundle_label'];
db_insert('eck_types')
->fields(array(
'entity' => $entity_type,
'type' => $bundle,
'label' => $bundle_label,
))
->execute();
drupal_set_message(t('the %bundle for entity type %entity_type has been saved', array('%bundle' => $bundle, '%entity_type' => $entity_type)));
drupal_get_schema(NULL, TRUE);
entity_info_cache_clear();
menu_rebuild();
drupal_goto("admin/structure/eck/{$entity_type}/{$bundle}");
}