diff --git a/groupmenu.module b/groupmenu.module index 3ef651a..a219e37 100644 --- a/groupmenu.module +++ b/groupmenu.module @@ -97,6 +97,7 @@ function groupmenu_form_alter(&$form, FormStateInterface $form_state, $form_id) function groupmenu_form_node_form_alter(&$form, FormStateInterface $form_state) { $account = \Drupal::currentUser(); $node = $form_state->getFormObject()->getEntity(); + $groups = []; // If a node is being edited at /node/%/edit, look for groups that it is a // part of. If not, try to use the group ID from the URL. if ($node->id()) { @@ -115,13 +116,15 @@ function groupmenu_form_node_form_alter(&$form, FormStateInterface $form_state) if (!empty($groups) || !$account->hasPermission('administer menu')) { $menu_options = &$form['menu']['link']['menu_parent']['#options']; if ($menu_options) { - $menu_exist = groupmenu_filter_parent_options($menu_options, $groups); - if ($menu_exist) { + $filtered_menu_options = groupmenu_filter_parent_options($menu_options, $groups); + if ($filtered_menu_options) { // If we have access to menu options, this means we have permission to - // edit the group menu. + // edit the group menu. When the menu options were added by Group menu, + // access needs to be restored again as the Menu module removed it. $form['menu']['#access'] = TRUE; } else { + // If there are no menu options available, remove the menu settings tab. $form['menu']['#access'] = FALSE; } } @@ -144,11 +147,8 @@ function groupmenu_filter_parent_options(array &$options, array $groups = []) { $allowed_menus = []; $groupmenu_service = \Drupal::service('groupmenu.menu'); if (!empty($groups)) { - foreach($groups as $group) { - $allowed_menus_raw[] = $groupmenu_service->loadUserGroupMenusByGroup('edit', $group->id()); - } - foreach ($allowed_menus_raw as $menu) { - $allowed_menus = array_merge($allowed_menus, $menu); + foreach ($groups as $group) { + $allowed_menus = array_merge($allowed_menus, $groupmenu_service->loadUserGroupMenusByGroup('edit', $group->id())); } } else {