You are here

function menu_firstchild_form_menu_link_content_form_alter in Menu Firstchild 8

Same name and namespace in other branches
  1. 2.x menu_firstchild.module \menu_firstchild_form_menu_link_content_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

File

./menu_firstchild.module, line 28
Contains menu_firstchild.module.

Code

function menu_firstchild_form_menu_link_content_form_alter(array &$form, FormStateInterface $form_state) {
  $link = $form_state
    ->getFormObject()
    ->getEntity();
  $options = $link->link
    ->first()->options;
  if (empty($options['menu_firstchild'])) {
    $options['menu_firstchild'] = [
      'enabled' => FALSE,
    ];
  }
  $form['link']['#prefix'] = '<div id="menu-firstchild-link-wrapper">';
  $form['link']['#suffix'] = '</div>';
  $form['link']['widget']['#disabled'] = !empty($options['menu_firstchild']['enabled']);
  $form['menu_firstchild_enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('First child'),
    '#description' => t('When enabled, this menu item will redirect to the first child item.'),
    '#default_value' => $options['menu_firstchild']['enabled'],
    '#ajax' => [
      'callback' => 'menu_firstchild_menu_link_content_form_ajax_callback',
    ],
    '#weight' => $form['link']['#weight'] + 1,
  ];
  $ajax = $form_state
    ->getValue([
    'menu_firstchild_enabled',
  ]) !== NULL;
  if ($ajax) {
    if ($form_state
      ->getValue([
      'menu_firstchild_enabled',
    ])) {
      $form['link']['widget'][0]['uri']['#value'] = 'route:<none>';
      $form['link']['widget']['#disabled'] = TRUE;
    }
    else {
      $form['link']['widget'][0]['uri']['#default_value'] = '';
      $form['link']['widget']['#disabled'] = FALSE;
    }
  }
  $form['actions']['submit']['#submit'][] = 'menu_firstchild_menu_link_content_form_submit';
}