-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_manager.module
More file actions
executable file
·363 lines (347 loc) · 12.1 KB
/
patch_manager.module
File metadata and controls
executable file
·363 lines (347 loc) · 12.1 KB
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
<?php
/**
* @file
* Patch manager provides developers with tools for managing patches.
*/
/**
* Implementation of hook_entity_info().
*/
function patch_manager_entity_info() {
$entities = array();
$entities['patch_manager_metaobject'] = array(
'label' => t('Metaobject'),
'entity class' => 'Metaobject',
'controller class' => 'MetaobjectController',
'base table' => 'patch_manager_metaobject',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
'label' => 'label',
'bundle' => 'type',
),
// Bundles are defined below.
'bundles' => array(),
// Bundle keys tell the FieldAPI how to extract information from the bundle objects.
'bundle keys' => array(
'bundle' => 'type',
),
'module' => 'patch_manager',
'admin ui' => array(
'controller class' => 'MetaobjectUIController',
'path' => 'admin/structure/metaobjects',
),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
),
'static cache' => TRUE,
'configuration' => TRUE,
'exportable' => TRUE,
'access callback' => 'patch_manager_metaobject_access',
// @todo Remove it.
//'features controller class' => 'MetaobjectFeaturesController',
);
// The entity that holds information about the entity types.
$entities['patch_manager_metaobject_type'] = array(
'label' => t('Metaobject Type'),
'entity class' => 'MetaobjectType',
'controller class' => 'MetaobjectTypeController',
'base table' => 'patch_manager_metaobject_type',
'fieldable' => TRUE,
'bundle of' => 'patch_manager_metaobject',
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
// Bundle keys tell the FieldAPI how to extract information from the bundle objects.
'bundle keys' => array(
'bundle' => 'type',
),
//'label callback' => 'entity_class_label',
//'uri callback' => 'entity_class_uri',
//'creation callback' => 'patch_manager_metaobject_type_create',
'module' => 'patch_manager',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/metaobject_types',
//'file' => 'metaobject_type.admin.inc',
'controller class' => 'MetaobjectTypeUIController',
),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
),
'static cache' => TRUE,
'configuration' => TRUE,
'exportable' => TRUE,
'access callback' => 'patch_manager_metaobject_type_access',
// @todo Remove it.
//'features controller class' => 'MetaobjectTypeFeaturesController',
//'metadata controller class' => 'EntityformTypeMetadataController',
);
return $entities;
}
/**
* Implements hook_entity_info_alter().
*
* We are adding the info about the metaobject types via a hook to avoid a recursion
* issue as loading the entityform types requires the entity info as well.
*\/ @todo Remove it: though it requires patch from http://drupal.org/node/1332004
function patch_manager_entity_info_alter(&$entity_info) {
foreach (patch_manager_get_metaobject_types() as $type => $bundle) {
$entity_info['patch_manager_metaobject']['bundles'][$type] = array(
'label' => $bundle->label,
'admin' => array(
'path' => 'admin/structure/metaobject_types/manage/%entity_type',
'real path' => 'admin/structure/metaobject_types/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array('administer metaobjects'),
),
);
}
}
*
*/
/**
* Implements hook_permission().
*/
function patch_manager_permission() {
$permissions = array(
'administer metaobjects' => array(
'title' => t('Administer metaobjects'),
'description' => t('Create and delete fields for metaobject types.'),
),
);
return $permissions;
}
// @todo Implement real entity controllers.
class MetaobjectController extends EntityAPIControllerExportable {}
class Metaobject extends Entity {}
class MetaobjectUIController extends EntityBundlesUIController {
/**
* Overrides overviewTableHeaders().
*/
protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
$header = parent::overviewTableHeaders($conditions, $rows, $additional_header);
// Insert column as second for metaobject type.
$first = array_shift($header);
array_unshift($header, t('Metaobject type'));
array_unshift($header, $first);
return $header;
}
protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
$row = parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
// Insert column as second for metaobject type.
$first = array_shift($row);
array_unshift($row, $entity->bundle());
array_unshift($row, $first);
return $row;
}
}
class MetaobjectTypeController extends EntityAPIControllerExportable {}
class MetaobjectType extends Entity {}
class MetaobjectTypeUIController extends EntityDefaultUIController {
/**
* Overrides hook_menu() defaults.
*/
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['description'] = 'Manage metaobject entity types, including adding and removing fields and the display of fields.';
$items[$this->path]['type'] = MENU_NORMAL_ITEM;
// @todo Describe what is it for.
$wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
$items["{$this->path}/manage/{$wildcard}/edit"]['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
return $items;
}
}
/**
* Create a metaobject type object.
*\/ @todo Remove it.
function patch_manager_metaobject_type_create($values = array()) {
return entity_get_controller('patch_manager_metaobject_type')->create($values);
}
*
*/
/**
* Implements hook_default_metaobject_configuration().
*/
function patch_manager_default_metaobject_configuration() {
// @todo Create default meta configuration.
$meta_object_configs = array();
return $meta_object_configs;
}
function patch_manager_metaobject_access() {
// @todo Implement better access handling.
return TRUE;
}
function patch_manager_metaobject_type_access() {
// @todo Implement better access handling.
return TRUE;
}
/**
* Generates the metaobject editing form.
*/
function patch_manager_metaobject_form($form, &$form_state, $metaobject, $op = 'edit') {
if ($op == 'clone') {
$metaobject->label .= ' (cloned)';
$metaobject->name .= '_clone';
}
// @todo Remove label for Method metaobject, leave for Module.
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => isset($metaobject->label) ? $metaobject->label : '',
);
// Machine-readable application id.
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => isset($metaobject->name) ? $metaobject->name : '',
'#machine_name' => array(
'exists' => 'patch_manager_get_metaobjects',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for this metaobject. It must only contain lowercase letters, numbers, and underscores.'),
);
// Add other fields.
field_attach_form('patch_manager_metaobject', $metaobject, $form, $form_state);
// Add submit button.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save metaobject'),
'#weight' => 40,
);
return $form;
}
/**
* Generates the metaobject type editing form.
*/
function patch_manager_metaobject_type_form($form, &$form_state, $metaobject_type, $op = 'edit') {
if ($op == 'clone') {
$metaobject_type->label .= ' (cloned)';
$metaobject_type->name .= '_clone';
}
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => isset($metaobject_type->label) ? $metaobject_type->label : '',
);
// Machine-readable application id.
$form['type'] = array(
'#type' => 'machine_name',
'#default_value' => isset($metaobject_type->type) ? $metaobject_type->type : '',
// @todo ?Do we need it?
//'#disabled' => $application->isLocked(),
'#machine_name' => array(
'exists' => 'patch_manager_get_metaobject_types',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for this metaobject type. It must only contain lowercase letters, numbers, and underscores.'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save metaobject type'),
'#weight' => 40,
);
return $form;
}
function patch_manager_get_metaobjects($metaobject_name = NULL) {
// @todo Implement existence check.
return array();
}
/**
* Get metaobject types.
*
* @param $metaobject_type_name
* The machine-readable name of a entityform type to load. If NULL - loads all.
* @param boolean $reset
* Whether to reset or not cached metaobject types.
* @return
* A metaobject type array, single metaobject type or FALSE if metaobject type
* called $metaobject_type_name does not exist.
*/
function patch_manager_get_metaobject_types($metaobject_type_name = NULL, $reset = FALSE) {
// entity_load will get the Entity controller for our entityform entity and call the load
// function of that object - we are loading entities by name here.
$metaobject_types = &drupal_static(__FUNCTION__, array(), $reset);
if (empty($metaobject_types) || (isset($metaobject_type_name) && empty($metaobject_types[$metaobject_type_name]))) {
if (!isset($metaobject_type_name)) {
$metaobject_types = entity_load_multiple_by_name('patch_manager_metaobject_type', FALSE);
}
else {
$types = entity_load_multiple_by_name('patch_manager_metaobject_type', array($metaobject_type_name));
if (empty($types)) {
return FALSE;
}
// @todo Describe why it is necessary.
$metaobject_types[$metaobject_type_name] = array_shift($types);
}
}
return isset($metaobject_type_name) ? $metaobject_types[$metaobject_type_name] : $metaobject_types;
}
/**
* Form API submit callback for the metaobject form.
*/
function patch_manager_metaobject_form_submit(&$form, &$form_state) {
$metaobject = entity_ui_form_submit_build_entity($form, $form_state);
// Save and go back.
$metaobject->save();
$form_state['redirect'] = 'admin/structure/metaobjects';
}
/**
* Form API submit callback for the metaobject type form.
*/
function patch_manager_metaobject_type_form_submit(&$form, &$form_state) {
$metaobject_type = entity_ui_form_submit_build_entity($form, $form_state);
// Save and go back.
$metaobject_type->save();
$form_state['redirect'] = 'admin/structure/metaobject_types';
}
/**
* Implements hook_admin_menu_map().
*/
function patch_manager_admin_menu_map() {
$map = array();
if (!user_access('administer metaobjects')) {
return;
}
// As Node does for Content Types.
// @see node_admin_menu_map()
$map['admin/structure/metaobject_types/manage/%entity_type'] = array(
'parent' => 'admin/structure/metaobject_types',
'arguments' => array(
array('%entity_type' => array_keys(patch_manager_get_metaobject_types())),
),
);
// As Field UI does.
// @see field_ui_admin_menu_map()
// Invoke general function to gather all alterations.
foreach (entity_get_info() as $obj_type => $info) {
if (in_array($obj_type, array_keys(patch_manager_entity_info()))) {
$arguments = array();
foreach ($info['bundles'] as $bundle_name => $bundle_info) {
if (isset($bundle_info['admin'])) {
$fields = array();
foreach (field_info_instances($obj_type, $bundle_name) as $field) {
$fields[] = $field['field_name'];
}
$arguments = array(
'%entity_type' => array($bundle_name),
'%field_ui_menu' => $fields,
);
$path = $bundle_info['admin']['path'];
$map["$path/fields/%field_ui_menu"]['parent'] = "$path/fields";
$map["$path/fields/%field_ui_menu"]['arguments'][] = $arguments;
}
}
}
}
return $map;
}