-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_item_extras.module
237 lines (218 loc) · 7.85 KB
/
menu_item_extras.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
<?php
/**
* @file
* Manage fields for the menu items.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\menu_item_extras\Utility\Utility;
/**
* Implements hook_help().
*/
function menu_item_extras_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the menu_item_extras module.
case 'help.page.menu_item_extras':
$output = [
'#type' => 'container',
'title' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => t('About'),
],
'description' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Provide an additional custom fields which can be used on Menu link'),
],
];
return render($output);
}
}
/**
* Implements hook_entity_type_build().
*/
function menu_item_extras_entity_type_build(array &$entity_types) {
$content_entity = 'menu_link_content';
// Set entity type to be bundled.
/** @var \Drupal\Core\Entity\ContentEntityTypeInterface $mlc */
$mlc = $entity_types[$content_entity];
$mlc->set('bundle_entity_type', 'menu');
$mlc->set('field_ui_base_route', 'entity.menu.edit_form');
// Set entity to be a bundle entity type for previous entity.
/** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $menu */
$menu = $entity_types['menu'];
$menu->set('bundle_of', $content_entity);
}
/**
* Implements hook_entity_base_field_info().
*
* Adds a view mode field for later using it per menu item and manage render
* based on the view mode.
*
* @see hook_entity_base_field_info()
*/
function menu_item_extras_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'menu_link_content') {
$fields['view_mode'] = BaseFieldDefinition::create('string')
->setLabel(t('View mode'))
->setDescription(t('Per item view mode selector.'))
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'menu_item_extras_view_mode_selector_select',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
}
return $fields;
}
/**
* Implements hook_entity_base_field_info_alter().
*
* As mentioned in the description of this hook
* the functionality can be changed.
*
* @TODO: We should follow possible changes.
*
* @see hook_entity_base_field_info_alter()
* @see https://www.drupal.org/node/2346329
*/
function menu_item_extras_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
if ($entity_type->id() === 'menu_link_content' && isset($fields['weight'])) {
$options = $fields["weight"]->getDisplayOptions('view');
// Lets check if field definition is buggy.
if ($options['type'] === 'integer') {
// Yep, this is single reason why view display can't be set. Let's fix it.
$options['type'] = 'number_integer';
// @TODO: This should be fixed in the menu_link_content core module.
$fields['weight']->setDisplayOptions('view', $options);
}
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function menu_item_extras_menu_link_content_presave(EntityInterface $entity) {
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
if (!empty($entity->original) && ($entity->original->getMenuName() !== $entity->getMenuName())) {
/** @var \Drupal\menu_item_extras\Service\MenuLinkContentServiceInterface $menu_links_helper */
$menu_links_helper = \Drupal::service('menu_item_extras.menu_link_content_helper');
$menu_links_helper->cleanupFields($entity);
$entity->set('bundle', $entity->getMenuName());
}
}
/**
* Implements hook_theme().
*/
function menu_item_extras_theme() {
$theme = [];
$theme['menu__extras'] = [
'render element' => 'content',
'base hook' => 'menu',
];
return $theme;
}
/**
* Implements hook_preprocess_block().
*/
function menu_item_extras_preprocess_block(&$variables) {
// Menus are built with #theme 'menu__MENU_NAME' form the the MenuLinkTree
// class. We need to build menus supported by menu_item_extras with the
// default #theme menu, to be able to add suggestions in the good order.
if (isset($variables['content']['#menu_name'])) {
$variables['content']['#theme'] = 'menu';
// Pass region name to the suggestions_menu_alter for
// the region suggestion.
$block_id = $variables['elements']['#id'];
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $block_storage */
$block_storage = \Drupal::entityTypeManager()
->getStorage('block');
/** @var \Drupal\block\BlockInterface $block */
$block = $block_storage->load($block_id);
$variables['content']['#attributes']['data-region'] = $block->getRegion();
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function menu_item_extras_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
if (Utility::checkBundleHasExtraFieldsThanEntity('menu_link_content', $variables['menu_name'])) {
$suggestion_prefix = 'menu__extras';
$menu_name = $variables['menu_name'];
$menu_name = strtr($menu_name, '-', '_');
$suggestions = [];
// Custom suggestions.
$suggestions[] = $suggestion_prefix;
$suggestions[] = $suggestion_prefix . '__' . $menu_name;
// Custom suggestions for the parent region.
if (isset($variables['attributes']['data-region'])) {
$suggestions[] = $suggestion_prefix . '__' . $menu_name . '__' . $variables['attributes']['data-region'];
}
}
return $suggestions;
}
/**
* Implements hook_preprocess_menu().
*/
function menu_item_extras_preprocess_menu(&$variables) {
// We preprocess only menus that has additional fields.
if (Utility::checkBundleHasExtraFieldsThanEntity('menu_link_content', $variables['menu_name'])) {
\Drupal::service('menu_item_extras.menu_link_tree_handler')
->processMenuLinkTree($variables['items']);
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function menu_item_extras_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\menu_link_content\Form\MenuLinkContentForm $form_object */
$form_object = $form_state->getBuildInfo()['callback_object'];
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $entity */
$entity = $form_object->getEntity();
$bundle = $entity->get('menu_name')->getString();
foreach ($form['menu_parent']['#options'] as $option_id => $option) {
if (strpos($option_id, $bundle) !== 0) {
unset($form['menu_parent']['#options'][$option_id]);
}
}
}
/**
* Implements hook_entity_extra_field_info().
*
* Creates pseudo field for managing menu item child in render.
*
* @see hook_entity_extra_field_info()
*/
function menu_item_extras_entity_extra_field_info() {
$fields = [];
foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('menu_link_content')) as $bundle) {
$fields['menu_link_content'][$bundle]['display']['children'] = [
'label' => t('Children'),
'description' => t('Child items position in render.'),
'visible' => FALSE,
];
}
return $fields;
}
/**
* Implements hook_entity_view_alter().
*
* Removes default menu link fields from render.
*
* @see hook_entity_view_alter()
*/
function menu_item_extras_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
// Disable rendering of standard entity fields for menu link content.
if ($entity->getEntityTypeId() === 'menu_link_content') {
$hidden_fields = ['title', 'description', 'weight', 'expanded', 'enabled'];
foreach ($hidden_fields as $hidden_field) {
$build[$hidden_field]['#access'] = FALSE;
}
}
}