· 9 years ago · Feb 02, 2017, 01:14 PM
1Notice: Undefined property: Entity::$bundle_type in EntityAPIController->invoke() (line 342 of /var/www/html/drupal/sites/all/modules/entity/includes/entity.controller.inc).
2
3; General information
4name = KD Element
5version = 7.x-1.0
6description = Module for managing element entities.
7package = KD
8
9; Technical specifications
10core = 7.x
11
12; Dependencies
13dependencies[] = field
14dependencies[] = system
15dependencies[] = entity
16
17/**
18 * @file
19 * The install file for the element entity functionality.
20 */
21
22/**
23 * Implements hook_schema().
24 */
25function kd_element_schema() {
26 $schema = array();
27
28 $schema['element'] = array(
29 'description' => 'The base table for the element entity.',
30 'fields' => array(
31 'element_id' => array(
32 'description' => 'The primary key for the element entity.',
33 'type' => 'serial',
34 'unsigned' => TRUE,
35 'not null' => TRUE,
36 ),
37 'element_title' => array(
38 'description' => 'The title of the element entity.',
39 'type' => 'varchar',
40 'length' => 255,
41 'not null' => TRUE,
42 'default' => '',
43 ),
44 'bundle_type' => array(
45 'description' => 'The bundle type.',
46 'type' => 'text',
47 'size' => 'medium',
48 'not null' => TRUE,
49 ),
50 'element_description' => array(
51 'description' => 'The description of the element.',
52 'type' => 'varchar',
53 'length' => 255,
54 'not null' => TRUE,
55 'default' => '',
56 ),
57 'created' => array(
58 'description' => 'The Unix timestamp of the element creation time.',
59 'type' => 'int',
60 'not null' => TRUE,
61 'default' => 0,
62 ),
63 ),
64 'primary key' => array('element_id'),
65 'foreign keys' => array(
66 'bundle_type' => array(
67 'table' => 'element_type',
68 'columns' => array('bundle_type' => 'type'),
69 ),
70 ),
71 );
72
73 $schema['element_type'] = array(
74 'description' => 'Stores information about element types.',
75 'fields' => array(
76 'id' => array(
77 'type' => 'serial',
78 'not null' => TRUE,
79 'description' => 'The unique identifier for element type.',
80 ),
81 'type' => array(
82 'description' => 'The machine readable name of the element type.',
83 'type' => 'varchar',
84 'length' => 255,
85 'not null' => TRUE,
86 ),
87 'label' => array(
88 'description' => 'The human readable name of this element type.',
89 'type' => 'varchar',
90 'length' => 255,
91 'not null' => TRUE,
92 'default' => '',
93 ),
94
95 // Adding required fields for exportable entities.
96 // @see entity_exportable_schema_fields().
97 'status' => array(
98 'type' => 'int',
99 'not null' => TRUE,
100 // Set the default to ENTITY_CUSTOM without using the constant as it is
101 // not safe to use it at this point.
102 'default' => 0x01,
103 'size' => 'tiny',
104 'description' => 'The exportable status of the entity.',
105 ),
106 'module' => array(
107 'description' => 'The name of the providing module if the entity has been defined in code.',
108 'type' => 'varchar',
109 'length' => 255,
110 'not null' => FALSE,
111 ),
112 ),
113 'primary key' => array('id'),
114 'unique keys' => array('type' => array('type')),
115 );
116
117 return $schema;
118}
119
120/**
121 * @file
122 * Main file for the element entity functionality.
123 */
124
125/**
126 * Implements hook_entity_info().
127 */
128function kd_element_entity_info() {
129 $info = array();
130
131 $info['element'] = array(
132 'label' => t('Element'),
133 'plural label' => t('Elements'),
134 'description' => t('An element entity.'),
135
136 // Use the default entity classes.
137 'entity class' => 'Entity',
138 'controller class' => 'EntityAPIController',
139
140 // The database table to store data in.
141 'base table' => 'element',
142
143 // Default entity callbacks.
144 'uri callback' => 'entity_class_uri',
145 'label callback' => 'entity_class_label',
146
147 'fieldable' => TRUE,
148
149 // Enable caching of the element during the same page request.
150 'static cache' => TRUE,
151
152// 'load hook' => 'entity_load',
153
154 'entity keys' => array(
155 'id' => 'element_id',
156 'bundle' => 'bundle_type',
157 'label' => 'element_title',
158 ),
159
160 // Bundles.
161 'bundles' => array(),
162 'bundle keys' => array(
163 'bundle' => 'bundle_type',
164 ),
165
166 // Parameters for displaying a user interface for the entity.
167 'admin ui' => array(
168 // The hook_menu() path for the UI.
169 'path' => 'admin/content/element',
170 // File in which the edit form resides.
171 'file' => 'kd_element.admin.inc',
172 ),
173
174 // Required access callback for the admin ui menu defined earlier.
175 'access callback' => 'kd_element_access',
176 'module' => 'kd_element',
177 );
178
179 // The element type (used to save bundles of elements).
180 $info['element_type'] = array(
181 'label' => t('Element Type'),
182 // Default controller class.
183 'controller class' => 'EntityAPIController',
184 // Default entity class.
185 'entity class' => 'Entity',
186
187 // Base table for saving the entity types.
188 'base table' => 'element_type',
189 'uri callback' => 'entity_class_uri',
190 'entity keys' => array(
191 'id' => 'id',
192 'name' => 'type',
193 'label' => 'label',
194 ),
195 'fieldable' => FALSE,
196 'exportable' => TRUE,
197 'bundle of' => 'element',
198 'module' => 'kd_element',
199 // Enable the entity API's admin UI.
200 'admin ui' => array(
201 'path' => 'admin/structure/element_types',
202 'controller' => 'EntityDefaultUIController',
203 'file' => 'kd_element.admin.inc',
204 ),
205 'access callback' => 'element_type_access',
206 );
207
208 return $info;
209}
210
211/**
212 * Access callback for element.
213 */
214function kd_element_access($op, $entity_example = NULL, $account = NULL) {
215 return user_access('administer elements');
216}
217
218/**
219 * Determines whether the given user can perform actions on an element type.
220 *
221 * @param string $op
222 * The operation being performed. One of 'view', 'update', 'create' or
223 * 'delete'.
224 * @param mixed $entity
225 * The entity in question. Can be NULL.
226 * @param mixed $account
227 * The account to check against. In the case of the entity API, this is always
228 * NULL.
229 * @param string $entity_type
230 * The element type of the element to check for.
231 *
232 * @return bool
233 * Whether access is allowed or not.
234 */
235function element_type_access($op, $entity = NULL, $account = NULL, $entity_type = 'element_type') {
236 return user_access('administer elements');
237}
238
239/**
240 * Gets an array of all profile types, keyed by the type name.
241 *
242 * @param $type_name
243 * If set, the type with the given name is returned.
244 * @return ProfileType[]
245 * Depending whether $type isset, an array of entity example bundles or a single one.
246 */
247function kd_element_get_element_types($bundle = NULL) {
248 $types = entity_load_multiple_by_name('element_type', isset($bundle) ? array($bundle) : FALSE);
249 return isset($bundle) ? reset($types) : $types;
250}
251
252/**
253 * @file
254 * Element editing UI.
255 *
256 * We make very little use of the EntityAPI interface for this
257 * - preferring instead to use views.
258 * That offers more flexibility to change a UI that will, more often than not,
259 * be end-user facing.
260 */
261
262function element_type_form($form, &$form_state, $element_type, $op = 'edit') {
263
264 $form['label'] = array(
265 '#title' => t('Label'),
266 '#type' => 'textfield',
267 '#default_value' => isset($element_type->label) ? $element_type->label : '',
268 );
269
270 // Machine-readable type name.
271 $form['type'] = array(
272 '#type' => 'machine_name',
273 '#default_value' => isset($element_type->type) ? $element_type->type : '',
274 '#machine_name' => array(
275 'exists' => 'kd_element_get_element_types',
276 'source' => array('label'),
277 ),
278 '#description' => t('A unique machine-readable name for this entity_example bundle. It must only contain lowercase letters, numbers, and underscores.'),
279 );
280
281 $form['actions'] = array('#type' => 'actions');
282 $form['actions']['submit'] = array(
283 '#type' => 'submit',
284 '#value' => t('Save entity'),
285 '#weight' => 40,
286 );
287 return $form;
288}
289
290function element_form($form, &$form_state, $element, $op = 'edit') {
291
292 $form['label'] = array(
293 '#title' => t('Label'),
294 '#type' => 'textfield',
295 '#default_value' => isset($element->label) ? $element->label : '',
296 );
297
298 $form['actions'] = array('#type' => 'actions');
299 $form['actions']['submit'] = array(
300 '#type' => 'submit',
301 '#value' => t('Save entity'),
302 '#weight' => 40,
303 );
304
305 return $form;
306}
307
308/**
309 * Form API submit callback for the type form.
310 */
311function element_type_form_submit(&$form, &$form_state) {
312 $element_type = entity_ui_form_submit_build_entity($form, $form_state);
313 // Save and go back.
314 $element_type->save();
315 // Matching bundle entity 'admin ui' -> 'path' on hook_entity_info().
316 $form_state['redirect'] = 'admin/structure/element_types';
317}