You are here

cami.module in Custom Active Menu Item 8

Same filename and directory in other branches
  1. 7 cami.module

File

cami.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter() for menu_link_edit().
 */
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';
}

/**
 * Submit function for menu add / edit form.
 */
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();
}

Functions

Namesort descending Description
cami_form_menu_link_content_menu_link_content_form_alter Implements hook_form_FORM_ID_alter() for menu_link_edit().
cami_menu_link_content_form_submit Submit function for menu add / edit form.