forked from zroger/bean
/
bean.pages.inc
157 lines (132 loc) · 3.82 KB
/
bean.pages.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
<?php
/**
* @file
* Bean Functions
*/
/**
* Edit bean page callback
*/
function bean_edit($bean) {
return drupal_get_form('bean_form', $bean);
}
/**
* Add bean page callback
*/
function bean_add($type) {
$bean = bean_create(array('type' => $type));
return drupal_get_form('bean_form', $bean);
}
/**
* Menu Callback to list available bean types to add
*/
function bean_add_page() {
$bean_types = bean_get_types();
// Bypass the block/add listing if only one block type is available.
if (count($bean_types) == 1) {
$bean_type = array_shift($bean_types);
drupal_goto('block/add/' . $bean_type->buildURL());
}
return theme('bean_add_list', array('content' => $bean_types));
}
/**
* Returns HTML for a list of available bean types for bean creation.
*
* @param $variables
* An associative array containing:
* - content: An array of bean types.
*
* @ingroup themeable
*/
function theme_bean_add_list($variables) {
$content = $variables['content'];
$output = '';
if ($content) {
$output = '<dl class="bean-type-list">';
foreach ($content as $item) {
$output .= '<dt>' . l('Add ' . $item->getLabel(), 'block/add/' . str_replace('_', '-', $item->type)) . '</dt>';
}
$output .= '</dl>';
}
else {
//$output = '<p>' . t('You have not created any block types yet. Go to the <a href="@create-content">block type creation page</a> to add a new bean type.', array('@create-content' => url('admin/structure/bean/add'))) . '</p>';
$output = '<p>' . t('You have not created any block types yet.') . '</p>';
}
return $output;
}
/**
* Bean form
*/
function bean_form($form, &$form_state, $bean) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#required' => TRUE,
'#default_value' => $bean->label,
'#description' => t('Name that displays in the admin interface'),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('The Title of the block.'),
'#default_value' => $bean->title,
);
$form['bean'] = array(
'#type' => 'value',
'#value' => $bean,
);
field_attach_form('bean', $bean, $form, $form_state);
// Get the Bean type form
$form += $bean->getForm();
$form['submit'] = array(
'#type' => 'submit',
'#weight' => 200,
'#value' => t('Save'),
);
return $form;
}
/**
* Validation for bean form
*/
function bean_form_validate($form, &$form_state) {
$bean = $form_state['values']['bean'];
$bean->label = $form_state['values']['label'];
$bean->title = $form_state['values']['title'];
field_attach_form_validate('bean', $bean, $form, $form_state);
$form_state['values']['bean'] = $bean;
$bean->validate($form_state['values']);
}
/**
* Submit function for bean form
*/
function bean_form_submit($form, &$form_state) {
$bean = $form_state['values']['bean'];
$bean->setValues($form_state['values']);
field_attach_submit('bean', $bean, $form, $form_state);
$bean->save();
$form_state['redirect'] = $bean->editURL();
}
/**
* Menu callback -- ask for confirmation of bean deletion
*/
function bean_delete_confirm($form, &$form_state, $bean) {
$form['#bean'] = $bean;
return confirm_form($form,
t('Are you sure you want to delete %title?', array('%title' => $bean->label)),
'block/' . $bean->bid,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Execute node deletion
*/
function bean_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$bean = $form_state['values']['bean'];
$bean->delete();
watchdog('block', '@type: deleted %title.', array('@type' => $bean->type, '%title' => $bean->label));
drupal_set_message(t('@type %title has been deleted.', array('@type' => $bean->type, '%title' => $bean->label)));
}
$form_state['redirect'] = '<front>';
}