-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrap.admin.inc
368 lines (303 loc) · 9.99 KB
/
rap.admin.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
<?php
/**
* @file
* Model editing UI.
*
* We make very little use of the EntityAPI interface for this - preferring instead to use
* views. That offers more flexibility to change a UI that will, more often than not,
* be end-user facing.
*/
/**
* UI controller.
*/
class ModelUIController extends EntityDefaultUIController {
/**
* Overrides hook_menu() defaults. Main reason for doing this is that
* parent class hook_menu() is optimized for entity type administration.
*/
public function hook_menu() {
$items = array();
$id_count = count(explode('/', $this->path));
$wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%' . $this->entityType;
$items[$this->path] = array(
'title' => 'Models',
'description' => 'Add edit and update raps.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file path' => drupal_get_path('module', 'system'),
'file' => 'system.admin.inc',
);
// Change the overview menu type for the list of raps.
$items[$this->path]['type'] = MENU_LOCAL_TASK;
// Change the add page menu to multiple types of entities
$items[$this->path . '/add'] = array(
'title' => 'Add a rap',
'description' => 'Add a new rap',
'page callback' => 'rap_add_page',
'access callback' => 'rap_access',
'access arguments' => array('edit'),
'type' => MENU_NORMAL_ITEM,
'weight' => 20,
'file' => 'rap.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module'])
);
// Add menu items to add each different type of entity.
foreach (rap_get_types() as $type) {
$items[$this->path . '/add/' . $type->type] = array(
'title' => 'Add ' . $type->label,
'page callback' => 'rap_form_wrapper',
'page arguments' => array(rap_create(array('type' => $type->type))),
'access callback' => 'rap_access',
'access arguments' => array('edit', 'edit ' . $type->type),
'file' => 'rap.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module'])
);
}
// Loading and editing rap entities
$items[$this->path . '/rap/' . $wildcard] = array(
'page callback' => 'rap_form_wrapper',
'page arguments' => array($id_count + 1),
'access callback' => 'rap_access',
'access arguments' => array('edit', $id_count + 1),
'weight' => 0,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'rap.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module'])
);
$items[$this->path . '/rap/' . $wildcard . '/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items[$this->path . '/rap/' . $wildcard . '/delete'] = array(
'title' => 'Delete',
'page callback' => 'rap_delete_form_wrapper',
'page arguments' => array($id_count + 1),
'access callback' => 'rap_access',
'access arguments' => array('edit', $id_count + 1),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 10,
'file' => 'rap.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module'])
);
// Menu item for viewing raps
$items['rap/' . $wildcard] = array(
//'title' => 'Title',
'title callback' => 'rap_page_title',
'title arguments' => array(1),
'page callback' => 'rap_page_view',
'page arguments' => array(1),
'access callback' => 'rap_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Create the markup for the add Model Entities page within the class
* so it can easily be extended/overriden.
*/
public function addPage() {
$item = menu_get_item();
$content = system_admin_menu_block($item);
if (count($content) == 1) {
$item = array_shift($content);
drupal_goto($item['href']);
}
return theme('rap_add_list', array('content' => $content));
}
}
/**
* Form callback wrapper: create or edit a rap.
*
* @param $rap
* The rap object being edited by this form.
*
* @see rap_edit_form()
*/
function rap_form_wrapper($rap) {
// Add the breadcrumb for the form's location.
rap_set_breadcrumb();
return drupal_get_form('rap_edit_form', $rap);
}
/**
* Form callback wrapper: delete a rap.
*
* @param $rap
* The rap object being edited by this form.
*
* @see rap_edit_form()
*/
function rap_delete_form_wrapper($rap) {
// Add the breadcrumb for the form's location.
//rap_set_breadcrumb();
return drupal_get_form('rap_delete_form', $rap);
}
/**
* Form callback: create or edit a rap.
*
* @param $rap
* The rap object to edit or for a create form an empty rap object
* with only a rap type defined.
*/
function rap_edit_form($form, &$form_state, $rap) {
// Add the default field elements.
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Model Name'),
'#default_value' => isset($rap->name) ? $rap->name : '',
'#maxlength' => 255,
'#required' => TRUE,
'#weight' => -5,
);
$form['data']['#tree'] = TRUE;
$form['data']['sample_data'] = array(
'#type' => 'checkbox',
'#title' => t('An interesting rap switch'),
'#default_value' => isset($rap->data['sample_data']) ? $rap->data['sample_data'] : 1,
);
// Add the field related form elements.
$form_state['rap'] = $rap;
field_attach_form('rap', $rap, $form, $form_state);
$form['actions'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('form-actions')),
'#weight' => 400,
);
// We add the form's #submit array to this button along with the actual submit
// handler to preserve any submit handlers added by a form callback_wrapper.
$submit = array();
if (!empty($form['#submit'])) {
$submit += $form['#submit'];
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save rap'),
'#submit' => $submit + array('rap_edit_form_submit'),
);
if (!empty($rap->name)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete rap'),
'#suffix' => l(t('Cancel'), 'admin/content/raps'),
'#submit' => $submit + array('rap_form_submit_delete'),
'#weight' => 45,
);
}
// We append the validate handler to #validate in case a form callback_wrapper
// is used to add validate handlers earlier.
$form['#validate'][] = 'rap_edit_form_validate';
return $form;
}
/**
* Form API validate callback for the rap form
*/
function rap_edit_form_validate(&$form, &$form_state) {
$rap = $form_state['rap'];
// Notify field widgets to validate their data.
field_attach_form_validate('rap', $rap, $form, $form_state);
}
/**
* Form API submit callback for the rap form.
*
* @todo remove hard-coded link
*/
function rap_edit_form_submit(&$form, &$form_state) {
$rap = entity_ui_controller('rap')->entityFormSubmitBuildEntity($form, $form_state);
// Save the rap and go back to the list of raps
// Add in created and changed times.
if ($rap->is_new = isset($rap->is_new) ? $rap->is_new : 0){
$rap->created = time();
}
$rap->changed = time();
$rap->save();
$form_state['redirect'] = 'admin/content/raps';
}
/**
* Form API submit callback for the delete button.
*
* @todo Remove hard-coded path
*/
function rap_form_submit_delete(&$form, &$form_state) {
$form_state['redirect'] = 'admin/content/raps/rap/' . $form_state['rap']->rap_id . '/delete';
}
/**
* Form callback: confirmation form for deleting a rap.
*
* @param $rap
* The rap to delete
*
* @see confirm_form()
*/
function rap_delete_form($form, &$form_state, $rap) {
$form_state['rap'] = $rap;
$form['#submit'][] = 'rap_delete_form_submit';
$form = confirm_form($form,
t('Are you sure you want to delete rap %name?', array('%name' => $rap->name)),
'admin/content/raps/rap',
'<p>' . t('This action cannot be undone.') . '</p>',
t('Delete'),
t('Cancel'),
'confirm'
);
return $form;
}
/**
* Submit callback for rap_delete_form
*/
function rap_delete_form_submit($form, &$form_state) {
$rap = $form_state['rap'];
rap_delete($rap);
drupal_set_message(t('The rap %name has been deleted.', array('%name' => $rap->name)));
watchdog('rap', 'Deleted rap %name.', array('%name' => $rap->name));
$form_state['redirect'] = 'admin/content/raps';
}
/**
* Page to add Model Entities.
*
* @todo Pass this through a proper theme function
*/
function rap_add_page() {
$controller = entity_ui_controller('rap');
return $controller->addPage();
}
/**
* Displays the list of available rap types for rap creation.
*
* @ingroup themeable
*/
function theme_rap_add_list($variables) {
$content = $variables['content'];
$output = '';
if ($content) {
$output = '<dl class="rap-type-list">';
foreach ($content as $item) {
$output .= '<dt>' . l($item['title'], $item['href']) . '</dt>';
$output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
}
$output .= '</dl>';
}
else {
if (user_access('administer rap types')) {
$output = '<p>' . t('Model Entities cannot be added because you have not created any rap types yet. Go to the <a href="@create-rap-type">rap type creation page</a> to add a new rap type.', array('@create-rap-type' => url('admin/structure/rap_types/add'))) . '</p>';
}
else {
$output = '<p>' . t('No rap types have been created yet for you to use.') . '</p>';
}
}
return $output;
}
/**
* Sets the breadcrumb for administrative rap pages.
*/
function rap_set_breadcrumb() {
$breadcrumb = array(
l(t('Home'), '<front>'),
l(t('Administration'), 'admin'),
l(t('Content'), 'admin/content'),
l(t('Models'), 'admin/content/raps'),
);
drupal_set_breadcrumb($breadcrumb);
}