You are here

function menu_link_modal_form_menu_link_content_form_alter in Menu Link Modal 8.2

Same name and namespace in other branches
  1. 8 menu_link_modal.module \menu_link_modal_form_menu_link_content_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

File

./menu_link_modal.module, line 23
Main file of Menu Link Modal module.

Code

function menu_link_modal_form_menu_link_content_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $menu_link = $form_state
    ->getFormObject()
    ->getEntity();
  $menu_link_options = $menu_link->link
    ->first()->options ?: [];
  $modal_info = isset($menu_link_options['modal']) ? $menu_link_options['modal'] : NULL;
  $form['modal_config'] = [
    '#type' => 'details',
    '#title' => t('Modal settings'),
    '#open' => $modal_info ? TRUE : FALSE,
  ];
  $form['modal_config']['open_modal'] = [
    '#type' => 'checkbox',
    '#title' => t('Open link in Modal.'),
    '#default_value' => $modal_info ? TRUE : FALSE,
    '#tree' => FALSE,
  ];
  $form['modal_config']['settings'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        'input[name="open_modal"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['modal_config']['settings']['modal_width'] = [
    '#type' => 'textfield',
    '#title' => t('Modal width'),
    '#description' => t('Add modal width in pixel, enter only numeric value here.'),
    '#default_value' => isset($modal_info['width']) ? $modal_info['width'] : 700,
  ];
  $form['modal_config']['settings']['modal_height'] = [
    '#type' => 'textfield',
    '#title' => t('Modal height'),
    '#description' => t('Add modal height in pixel, enter only numeric value here.'),
    '#default_value' => isset($modal_info['height']) ? $modal_info['height'] : 400,
  ];

  // @todo: Add validation to validate width and height values.
  $form['actions']['submit']['#submit'][] = 'menu_link_modal_menu_link_content_form_submit';
}