You are here

improved_multi_select.module in Improved Multi Select 8

Replace the default multi-select boxes with two panels list and filter.

File

improved_multi_select.module
View source
<?php

/**
 * @file
 * Replace the default multi-select boxes with two panels list and filter.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_page_attachments().
 */
function improved_multi_select_page_attachments(&$attachments) {
  $activated = FALSE;
  $config = \Drupal::config('improved_multi_select.settings');
  $context = [
    'url' => trim($config
      ->get('url')),
    'request_path' => \Drupal::request()
      ->getRequestUri(),
    'selectors' => array_filter(explode("\n", str_replace("\r", "\n", trim($config
      ->get('selectors'))))),
  ];

  // Activated on replace all option.
  if ($replace_all = $config
    ->get('isall')) {
    $activated = TRUE;
  }
  elseif ($context['url'] && \Drupal::service('path.matcher')
    ->matchPath($context['request_path'], $context['url']) || $context['selectors']) {
    $activated = TRUE;
  }

  // Allow other modules to activate/deactivate the module for specific pages.
  \Drupal::moduleHandler()
    ->alter('improved_multi_select_activated', $activated, $context);
  if (!$activated) {
    return;
  }
  $drupal_settings = [
    'selectors' => improved_multi_select_load_selectors($replace_all, $context['selectors']),
    'filtertype' => $config
      ->get('filtertype'),
    'placeholder_text' => $config
      ->get('placeholder_text'),
    'orderable' => $config
      ->get('orderable'),
    'js_regex' => $config
      ->get('js_regex'),
    'groupresetfilter' => $config
      ->get('groupresetfilter'),
    'remove_required_attr' => $config
      ->get('remove_required_attr'),
    'buttontext_add' => $config
      ->get('buttontext_add'),
    'buttontext_addall' => $config
      ->get('buttontext_addall'),
    'buttontext_del' => $config
      ->get('buttontext_del'),
    'buttontext_delall' => $config
      ->get('buttontext_delall'),
    'buttontext_moveup' => $config
      ->get('buttontext_moveup'),
    'buttontext_movedown' => $config
      ->get('buttontext_movedown'),
  ];

  // Allow other modules to alter our data.
  \Drupal::moduleHandler()
    ->alter('improved_multi_select_attached', $drupal_settings, $context);
  $attachments['#attached']['library'][] = 'improved_multi_select/ims';
  $attachments['#attached']['drupalSettings']['improved_multi_select'] = $drupal_settings;
}

/**
 * Return array of jQuery selectors depending on module configuration.
 *
 * @param bool $replace_all
 *   Boolean indicating if all multi value selects should be replaced.
 * @param array $selectors
 *   Array with jQuery selectors.
 *
 * @return array
 *   Array with jQuery selectors.
 */
function improved_multi_select_load_selectors(bool $replace_all, array $selectors) {
  $output = [];
  if (!empty($selectors) and !$replace_all) {
    foreach ($selectors as $selector) {
      $output[] = $selector;
    }
  }
  else {
    $output = [
      'select[multiple]',
    ];
  }
  return $output;
}

/**
 * Implements hook_help().
 *
 * @inheritdoc
 */
function improved_multi_select_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.improved_multi_select':
      $text = file_get_contents(dirname(__FILE__) . '/README.txt');
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }

      // Use the Markdown filter to render the README.
      $filter_manager = \Drupal::service('plugin.manager.filter');
      $settings = \Drupal::configFactory()
        ->get('markdown.settings')
        ->getRawData();
      $config = [
        'settings' => $settings,
      ];
      $filter = $filter_manager
        ->createInstance('markdown', $config);
      return $filter
        ->process($text, 'en');
  }
  return NULL;
}

Functions

Namesort descending Description
improved_multi_select_help Implements hook_help().
improved_multi_select_load_selectors Return array of jQuery selectors depending on module configuration.
improved_multi_select_page_attachments Implements hook_page_attachments().