-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_layout.admin.inc
218 lines (188 loc) · 5.77 KB
/
node_layout.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
<?php
/**
* @file
* Admin forms for the node_layout module.
*/
/**
* Callback for Node Layout edit form.
*
* @param array $form
* @param array $form_state
*
* @return array
* @throws \Exception
*/
function node_layout_edit_form($form = array(), &$form_state) {
// Add the JS needed for Vue and the Draggable component.
backdrop_add_js('https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js', 'external');
backdrop_add_js('https://cdn.jsdelivr.net/npm/[email protected]/Sortable.min.js', 'external');
backdrop_add_js('https://cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/2.15.0/vuedraggable.min.js', 'external');
// Add Bootstrap for CSS and modal.
backdrop_add_js('https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js', 'external');
backdrop_add_js('https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js', 'external');
backdrop_add_css('https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css', 'external');
// Include the base URL to use in fetching a node layout list later.
global $base_url;
backdrop_add_js(array(
'node_layout' => array(
'baseURL' => $base_url
)
), 'setting');
// Since the route doesn't already have the node loaded, we need to parse the
// path output, e.g. "node/3/layout".
// @todo Add error handling around different path outcomes.
$nid = explode('/', current_path())[1];
$node = node_load($nid);
$form_state['_nid'] = $nid;
// Each node type will have different layout settings.
$regions = node_layout_get_active_regions($node->type);
$layout = node_layout_get_layout($nid);
$data = nl_edit_form_prepare_data($layout, $regions, $nid);
$form_state['_layout'] = $layout;
$form['node_layout'] = array(
'#type' => 'fieldset',
// '#title' => t('Node Layout'),
'#collapsible' => FALSE,
);
$form['node_layout']['vue_form'] = array(
'#markup' => theme('node_layout_edit_layout', array('data' => $data)),
);
$form['_final_layout'] = array(
'#type' => 'hidden',
'#default_value' => array(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/**
* Submission function for saving node layout.
*
* @param $form
* @param $form_state
*/
function node_layout_edit_form_submit(&$form, &$form_state) {
$submitted_layout = json_decode($form_state['values']['_final_layout']);
if ($form_state['_layout'] !== FALSE) {
$entity = entity_load('node_layout', $form_state['_layout']->id);
$entity->data = serialize($submitted_layout);
}
else {
$values = array();
$values['nid'] = $form_state['_nid'];
$values['layout'] = $submitted_layout;
$entity = entity_create('node_layout', $values);
}
try {
$entity->save();
} catch (EntityStorageException $e) {
watchdog('node_layout', $e->getMessage());
}
}
// @todo Refactor so that this function is included in generating a layout entity.
/**
* Prepare the layout data with placeholders, if needed.
*
* @param $layout
* @param $regions
* @param $nid
*
* @return array
*/
function nl_edit_form_prepare_data($layout, $regions, $nid) {
if ($layout === FALSE) {
$layout->data = array(
'nid' => $nid,
'regions' => array_map(function($region) {
return array(
'name' => $region,
'references' => array(
array(
'nid' => NULL,
'title' => 'placeholder',
'type' => NULL,
),
),
);
}, $regions),
);
$data = array(
'encoded_layout' => json_encode($layout->data),
'layout' => $layout,
);
} else {
$data = array(
'encoded_layout' => json_encode(unserialize($layout->data)),
'layout' => $layout,
);
}
return $data;
}
/**
* Check whether the layout tab should show on the node/% paths.
*
* @return bool|null
*/
function node_layout_edit_access_callback() {
// @todo Add user access check.
$nid = explode('/', current_path())[1];
$node = node_load($nid);
if (isset($node->type)) {
return node_layout_is_type_available($node->type);
}
}
/**
* Settings form for configuring the node layout module.
*
* @param array $form
* @param $form_state
*
* @return mixed|string
*/
function node_layout_settings_form($form = array(), &$form_state) {
$form['#config'] = 'node_layout.settings';
$config = config_get('node_layout.settings');
// Get all node types excluding pageless ones.
$available_node_types = array_filter(node_type_get_types(), function ($val) {
return $val->settings['hidden_path'] === FALSE;
});
if (!$available_node_types) {
return 'No available node types to select from.';
}
$form['node_types'] = array(
'#type' => 'fieldset',
'#title' => 'Available Node Types',
'#description' => 'Select node types to configure settings for them.',
);
$form['node_types']['available_node_types'] = array(
'#type' => 'checkboxes',
'#options' => array_combine(
array_keys($available_node_types),
array_keys($available_node_types)
),
'#default_value' => $config['available_node_types'],
);
// Get the node layout.
$node_layout = layout_load('default');
// Get the template associated with the node layout.
// the template has the regions that can be selected.
$template = layout_get_layout_template_info($node_layout->layout_template);
$template_regions = $template['regions'];
foreach ($config['available_node_types'] as $type) {
if ($type === 0) {
continue;
}
$form[$type] = array(
'#type' => 'fieldset',
'#title' => $type,
);
$form[$type][$type . '_regions'] = array(
'#type' => 'checkboxes',
'#options' => $template_regions,
'#default_value' => $config[$type . '_regions'],
);
}
return system_settings_form($form);
}