You are here

public function NodeFormAlter::alter in Group Content Menu 8

Alter node forms to add GroupContentMenu options where appropriate.

Parameters

array $form: A form array as from hook_form_alter().

\Drupal\Core\Form\FormStateInterface $form_state: A form state object as from hook_form_alter().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

See also

group_content_menu_form_node_form_alter()

File

src/NodeFormAlter.php, line 97

Class

NodeFormAlter
Helper class to handle altering node forms.

Namespace

Drupal\group_content_menu

Code

public function alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $this->entityTypeManager
    ->getStorage('node_type')
    ->load($node
    ->bundle());
  $available_menus = $node_type
    ->getThirdPartySetting('menu_ui', 'available_menus', [
    'main',
  ]);
  $menu_ui_menus = array_map(static function (MenuInterface $menu) {
    return $menu
      ->label();
  }, $this->entityTypeManager
    ->getStorage('menu')
    ->loadMultiple($available_menus));
  $groups = $this
    ->getGroups($form_state, $node);
  if (!empty($groups)) {
    $group_menus = $this
      ->getGroupMenus($groups);
    $defaults = menu_ui_get_menu_link_defaults($node);
    if ($defaults['id']) {
      $default = $defaults['menu_name'] . ':' . $defaults['parent'];
    }
    else {
      $defaults = group_content_menu_get_menu_link_default($node, array_keys($group_menus));
      $default = $defaults['menu_name'] . ':' . $defaults['parent'];
    }

    // Are there any traditional menus that are not group menus?
    $traditional_menus = !empty($form['menu']['link']['menu_parent']['#options']);
    $form['menu'] = [
      '#type' => 'details',
      '#title' => t('Menu settings'),
      '#open' => (bool) $defaults['id'],
      '#group' => 'advanced',
      '#attached' => [
        'library' => [
          'menu_ui/drupal.menu_ui',
        ],
      ],
      '#tree' => TRUE,
      '#weight' => -2,
      '#attributes' => [
        'class' => [
          'menu-link-form',
        ],
      ],
    ];
    $form['menu']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Provide a menu link'),
      '#default_value' => (int) (bool) $defaults['id'],
    ];
    $form['menu']['link'] = [
      '#type' => 'container',
      '#parents' => [
        'menu',
      ],
      '#states' => [
        'invisible' => [
          'input[name="menu[enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['menu']['link']['menu_parent'] = $this->menuParentSelector
      ->parentSelectElement($default, $defaults['id'], array_merge($group_menus, $menu_ui_menus));

    // Populate the element with the link data.
    foreach ([
      'id',
      'entity_id',
    ] as $key) {
      $form['menu']['link'][$key] = [
        '#type' => 'value',
        '#value' => $defaults[$key],
      ];
    }
    $form['menu']['link']['title'] = [
      '#type' => 'textfield',
      '#title' => t('Menu link title'),
      '#default_value' => $defaults['title'],
      '#maxlength' => $defaults['title_max_length'],
    ];
    $form['menu']['link']['description'] = [
      '#type' => 'textfield',
      '#title' => t('Description'),
      '#default_value' => $defaults['description'],
      '#description' => t('Shown when hovering over the menu link.'),
      '#maxlength' => $defaults['description_max_length'],
    ];
    $form['menu']['link']['menu_parent']['#title'] = t('Parent item');
    $form['menu']['link']['menu_parent']['#attributes']['class'][] = 'menu-parent-select';
    $form['menu']['link']['weight'] = [
      '#type' => 'number',
      '#title' => t('Weight'),
      '#default_value' => $defaults['weight'],
      '#description' => t('Menu links with lower weights are displayed before links with higher weights.'),
    ];
    foreach (array_keys($form['actions']) as $action) {
      if ($action !== 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'menu_ui_form_node_form_submit';
      }
    }
    $form['menu']['#access'] = FALSE;
    if (!empty($form['menu']['link']['menu_parent']['#options'])) {

      // If there are traditional menus and user has admin permission.
      if ($traditional_menus && $this->currentUser
        ->hasPermission('administer menu')) {
        $form['menu']['#access'] = TRUE;
      }
      else {
        $context_id = '@group.group_route_context:group';
        $contexts = $this->contextRepository
          ->getRuntimeContexts([
          $context_id,
        ]);
        $group = $contexts[$context_id]
          ->getContextValue();
        if ($group && $group
          ->hasPermission('manage group_content_menu', $this->currentUser)) {
          $form['menu']['#access'] = TRUE;
        }
      }
    }
    $form['#entity_builders'][] = 'menu_ui_node_builder';
  }
}