You are here

function select2boxes_preprocess_select in Select2 Boxes 8

Implements template_preprocess_select().

File

./select2boxes.module, line 210
Basic module file.

Code

function select2boxes_preprocess_select(&$vars) {
  $config = \Drupal::config('select2boxes.settings');
  $global = (bool) $config
    ->get('select2_global');
  if ($global) {
    $is_disabled_for_admin = (bool) $config
      ->get('disable_for_admin_pages');
    if ($is_disabled_for_admin) {

      /** @var \Drupal\Core\Routing\AdminContext $admin_context */
      $admin_context = \Drupal::service('router.admin_context');
      if ($admin_context
        ->isAdminRoute()) {
        return;
      }
    }
    $attrs =& $vars['attributes'];
    $attrs['data-jquery-once-autocomplete'] = 'true';
    $attrs['data-select2-autocomplete-list-widget'] = 'true';
    $attrs['class'][] = 'select2-widget';
    $vars['#attached']['library'][] = 'select2boxes/widget';

    // Handle limited search option for globally enabled select2 widgets.
    if ($config
      ->get('limited_search') == '1') {
      $attrs['data-minimum-search-length'] = $config
        ->get('minimum_search_length') ?: 0;
    }
  }
}