View source
<?php
use Drupal\Core\Form\FormStateInterface;
function cami_form_menu_link_content_menu_link_content_form_alter(&$form, FormStateInterface $form_state) {
$menu_link = $form_state
->getFormObject()
->getEntity();
$menu_link_options = $menu_link->link
->first()->options ?: [];
$defaults = isset($menu_link_options['cami']) ? $menu_link_options['cami'] : [];
$form['options']['cami'] = array(
'#type' => 'details',
'#open' => FALSE,
'#title' => t('Custom Active Menu Item'),
'#weight' => 1,
'#tree' => TRUE,
);
$form['options']['cami']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#description' => t("Pages where the menu item will be active, Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog."),
'#default_value' => isset($defaults['pages']) ? $defaults['pages'] : '',
);
$form['actions']['submit']['#submit'][] = 'cami_menu_link_content_form_submit';
}
function cami_menu_link_content_form_submit($form, FormStateInterface $form_state) {
$menu_link = $form_state
->getFormObject()
->getEntity();
$options = [
'cami' => $form_state
->getValue('cami'),
];
$menu_link_options = $menu_link->link
->first()->options;
$menu_link->link
->first()->options = array_merge($menu_link_options, $options);
$menu_link
->save();
}