This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_block_placement.module
executable file
·478 lines (433 loc) · 15.3 KB
/
menu_block_placement.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/**
* @file
* Custom module to place content on site using menu system
*/
/**
* Implements hook_field_info().
*/
function menu_block_placement_field_info() {
return array(
'menu_block_placement_reference' => array(
'label' => t('Menu Block Placement'),
'description' => t('This field allows the user to use the menu to place blocks on pages and sub pages.'),
'default_widget' => 'menu_block_placement_widget',
'default_formatter' => 'menu_block_placement_formatter',
'settings' => array(
'default_menus' => array(),
'default_selects' => array(
'region' => 'region',
'individ' => 'individ',
),
),
),
);
}
/**
* Implements hook_field_widget_info().
*/
function menu_block_placement_field_widget_info() {
return array(
'menu_block_placement_widget' => array(
'label' => t('Menu Block Placement'),
'field types' => array('menu_block_placement_reference'),
)
);
}
/**
* Implements hook_field_create_field().
*/
function menu_block_placement_field_create_field($field) {
if ($field['type'] == 'menu_block_placement_reference') {
$field['cardinality'] = '-1';
field_update_field($field);
}
}
/**
* The field settings. Allows a user to choose which menus to be included in
* the select list, as well as which additional fields to include
*
* Implements hook_field_settings_form().
*/
function menu_block_placement_field_settings_form($field, $instance) {
$defaults = field_info_field_settings($field['type']);
$settings = array_merge($defaults, $field['settings']);
$options = menu_get_menus();
//choose which select boxes are desired
$form['default_selects'] = array(
'#type' => 'checkboxes',
'#title' => t('Default Select Lists'),
'#options' => array(
'region' => 'Include Region Select List',
'individ' => 'Include \'Individual Pages\' checkbox',
),
'#default_value' => $settings['default_selects'],
'#description' => t('Which select lists to include in the widget.<br>WARNING: These settings can not be altered after data has been entered.'),
);
//choose from the available menus, which to include in the dropdown.
$form['default_menus'] = array(
'#type' => 'checkboxes',
'#title' => t('Default Menu Options'),
'#options' => $options,
'#default_value' => $settings['default_menus'],
'#description' => t('Select which menus will be available to the content editor.<br>WARNING: These settings can not be altered after data has been entered.'),
);
//if data has been entered, disable the default selects field
$form['default_selects']['#disabled'] = field_has_data($field['field_name']);
return $form;
}
/**
* Implements hook_field_widget_form().
*/
function menu_block_placement_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$regions = _mbp_regions();
$options = _menu_block_placement_menu_items($field);
//menu mlid select list
$element['mlid'] = array(
'#type' => 'select',
'#title' => t('Menu Item'),
'#options' => $options,
'#multiple' => $field['cardinality'] == '-1' ? TRUE : FALSE,
'#required' => $instance['required'],
'#default_value' => isset($items[0]['mlid']) ? $items[0]['mlid'] : (isset($instance['default_value'][0]['mlid']) ? $instance['default_value'][0]['mlid'] : ''),
);
//regions select list
if ($field['settings']['default_selects']['region']) {
$element['region'] = array(
'#type' => 'select',
'#title' => t('Region'),
'#options' => $regions,
'#required' => $instance['required'],
'#empty_option' => t('- None -'),
'#default_value' => isset($items[0]['region']) ? $items[0]['region'] : (isset($instance['default_value'][0]['region']) ? $instance['default_value'][0]['region'] : ''),
);
}
//show on individual pages checkbox
if ($field['settings']['default_selects']['individ']) {
$element['individual_pages'] = array(
'#type' => 'checkbox',
'#title' => t('Display only on the selected pages'),
'#default_value' => isset($items[0]['individual_pages']) ? $items[0]['individual_pages'] : (isset($instance['default_value'][0]['individual_pages']) ? $instance['default_value'][0]['individual_pages'] : 0),
);
}
return $element;
}
/**
* Manipulates a single field's data to simulate multiple entries.
* This helps to save each menu item into each row when multiple menu items are chosen
*
* Implement hook_field_presave().
*/
function menu_block_placement_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
$data = array();
if (is_array($items[0]['mlid'])) {
foreach ($items[0]['mlid'] as $mlid) {
$data[] = array(
'mlid' => $mlid,
'region' => isset($items[0]['region']) ? $items[0]['region'] : NULL,
'individual_pages' => isset($items[0]['individual_pages']) ? $items[0]['individual_pages'] : NULL,
);
}
$items = $data;
}
}
/**
* Implements hook_field_formatter_info().
*/
function menu_block_placement_field_formatter_info() {
return array(
'menu_block_placement_formatter' => array(
'label' => t('All Data'),
'field types' => array('menu_block_placement_reference'),
),
'menu_block_placement_mlid' => array(
'label' => t('Menu Items'),
'field types' => array('menu_block_placement_reference'),
),
'menu_block_placement_region' => array(
'label' => t('Region'),
'field types' => array('menu_block_placement_reference'),
),
'menu_block_placement_ind_pages' => array(
'label' => t('Individual Pages'),
'field types' => array('menu_block_placement_reference'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function menu_block_placement_field_formatter_view($entity_type, $entity, &$field, $instance, $langcode, $items, $display) {
$menu_array = _menu_block_placement_menu_items($field);
$regions = _mbp_regions();
$output = array();
$element = array();
switch ($display['type']) {
case 'menu_block_placement_formatter':
//creates and structures an array for easier manipulation
foreach ($items as $item) {
foreach ($menu_array as $menu) {
if (isset($menu[$item['mlid']])) {
$output['mlid'][$item['mlid']] = trim($menu[$item['mlid']], "-\t\n\r\0\x0B");
}
}
}
isset($items[0]['region']) ? $output['region'] = (isset($regions[$items[0]['region']]) ? $regions[$items[0]['region']] : '') : '';
isset($items[0]['individual_pages']) ? $output['individual_pages'] = ($items[0]['individual_pages'] ? 'True' : 'False') : '';
//develops the html output of the data in a simple form
$html_output = !empty($output['mlid']) ? 'Menu Items: <ul><li>' : '';
$html_output .= !empty($output['mlid']) ? implode('</li><li>', $output['mlid']) : '';
$html_output .= !empty($output['mlid']) ? '</li></ul>' : '';
$html_output .= !empty($output['region']) ? 'Region: ' . $output['region'] . '<br>' : '';
$html_output .= !empty($output['individual_pages']) ? 'Show on individual pages: ' . $output['individual_pages'] . '<br>' : '';
$element = array(
array(
'#markup' => $html_output,
),
);
break;
case 'menu_block_placement_mlid':
//creates and structures an array for easier manipulation
foreach ($items as $item) {
foreach ($menu_array as $menu) {
if (isset($menu[$item['mlid']])) {
$output[] = trim($menu[$item['mlid']], "-\t\n\r\0\x0B");
}
}
}
foreach ($output as $delta => $menu) {
$element[$delta] = array(
'#markup' => $menu,
);
}
break;
case 'menu_block_placement_region':
if (!empty($items[0]['region'])) {
$element[] = array('#markup' => $regions[$items[0]['region']]);
}
break;
case 'menu_block_placement_ind_pages':
if (isset($items[0]['individual_pages'])) {
$element[] = $items[0]['individual_pages'] ? 'True' : 'False';
}
break;
}
return $element;
}
/**
* Removes options from the "Number of values' that aren't compatible with this field
*
* Implements hook_form_FORM_ID_alter().
*/
function menu_block_placement_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
if ($form['#field']['type'] == 'menu_block_placement_reference') {
$form['field']['cardinality']['#options'] = array(
-1 => 'Unlimited',
1 => 1
);
}
}
/**
* If the form contains a MBP field, adds that field name to the form state to be used in the form alter
*
* Implements hook_field_widget_form_alter().
*/
function menu_block_placement_field_widget_form_alter(&$element, &$form_state, $context) {
if (isset($element['mlid'])) {
$mbp_fields = isset($form_state['mbp_fields']) ? $form_state['mbp_fields'] : array();
!in_array($element['#field_name'], $mbp_fields) ? $mbp_fields[] = $element['#field_name'] : '';
$form_state['mbp_fields'] = $mbp_fields;
unset($element['_weight']);
}
}
/**
* Since the field must be set to unlimited items, we have to alter the form to hide duplicated
* field widgets. This will also pull in the correct data for the field.
*
* Implements hook_form_alter().
*/
function menu_block_placement_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#entity_type']) && $form['#entity_type'] != 'node') {
unset($form['fields']['_add_new_field']['type']['#options']['menu_block_placement_reference']);
foreach (field_info_fields() as $field) {
if ($field['type'] == 'menu_block_placement_reference') {
unset($form['fields']['_add_existing_field']['field_name']['#options'][$field['field_name']]);
}
}
}
if (isset($form_state['mbp_fields'])) {
foreach ($form_state['mbp_fields'] as $field) {
//$lang = $form[$field]['#language'];
//mimick a single field instead of unlimited fields
$form[$field][LANGUAGE_NONE]['#cardinality'] = 1;
//unsets all repeated field settings
foreach ($form[$field][LANGUAGE_NONE] as $key => $value) {
if (isset($value['mlid']) && $key != 0) {
unset($form[$field][LANGUAGE_NONE][$key]);
}
}
//unsets add more
unset($form[$field][LANGUAGE_NONE]['add_more']);
//queries the db to find all the mlid's for the current node
$items = array();
//dpm($form);
if (isset($form['nid'])) {
$items = db_select('field_data_' . $field, 'f')
->fields('f', array($field . '_mlid'))
->condition('entity_id', $form['nid']['#value'])
->condition('revision_id', $form['vid'])
->execute();
}
//creates an array from the mlid's for the current node
$form[$field][LANGUAGE_NONE][0]['mlid']['#default_value'] = array();
foreach ($items as $item) {
$item = (array) $item;
$form[$field][LANGUAGE_NONE][0]['mlid']['#default_value'][] = $item[$field . '_mlid'];
}
}
}
}
/**
* Implements hook_node_insert().
*/
function menu_block_placement_node_insert($node) {
_menu_block_placement_reindex($node);
}
/**
* Implements hook_node_update().
*/
function menu_block_placement_node_update($node) {
_menu_block_placement_reindex($node);
}
/**
* Implements hook_node_delete().
*/
function menu_block_placement_node_delete($node) {
_menu_block_placement_reindex($node);
}
/**
* @param $node
*
* Function to flag nodes for reindex when a node changes.
*/
function _menu_block_placement_reindex($node) {
if (db_table_exists('search_dataset')) {
$nids = array();
foreach ($fields = field_info_field_map() as $key => $field) {
if ($field['type'] == 'menu_block_placement_reference' && isset($node->$key)) {
$field_data = $node->$key;
foreach ($field_data[LANGUAGE_NONE] as $delta => $value) {
$mlid = $value['mlid'];
$menu_item = menu_link_load($mlid);
if (strpos($menu_item['link_path'], 'node') !== FALSE) {
$path = explode('/', $menu_item['link_path']);
$nids[] = end($path);
}
}
}
}
foreach ($nids as $nid) {
cache_clear_all('field:node:' . $nid, 'cache_field');
db_update('search_dataset')
->condition('sid', $nid)
->fields(array('reindex' => time()))
->execute();
}
}
}
/**
* Implements hook_field_widget_error().
*/
function menu_block_placement_field_widget_error($element, $error, $form, &$form_state) {
form_error($element, $error['message']);
}
/**
* Implements hook_field_is_empty().
*/
function menu_block_placement_field_is_empty($item, $field) {
return !is_array($item) ? TRUE : FALSE;
}
/**
* Implement hook_views_api().
*/
function menu_block_placement_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'menu_block_placement') . '/includes',
);
}
/**
* @param $field
* @return array
* Returns the available menus for the specific field.
*/
function _menu_block_placement_menu_items($field = array(), $menus = NULL) {
$options = array();
if (isset($field["settings"]["default_menus"])) {
$default_menus = $field["settings"]["default_menus"];
foreach ($default_menus as $key => $value) {
if ($value === 0) {
unset($default_menus[$key]);
}
}
$options = array();
$menu_items = menu_parent_options($default_menus, array("mlid" => 0));
foreach ($menu_items as $key => $value) {
$exploded_key = explode(":", $key);
$menu_machine_name = ucwords(str_replace("-", " ", array_shift($exploded_key)));
$menu_id = array_pop($exploded_key);
if ($menu_id != 0) {
$value = substr($value, 0, 2) == '--' ? substr($value, 2) : $value;
$options[$menu_machine_name][$menu_id] = str_replace(array(
"<",
">"
), "", $value);
}
}
}
else {
if (!is_null($menus)) {
$options = array();
$menu_items = menu_parent_options($menus, array("mlid" => 0));
foreach ($menu_items as $key => $value) {
$exploded_key = explode(":", $key);
$menu_machine_name = ucwords(str_replace("-", " ", array_shift($exploded_key)));
$menu_id = array_pop($exploded_key);
if ($menu_id != 0) {
$value = substr($value, 0, 2) == '--' ? substr($value, 2) : $value;
$options[$menu_machine_name][$menu_id] = str_replace(array(
"<",
">"
), "", $value);
}
}
}
}
return $options;
}
/**
* Helper function to set the cache for available regions in the select list.
*/
function _mbp_regions() {
if (!$cache = cache_get('mbp_regions')) {
$available_regions = array();
$theme_settings = system_get_info('theme', variable_get('theme_default', NULL));
if (isset($theme_settings['settings']['menu_block_placement'])) {
foreach ($theme_settings['settings']['menu_block_placement'] as $key => $value) {
if ($value && isset($theme_settings['regions'][$key])) {
$available_regions[$key] = $theme_settings['regions'][$key];
}
}
}
//if no block placement regions are labeled in the info file, all regions will be displayed in the dropdown
else {
if (isset($theme_settings['regions'])) {
$available_regions = $theme_settings['regions'];
}
}
//sets the cache for use in the field.
cache_set('mbp_regions', $available_regions);
$cache = cache_get('mbp_regions');
}
return $cache->data;
}