You are here

menu_link_modal.module in Menu Link Modal 8

Same filename and directory in other branches
  1. 8.2 menu_link_modal.module

Main file of Menu Link Modal module.

File

menu_link_modal.module
View source
<?php

/**
 * @file
 * Main file of Menu Link Modal module.
 */
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_modules_installed().
 */
function menu_link_modal_modules_installed(array $modules) {
  if (in_array('menu_link_modal', $modules)) {
    module_set_weight('menu_link_modal', 100);
  }
}

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

/**
 * Submit handler for the menu form alter.
 */
function menu_link_modal_menu_link_content_form_submit($form, FormStateInterface $form_state) {
  $menu_link = $form_state
    ->getFormObject()
    ->getEntity();
  $options = [
    'modal' => [],
  ];
  if ($form_state
    ->getValue('open_modal')) {
    $options['modal'] = [
      'width' => $form_state
        ->getValue('modal_width'),
      'height' => $form_state
        ->getValue('modal_height'),
    ];
  }
  $menu_link_options = $menu_link->link
    ->first()->options;
  $menu_link->link
    ->first()->options = array_merge($menu_link_options, $options);
  $menu_link
    ->save();
}

/**
 * Implements hook_preprocess_menu().
 */
function menu_link_modal_preprocess_menu(&$variables) {
  $modal_link = FALSE;
  menu_link_modal_set_attributes($variables['items'], $modal_link);
  if ($modal_link) {
    $variables['#attached']['library'][] = 'core/drupal.dialog.ajax';
  }
}

/**
 * Set the attributes on the given items.
 *
 * @param array $items
 *   List of menu items.
 * @param bool $modal_link
 *   Modal link: true if exist else false.
 */
function menu_link_modal_set_attributes(array &$items, &$modal_link) {
  foreach ($items as &$item) {
    $modal_info = $item['url']
      ->getOption('modal') ?? [];
    if (!empty($modal_info)) {
      $modal_link = TRUE;
      $attributes = $item['url']
        ->getOption('attributes') ?: [];
      $attributes['class'] = isset($attributes['class']) ? !is_array($attributes['class']) ? explode(' ', $attributes['class']) : $attributes['class'] : [];
      $attributes['class'][] = 'use-ajax';
      $attributes['data-dialog-type'] = 'modal';
      $attributes['data-dialog-options'] = Json::encode([
        'height' => $modal_info['height'],
        'width' => $modal_info['width'],
      ]);
      $item['url']
        ->setOption('attributes', $attributes);
    }
    if (!empty($item['below'])) {
      menu_link_modal_set_attributes($item['below'], $modal_link);
    }
  }
}

Functions

Namesort descending Description
menu_link_modal_form_menu_link_content_form_alter Implements hook_form_BASE_FORM_ID_alter().
menu_link_modal_menu_link_content_form_submit Submit handler for the menu form alter.
menu_link_modal_modules_installed Implements hook_modules_installed().
menu_link_modal_preprocess_menu Implements hook_preprocess_menu().
menu_link_modal_set_attributes Set the attributes on the given items.