-
Notifications
You must be signed in to change notification settings - Fork 1
/
nestedbox_type.admin.inc
111 lines (103 loc) · 3.38 KB
/
nestedbox_type.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
<?php
use Drupal\nestedbox_core\NestedBoxType;
/**
* Generates the nestedbox type editing form.
*
* @param array $form
* @param array $form_state
* @param \Drupal\nestedbox_core\NestedBoxType $nestedbox_type
* @param string $op
*
* @return mixed
*
* @see entity_ui_get_form()
*/
function nestedbox_type_form(
array $form,
/** @noinspection PhpUnusedParameterInspection */ array &$form_state,
NestedBoxType $nestedbox_type,
$op = 'edit'
) {
if ($op === 'clone') {
$nestedbox_type->label .= ' (cloned)';
$nestedbox_type->type = '';
}
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $nestedbox_type->label,
'#description' => t('The human-readable name of this Nested Box type.'),
'#required' => TRUE,
'#size' => 30,
);
// Machine-readable type name.
$form['type'] = array(
'#type' => 'machine_name',
'#default_value' => isset($nestedbox_type->type) ? $nestedbox_type->type : '',
'#maxlength' => 32,
// '#disabled' => $nestedbox_type->isLocked() && $op != 'clone',
'#machine_name' => array(
'exists' => 'nestedbox_get_type',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for this Nested Box type. '
. 'It must only contain lowercase letters, numbers, and underscores.'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Nested Box type'),
'#weight' => 40,
);
if (!empty($nestedbox_type->type) && $op !== 'add') {
if (!$nestedbox_type->hasStatus(ENTITY_IN_CODE)) {
// Entity lives only in the database.
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete nestedbox type'),
'#weight' => 45,
'#limit_validation_errors' => array(),
/* @see nestedbox_type_form_submit_redirect() */
'#submit' => array('nestedbox_type_form_submit_redirect'),
'#redirect_path' => 'admin/structure/nestedbox-types/manage/' . $nestedbox_type->type . '/delete',
);
}
elseif ($nestedbox_type->hasStatus(ENTITY_OVERRIDDEN)) {
// Entity lives in code, and is overridden.
$form['actions']['revert'] = array(
'#type' => 'submit',
'#value' => t('Revert nestedbox type'),
'#weight' => 45,
'#limit_validation_errors' => array(),
/* @see nestedbox_type_form_submit_redirect() */
'#submit' => array('nestedbox_type_form_submit_redirect'),
'#redirect_path' => 'admin/structure/nestedbox-types/manage/' . $nestedbox_type->type . '/revert',
);
}
}
return $form;
}
/**
* Form API submit callback for the type form.
*
* @param array $form
* @param array $form_state
*/
function nestedbox_type_form_submit(array &$form, array &$form_state) {
/** @var \Drupal\nestedbox_core\NestedBoxType $nestedbox_type */
$nestedbox_type = entity_ui_form_submit_build_entity($form, $form_state);
$nestedbox_type->save();
$form_state['redirect'] = 'admin/structure/nestedbox-types';
}
/**
* Form API submit callback for the delete or revert button.
*
* @param array $form
* @param array $form_state
*/
function nestedbox_type_form_submit_redirect(
/** @noinspection PhpUnusedParameterInspection */ array &$form,
array &$form_state
) {
$form_state['redirect'] = $form_state['clicked_button']['#redirect_path'];
}