This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
menu_link_attributes.module
223 lines (184 loc) · 7.79 KB
/
menu_link_attributes.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Url;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function menu_link_attributes_form_menu_link_content_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$account = \Drupal::currentUser();
$attributes = \Drupal::config('menu_link_attributes.config')->get('attributes') ?: [];
$menu_link = $form_state->getFormObject()->getEntity();
$menu_link_options = $menu_link->link ? $menu_link->link->first()->options : [];
if (!$account->hasPermission('use menu link attributes')) {
$form_state->set('menu_link_options', $menu_link_options);
$form['actions']['submit']['#submit'][] = 'menu_link_attributes_menu_link_content_form_preserve';
return;
}
$form['options']['attributes'] = [
'#type' => 'details',
'#title' => t('Attributes'),
'#weight' => -3,
'#tree' => TRUE,
];
$form['options']['container_attributes'] = [
'#type' => 'details',
'#title' => t('Container attributes'),
'#description' => t('These attributes are applied to the container element (<code><li></code>) of the menu link.'),
'#weight' => -2,
'#tree' => TRUE,
'#access' => FALSE,
];
$config_path = Url::fromRoute('menu_link_attributes.config')->toString();
$referrer_path = parse_url(\Drupal::request()->headers->get('referer'))['path'];
$coming_from_config = $config_path == $referrer_path;
// Open <details> element if coming from config page.
if ($coming_from_config) {
$form['options']['attributes']['#open'] = TRUE;
}
$destination = \Drupal::destination()->getAsArray();
$config_path = Url::fromRoute('menu_link_attributes.config', [], ['query' => $destination])->toString();
if ($account->hasPermission('administer menu link attributes')) {
if (count($attributes)) {
$form['options']['attributes']['#description'] = '<small>' . t('Manage available attributes <a href="@config">here</a>.', ['@config' => $config_path]) . '</small>';
}
else {
$form['options']['attributes']['help'] = [
'#markup' => t('Manage available attributes <a href="@config">here</a>.', ['@config' => $config_path]),
];
}
}
$autofocus = FALSE;
// Iterate all defined attributes and create text field for them.
foreach ($attributes as $attribute => $info) {
$is_container_attribute = menu_link_attributes_is_container_attribute($attribute);
$attribute_formatted = $attribute;
$attributes_key = 'attributes';
if ($is_container_attribute) {
$attributes_key = 'container_attributes';
$attribute_formatted = preg_replace('/^container_/', '', $attribute);
}
// Provide default label / description for attributes.
if (empty($info['label'])) {
$info['label'] = str_replace('-', ' ', Unicode::ucfirst($attribute_formatted));
}
if (empty($info['description'])) {
$info['description'] = t('Enter value for <code>@attribute</code> attribute.', ['@attribute' => $attribute_formatted]);
}
// Determine type based on options field.
if (empty($info['type'])) {
$type = !empty($info['options']) ? 'select' : 'textfield';
}
else {
$type = $info['type'];
}
$form['options']['attributes'][$attribute] = [
'#type' => $type,
'#title' => $info['label'],
'#description' => $info['description'],
'#default_value' => isset($menu_link_options[$attributes_key][$attribute_formatted]) ? $menu_link_options[$attributes_key][$attribute_formatted] : '',
];
// Fill options if select list.
if ($type == 'select') {
$form['options']['attributes'][$attribute]['#empty_option'] = t('- Select -');
$form['options']['attributes'][$attribute]['#options'] = $info['options'];
}
// Add "autofocus" attribute for first attribute input field
// if coming from config page.
if ($coming_from_config && !$autofocus) {
$form['options']['attributes'][$attribute]['#attributes'] = ['autofocus' => 'autofocus'];
$autofocus = TRUE;
}
}
$form['actions']['submit']['#submit'][] = 'menu_link_attributes_menu_link_content_form_submit';
}
/**
* Submit function for menu add / edit form.
*/
function menu_link_attributes_menu_link_content_form_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\menu_link_content\Form\MenuLinkContentForm $form_object */
$form_object = $form_state->getFormObject();
$menu_link = $form_object->getEntity();
if (!$menu_link->link) {
return;
}
$menu_link_options = $menu_link->link->first()->options ?: [];
$menu_link_attributes = ['attributes' => $form_state->getValue('attributes')];
foreach ($menu_link_attributes['attributes'] as $attribute_name => $attribute_value) {
$is_container_attribute = menu_link_attributes_is_container_attribute($attribute_name);
if ($is_container_attribute) {
unset($menu_link_attributes['attributes'][$attribute_name]);
$attribute_name = preg_replace('/^container_/', '', $attribute_name);
$menu_link_attributes['container_attributes'][$attribute_name] = $attribute_value;
}
}
foreach ($menu_link_attributes as $attribute_group => $attributes) {
$menu_link_attributes[$attribute_group] = array_filter($attributes);
}
$menu_link_attributes = array_filter($menu_link_attributes);
$menu_link_options = array_merge_recursive($menu_link_options, $menu_link_attributes);
if (count($menu_link_attributes)) {
$menu_link->link->first()->options = $menu_link_options;
$menu_link->save();
}
}
/**
* Preserve menu link attributes on form submit,
* for users that don't have permissions to edit attributes.
*/
function menu_link_attributes_menu_link_content_form_preserve($form, FormStateInterface $form_state) {
/** @var \Drupal\menu_link_content\Form\MenuLinkContentForm $form_object */
$form_object = $form_state->getFormObject();
$menu_link = $form_object->getEntity();
if (!$menu_link->link) {
return;
}
$menu_link_options = $form_state->get('menu_link_options');
if (!empty($menu_link_options)) {
$menu_link->link->options = $menu_link_options;
$menu_link->save();
}
}
/**
* Helper function to determine if attribute is a container attribute.
*
* @param string $attribute_name
* The name of the attribute.
*
* @return bool
*/
function menu_link_attributes_is_container_attribute($attribute_name) {
$attributes = \Drupal::config('menu_link_attributes.config')->get('attributes') ?: [];
$info = $attributes[$attribute_name];
$possible_container_attribute = strpos($attribute_name, 'container_') === 0;
$disabled_container = isset($info['container']) && !$info['container'];
$possible_container_attribute = $possible_container_attribute && !$disabled_container;
$container_attribute_enabled = isset($info['container']) && $info['container'];
$is_container_attribute = $possible_container_attribute || $container_attribute_enabled;
return $is_container_attribute;
}
/**
* Implements template_preprocess_menu().
*/
function menu_link_attributes_preprocess_menu(&$variables) {
_menu_link_attributes_preprocess_menu_items($variables['items']);
}
/**
* Helper function to recursively set list item attributes.
*/
function _menu_link_attributes_preprocess_menu_items(&$items) {
foreach ($items as $item) {
/** @var \Drupal\Core\Menu\MenuLinkDefault $menu_link */
$menu_link = isset($item['original_link']) ? $item['original_link'] : NULL;
$options = !empty($menu_link) ? $menu_link->getOptions() : NULL;
// Apply container attributes on <li> element.
if ($options && isset($options['container_attributes'])) {
foreach ($options['container_attributes'] as $attribute => $value) {
$item['attributes']->setAttribute($attribute, $value);
}
}
if (!empty($item['below'])) {
_menu_link_attributes_preprocess_menu_items($item['below']);
}
}
}