function menu_firstchild_form_menu_link_content_form_alter in Menu Firstchild 2.x
Same name and namespace in other branches
- 8 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) {
// Add validation handler.
$form['#validate'] = array_merge([
'menu_firstchild_menu_link_content_form_validate',
], $form['#validate']);
$link = $form_state
->getFormObject()
->getEntity();
$form_storage = $form_state
->getStorage();
if ($link
->isDefaultTranslationAffectedOnly() && $form_storage['entity_default_langcode'] != $form_storage['langcode']) {
return;
}
$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;
}
}
}