This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfarm_asset.pages.inc
257 lines (220 loc) · 6.66 KB
/
farm_asset.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
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
<?php
/**
* @file
* Farm asset pages.
*/
/**
* Asset view callback.
*
* @param FarmAsset $farm_asset
* The farm asset entity.
*
* @return array
* Returns the entity render array.
*/
function farm_asset_view(FarmAsset $farm_asset) {
// Set the page title.
drupal_set_title(entity_label('farm_asset', $farm_asset));
// Set the page breadcrumb.
$breadcrumb = array(
l(t('Home'), '<front>'),
l(t('Farm'), 'farm'),
);
$module_breadcrumbs = module_invoke_all('farm_asset_breadcrumb', $farm_asset);
$breadcrumb = array_merge($breadcrumb, $module_breadcrumbs);
drupal_set_breadcrumb($breadcrumb);
// Build the asset's render array.
$build = entity_view('farm_asset', array(entity_id('farm_asset', $farm_asset) => $farm_asset), 'full');
// Return the render array.
return $build;
}
/**
* Page to select asset type to add new asset.
*/
function farm_asset_add_types_page() {
$items = array();
foreach (farm_asset_types() as $farm_asset_type_key => $farm_asset_type) {
if (farm_asset_access('create', $farm_asset_type)) {
$items[] = l(entity_label('farm_asset_type', $farm_asset_type), 'farm/asset/add/' . $farm_asset_type_key);
}
}
return array(
'list' => array(
'#theme' => 'item_list',
'#items' => $items,
'#title' => t('Select a type of asset to create.'),
),
);
}
/**
* Add new asset page callback.
*
* @param string $type
* The farm asset type.
*
* @return array
* Returns a form array.
*/
function farm_asset_add($type) {
$farm_asset_type = farm_asset_types($type);
$farm_asset = entity_create('farm_asset', array('type' => $type));
drupal_set_title(t('Add @name', array('@name' => entity_label('farm_asset_type', $farm_asset_type))));
$output = drupal_get_form('farm_asset_form', $farm_asset);
return $output;
}
/**
* Asset form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param FarmAsset $farm_asset
* The farm asset entity.
*
* @return array
* Returns a form array.
*/
function farm_asset_form(array $form, array &$form_state, FarmAsset $farm_asset) {
$form['farm_asset'] = array(
'#type' => 'value',
'#value' => $farm_asset,
);
// Load the asset type.
$farm_asset_type = farm_asset_type_load($farm_asset->type);
// Asset name.
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#description' => t('Give this %type a name.', array('%type' => $farm_asset_type->label)),
'#default_value' => $farm_asset->name,
'#required' => TRUE,
'#weight' => -100,
);
// Additional settings (vertical tabs at the bottom of the form).
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
// Asset active/inactive (new assets are active by default).
if (empty($farm_asset->id)) {
$farm_asset->active = TRUE;
}
$form['asset_status'] = array(
'#type' => 'fieldset',
'#title' => t('Asset status'),
'#description' => t('Mark this asset as active/inactive. Inactive assets will not show in most lists, but will be visible in archives.'),
'#collapsible' => TRUE,
'#group' => 'additional_settings'
);
$form['asset_status']['active'] = array(
'#type' => 'checkbox',
'#title' => t('Active'),
'#default_value' => $farm_asset->active,
);
// Asset user id.
$form['uid'] = array(
'#type' => 'value',
'#value' => $farm_asset->uid,
);
field_attach_form('farm_asset', $farm_asset, $form, $form_state);
$submit = array();
if (!empty($form['#submit'])) {
$submit += $form['#submit'];
}
$form['actions'] = array(
'#weight' => 100,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => $submit + array('farm_asset_form_submit'),
);
// Show Delete button if allowed.
$farm_asset_id = entity_id('farm_asset', $farm_asset);
if (!empty($farm_asset_id) && farm_asset_access('delete', $farm_asset)) {
// Get the destination query parameter. If it is the current path, change
// to <front> (because the current path won't exist once the asset is
// deleted).
$destination = drupal_get_destination();
if ($destination['destination'] == current_path()) {
$destination['destination'] = '<front>';
}
$form['actions']['delete'] = array(
'#type' => 'markup',
'#markup' => l(t('Delete'), 'farm/asset/' . $farm_asset_id . '/delete', array('query' => $destination)),
);
}
return $form;
}
/**
* Asset validate handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_asset_form_validate(array $form, array &$form_state) {
}
/**
* Asset submit handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_asset_form_submit(array $form, array &$form_state) {
$farm_asset = $form_state['values']['farm_asset'];
entity_form_submit_build_entity('farm_asset', $farm_asset, $form, $form_state);
farm_asset_save($farm_asset);
$farm_asset_uri = entity_uri('farm_asset', $farm_asset);
$form_state['redirect'] = $farm_asset_uri['path'];
drupal_set_message(t('Asset saved: <a href="@uri">%title</a>', array('@uri' => base_path() . $farm_asset_uri['path'], '%title' => entity_label('farm_asset', $farm_asset))));
}
/**
* Delete confirmation form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
* @param FarmAsset $farm_asset
* The farm asset entity.
*
* @return array
* Returns a form array.
*/
function farm_asset_delete_form(array $form, array &$form_state, FarmAsset $farm_asset) {
$form['farm_asset'] = array(
'#type' => 'value',
'#value' => $farm_asset,
);
// Always provide entity id in the same form key as in the entity edit form.
$form['farm_asset_type_id'] = array(
'#type' => 'value',
'#value' => entity_id('farm_asset', $farm_asset));
$farm_asset_uri = entity_uri('farm_asset', $farm_asset);
return confirm_form($form,
t('Are you sure you want to delete %title?', array('%title' => entity_label('farm_asset', $farm_asset))),
$farm_asset_uri['path'],
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Delete form submit handler.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_asset_delete_form_submit(array $form, array &$form_state) {
$farm_asset = $form_state['values']['farm_asset'];
farm_asset_delete($farm_asset);
drupal_set_message(t('%title was deleted.', array('%title' => entity_label('farm_asset', $farm_asset))));
$form_state['redirect'] = '<front>';
}