You are here

function groupmenu_form_node_form_alter in Group Menu 8

Same name and namespace in other branches
  1. 7 groupmenu.module \groupmenu_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

File

./groupmenu.module, line 97
Gives the ability to create and manage menus for groups.

Code

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()) {
    $group_content_array = GroupContent::loadByEntity($node);
    foreach ($group_content_array as $group_content) {
      $group_ids[] = $group_content->gid->target_id;
    }
    if (!empty($group_ids)) {
      $groups = Group::loadMultiple($group_ids);
    }
  }
  elseif (isset($form_state
    ->getStorage()['group'])) {
    $groups[] = $form_state
      ->getStorage()['group'];
  }
  if (!empty($groups) || !$account
    ->hasPermission('administer menu')) {
    $menu_options =& $form['menu']['link']['menu_parent']['#options'];
    if ($menu_options) {
      $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. 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;
      }
    }
  }
}