-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_layout.module
300 lines (259 loc) · 7.81 KB
/
node_layout.module
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
<?php
/**
* @file
* Hooks for node_layout module.
*/
/**
* Implements hook_entity_info().
*/
function node_layout_entity_info() {
$info['node_layout'] = array(
'label' => t('Node Layout'),
'base table' => 'node_layout',
'entity class' => 'NodeLayoutEntity',
'controller class' => 'NodeLayoutEntityController',
'entity keys' => array(
'id' => 'id',
),
'fieldable' => FALSE,
'uuid' => FALSE,
);
return $info;
}
/**
* Implements hook_autoload_info().
*/
function node_layout_autoload_info() {
return array(
'NodeLayoutEntity' => 'src/entity/node_layout.entity.inc',
'NodeLayoutEntityController' => 'src/entity/node_layout.entity.inc',
'NodeLayoutEntityControllerInterface' => 'src/entity/node_layout.entity.inc',
);
}
/**
* Implements hook_menu().
*/
function node_layout_menu() {
$items = array();
$items['node/%/layout'] = array(
'title' => 'Layout',
'description' => 'Edit Node Layout',
'page callback' => 'backdrop_get_form',
'page arguments' => array('node_layout_edit_form'),
// 'access arguments' => array('edit node layouts'),
'access callback' => 'node_layout_edit_access_callback',
'file' => 'node_layout.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['api/node_layouts'] = array(
'page callback' => '_node_layouts_listing',
'access arguments' => 'access content',
'type' => MENU_CALLBACK,
);
$items['admin/config/content/node-layout'] = array(
'title' => 'Node Layout Settings',
'description' => 'Configuration options for the Node Layout module',
'page callback' => 'drupal_get_form',
'page arguments' => array('node_layout_settings_form'),
'access arguments' => array('administer node layout'),
'type' => MENU_NORMAL_ITEM,
'weight' => 0,
'file' => 'node_layout.admin.inc',
);
$items['admin/blocks/add'] = array(
'title' => 'Add Block',
'page callback' => 'et_add_page',
'file' => 'node_layout.pages.inc',
'access arguments' => array('administer node layout'),
'type' => MENU_NORMAL_ITEM,
);
// Need to add each hidden path content type to show up on add block page.
foreach (node_type_get_types() as $type) {
// If the hidden path setting is falsey, then don't add a menu item.
if (!$type->settings['hidden_path']) {
continue;
}
// This is taken from the node module's code for node/add/* meu paths.
$type_url_str = str_replace('_', '-', $type->type);
$items['admin/blocks/add/' . $type_url_str] = array(
'title' => $type->name,
'title callback' => 'check_plain',
'page callback' => 'node_add',
'page arguments' => array($type->type),
'access callback' => 'node_access',
'access arguments' => array('create', $type->type),
'description' => $type->description,
'file' => 'node.pages.inc',
'file path' => backdrop_get_path('module', 'node'),
);
}
return $items;
}
/**
* Implements hook_theme().
*/
function node_layout_theme($existing, $type, $theme, $path) {
return array(
'node_layout_edit_layout' => array(
'variables' => array('data' => NULL),
'template' => 'templates/edit_layout'
),
);
}
/**
* Implements hook_block_info().
*/
function node_layout_block_info() {
$blocks = array();
// Combine arrays of regions so that as long as one node layout uses it, the
// region is included.
$regions = node_layout_get_active_regions();
foreach ($regions as $region) {
$blocks["node_layout_$region"] = array(
'info' => "Node Layout Block: $region",
'description' => t("Layout block used for $region region."),
// @todo Add caching strategy.
// 'cache' => BACKDROP_NO_CACHE,
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function node_layout_block_view($delta = '', $settings = array()) {
// @todo Figure out why I had to add this?
if (strpos(current_path(), '/admin/blocks/add')) {
return;
}
// @todo Add better check for exclusion so that future calls won't run through
// the logic if it is determined the layout isn't needed for the current route.
$item = menu_get_item();
if (strpos($delta, 'node_layout') !== -1 && $item['path'] === 'node/%') {
$block['content'] = NULL;
$node = menu_get_object('node');
$layout = node_layout_get_layout($node->nid);
if ($layout) {
$region_name = explode('node_layout_', $delta)[1];
$data = unserialize($layout->data);
foreach ($data->regions->$region_name->references as $reference) {
if ($reference->nid) {
$block['content'][] = node_view(node_load($reference->nid));
}
}
return $block;
}
}
}
/**
* Gets the layout for the node in question.
*
* @param $nid
*
* @return array|bool|mixed|null
*/
// @todo Refactor so that this function returns a node_layout entity.
function node_layout_get_layout($nid) {
$layout = &backdrop_static(__FUNCTION__);
if ($layout === NULL) {
$layout_id = db_query('SELECT id FROM {node_layout} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
// $layout->regions = unserialize($layout->data);
if ($layout_id) {
$layout = entity_load('node_layout', $layout_id);
} else {
// $values = array();
// $values['nid'] = $nid;
// $values['data'] = $submitted_layout;
// $layout = entity_create('node_layout', $values);
$layout = false;
}
}
return $layout;
}
/**
* Gets active regions the layout tool can use.
*
* @param string $node_type
*
* @return array
*/
function node_layout_get_active_regions(string $node_type = '') {
$config = config_get('node_layout.settings');
// Get the node layout.
// @todo Add to settings page.
// $node_layout = layout_load($config['active_layout']);
if ($node_type === '') {
$node_types = array_filter(array_values(node_layout_get_available_node_types()), function($value) {
return $value !== 0;
});
} else {
$node_types = array($node_type);
}
$regions = array();
foreach ($node_types as $type) {
// @todo Don't merge arrays in a loop.
$regions = array_merge($config[$type. '_regions'], $regions);
}
return array_filter($regions, function ($region) {
return $region !== 0;
});
}
/**
* Returns whether or not the node type can use the layout tool.
*
* @param string $type
*
* @return bool|null
*/
function node_layout_is_type_available($type = '') {
if ($type === '') {
return NULL;
}
return in_array($type, array_values(node_layout_get_available_node_types()), TRUE);
}
/**
* Returns an array of the node types that can use the layout tool.
*
* @return mixed
*/
function node_layout_get_available_node_types() {
$config = config_get('node_layout.settings');
return $config['available_node_types'];
}
/**
* Returns a list of all nodes with type of hidden path.
*/
function _node_layouts_listing() {
// Get query parameters.
$q = backdrop_get_query_parameters();
$nids = null;
if (isset($q['nids'])) {
$nids = json_decode($q['nids']);
}
// Query node table based on hidden path settings.
$enabled_types = array_filter(node_type_get_types(), function($val) {
return $val->settings['hidden_path'];
});
$types_names = array_map(function($val) {
return $val->type;
}, $enabled_types);
if ($nids) {
$results = db_query('SELECT nid, title, type FROM {node} WHERE type IN (:types) AND nid IN (:ids)',
array(':types' => $types_names, ':ids' => $nids))->fetchAll();
} else {
$results = db_query('SELECT nid, title, type FROM {node} WHERE type IN (:types)',
array(':types' => $types_names))->fetchAll();
}
backdrop_json_output(json_encode($results));
exit();
}
/**
* Implements hook_admin_paths().
*/
function node_layout_admin_paths() {
// return array(
// 'node/*/layout' => TRUE,
// 'admin/blocks/add' => TRUE,
// 'admin/blocks/add/*' => TRUE,
// );
}