-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroup_menu.module
205 lines (179 loc) · 7.86 KB
/
group_menu.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
<?php
/**
* @file
* Enables menu functionality for the group module.
*/
use Drupal\group\Entity\GroupContentType;
use Drupal\group_menu\Form\GroupMenuConfigSync;
use Drupal\system\MenuInterface;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_form_FORM_ID_alter().
*/
function group_menu_form_config_export_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add submit handler if proper configuration is set.
$config = \Drupal::config('group_menu.settings');
if ($config->get('config_sync_group_menu_ignore')) {
$form['#submit'][] = 'group_menu_config_export_form_submit';
}
}
/**
* Submit callback for config_export_form form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $form_id
* String representing the name of the form itself.
*/
function group_menu_config_export_form_submit($form, FormStateInterface $form_state, $form_id) {
$form_state->setRedirect('group_menu.config.export_download');
}
/**
* Implements hook_entity_type_alter().
*/
function group_menu_entity_type_alter(array &$entity_types) {
// Use a different list builder to exclude group menus.
$config = \Drupal::config('group_menu.settings');
if ($config->get('group_menu_hide_list')) {
$entity_types['menu']->setHandlerClass('storage', 'Drupal\group_menu\Entity\GroupMenuEntityStorage');
$entity_types['menu']->setHandlerClass('list_builder', 'Drupal\group_menu\GroupMenuListBuilder');
}
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
// Add the menu creation wizard steps as entity forms.
$entity_types['menu']->setFormClass('group_menu-form', 'Drupal\group_menu\Form\GroupMenuFormStep1');
$entity_types['group_menu']->setFormClass('group_menu-form', 'Drupal\group_menu\Form\GroupMenuFormStep2');
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function group_menu_menu_insert(MenuInterface $menu) {
\Drupal::service('plugin.manager.group_content_enabler')->clearCachedDefinitions();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function group_menu_form_menu_link_content_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$build_info = $form_state->getBuildInfo();
$entity = $build_info['callback_object']->getEntity();
$form['actions']['delete']['#url'] = Url::fromRoute('entity.group_menu.menu_link_content.delete_form', ['menu_link_content' => $entity->id()], [
'query' => ['destination' => Url::fromRoute('entity.group_menu.group_menu_edit_form', ['menu' => $entity->getMenuName()])->toString()],
]);
$form['actions']['submit']['#submit'][] = 'group_menu_menu_link_edit_form_submit';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function group_menu_form_menu_link_content_menu_link_content_delete_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['actions']['submit']['#submit'][] = 'group_menu_menu_link_edit_form_submit';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function group_menu_form_menu_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only proceed if we're on group menu route.
if (\Drupal::routeMatch()->getRouteName() == 'entity.group_menu.group_menu_edit_form') {
$build_info = $form_state->getBuildInfo();
$entity = $build_info['callback_object']->getEntity();
// Update the "Add link" link to go to group menu link creation.
$form['links']['links']['#empty'] = t('There are no menu links yet. <a href=":url">Add link</a>.', [
':url' => Url::fromRoute('entity.group_menu.add_link_form', ['menu' => $entity->id()], [
'query' => ['destination' => Url::fromRoute('entity.group_menu.group_menu_edit_form', ['menu' => $entity->id()])->toString()],
])->toString(),
]);
// Set redirect to go to non-admin, group-accessible form.
$form['actions']['submit']['#submit'][] = 'group_menu_menu_edit_form_submit';
// Update edit and delete links appropriately.
// @todo I wonder if there is a better way to do this...
foreach($form['links']['links'] as &$link) {
if (is_array($link) && isset($link['operations']['#links']['edit']) && isset($link['operations']['#links']['delete'])) {
$link['operations']['#links']['edit']['url'] = Url::fromRoute('entity.group_menu.menu_link_content.edit_form', $link['operations']['#links']['edit']['url']->getRouteParameters());
$link['operations']['#links']['delete']['url'] = Url::fromRoute('entity.group_menu.menu_link_content.delete_form', $link['operations']['#links']['delete']['url']->getRouteParameters());
}
}
}
}
/**
* Submit callback to change redirect URL for menu content edit/delete forms.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $form_id
* String representing the name of the form itself.
*/
function group_menu_menu_link_edit_form_submit($form, FormStateInterface $form_state, $form_id) {
$build_info = $form_state->getBuildInfo();
$entity = $build_info['callback_object']->getEntity()->getMenuName();
$form_state->setRedirectUrl(Url::fromRoute('entity.group_menu.group_menu_edit_form', ['menu' => $entity]));
}
/**
* Update edit form redirect Url.
*/
function group_menu_menu_edit_form_submit($form, FormStateInterface $form_state, $form_id) {
$build_info = $form_state->getBuildInfo();
$entity = $build_info['callback_object']->getEntity();
$form_state->setRedirectUrl(Url::fromRoute('entity.group_menu.group_menu_edit_form', ['menu' => $entity->id()]));
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
*
* Adds Group Menus to the available menus set by menu_ui.
*/
function group_menu_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$node_type = $node->type->entity;
$available_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', array('main'));
// Clear out available menus if user does not have permission so that TRUE
// can be set in group_menu_process_access().
if (!\Drupal::currentUser()->hasPermission('administer menu')) {
$available_menus = [];
}
$group_content_types = GroupContentType::loadByContentPluginId("group_menu:menu");
if (!empty($group_content_types)) {
$group_contents = \Drupal::entityTypeManager()
->getStorage('group_menu')
->loadByProperties([
'type' => array_keys($group_content_types),
]);
if (!empty($group_contents)) {
foreach ($group_contents as $group_content) {
/** @var \Drupal\group\Entity\GroupContentInterface $group_content */
$group = $group_content->getGroup();
$account = \Drupal::currentUser();
if ($group->hasPermission('edit group menus', $account)) {
$entity_id = $group_content->get('entity_id')->getValue()[0]['target_id'];
$available_menus[$entity_id] = $entity_id;
}
}
}
}
$node_type->setThirdPartySetting('menu_ui', 'available_menus', array_values(array_filter($available_menus)));
}
/**
* Implements hook_element_info_alter().
*
* If a user does not have the 'administer menu' permission, the '#access' value
* set in menu_ui_form_node_form_alter() needs to be overridden so that a Group
* member can edit the Menu Settings on \Drupal\node\NodeForm.
*/
function group_menu_element_info_alter(array &$types) {
if (isset($types['details'])) {
$types['details']['#process'][] = 'group_menu_process_access';
}
}
/**
* Form element process handler setting menu edit access.
*
* @see group_menu_element_info_alter().
*/
function group_menu_process_access($element) {
if ($element['#parents'][0] == 'menu') {
$element['#access'] = TRUE;
}
return $element;
}