You are here

public static function AutocompleteDeluxeElement::processElement in Autocomplete Deluxe 2.0.x

Same name and namespace in other branches
  1. 8 src/Element/AutocompleteDeluxeElement.php \Drupal\autocomplete_deluxe\Element\AutocompleteDeluxeElement::processElement()

Autocomplete Deluxe element process callback.

File

src/Element/AutocompleteDeluxeElement.php, line 48

Class

AutocompleteDeluxeElement
Provides an Autocomplete Deluxe Form API element.

Namespace

Drupal\autocomplete_deluxe\Element

Code

public static function processElement($element) {

  // Do not attach js library if the element is disabled.
  $element_disabled = $element['#disabled'] ?? FALSE;
  if (!$element_disabled) {
    $element['#attached']['library'][] = 'autocomplete_deluxe/assets';
    $active_theme = \Drupal::theme()
      ->getActiveTheme();
    $base_themes = (array) $active_theme
      ->getBaseThemeExtensions();
    if ($active_theme
      ->getName() === 'claro' || array_key_exists('claro', $base_themes)) {

      // Workaround for problems with jquery css in claro theme.
      $element['#attached']['library'][] = 'autocomplete_deluxe/assets.claro';
    }
    elseif ($active_theme
      ->getName() == 'seven' || array_key_exists('seven', $base_themes)) {

      // Workaround for problems with jquery css in seven theme.
      $element['#attached']['library'][] = 'autocomplete_deluxe/assets.seven';
    }
  }
  $html_id = Html::getUniqueId('autocomplete-deluxe-input');
  $element['#after_build'][] = [
    get_called_class(),
    'afterBuild',
  ];

  // Set default options for multiple values.
  $element['#multiple'] = isset($element['#multiple']) ? $element['#multiple'] : FALSE;

  // Add label_display and label variables to template.
  $element['label'] = [
    '#theme' => 'form_element_label',
  ];
  $element['label'] += array_intersect_key($element, array_flip([
    '#id',
    '#required',
    '#title',
    '#title_display',
  ]));
  $element['textfield'] = [
    '#disabled' => $element_disabled,
    '#type' => 'textfield',
    '#size' => isset($element['#size']) ? $element['#size'] : '',
    '#attributes' => [
      'class' => [
        'autocomplete-deluxe-form',
      ],
      'id' => $html_id,
    ],
    '#default_value' => '',
    '#description' => isset($element['#description']) ? $element['#description'] : '',
  ];

  // Add autcomplete deluxe container only if element is enabled.
  if (!$element_disabled) {
    $element['textfield']['#prefix'] = '<div class="autocomplete-deluxe-container">';
    $element['textfield']['#suffix'] = '</div>';
  }
  $js_settings[$html_id] = [
    'input_id' => $html_id,
    'multiple' => $element['#multiple'],
    'required' => $element['#required'],
    'limit' => isset($element['#limit']) ? $element['#limit'] : 10,
    'min_length' => isset($element['#min_length']) ? $element['#min_length'] : 0,
    'use_synonyms' => isset($element['#use_synonyms']) ? $element['#use_synonyms'] : 0,
    'delimiter' => isset($element['#delimiter']) ? $element['#delimiter'] : '',
    'not_found_message_allow' => isset($element['#not_found_message_allow']) ? $element['#not_found_message_allow'] : FALSE,
    'not_found_message' => isset($element['#not_found_message']) ? $element['#not_found_message'] : "The term '@term' will be added.",
    'new_terms' => isset($element['#new_terms']) ? $element['#new_terms'] : FALSE,
    'no_empty_message' => isset($element['#no_empty_message']) ? $element['#no_empty_message'] : 'No terms could be found. Please type in order to add a new term.',
  ];
  if (isset($element['#autocomplete_deluxe_path'])) {
    if (isset($element['#default_value'])) {

      // Split on the comma only if that comma has zero, or an even number of
      // quotes in ahead of it.
      // http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes
      $default_value = preg_replace('/,(?=([^\\"]*\\"[^\\"]*\\")*[^\\"]*$)/i', '"" ""', $element['#default_value']);
      $default_value = '""' . $default_value . '""';
    }
    else {
      $default_value = '';
    }
    if ($element['#multiple']) {
      $element['value_field'] = [
        '#type' => 'textfield',
        '#attributes' => [
          'class' => [
            'autocomplete-deluxe-value-field',
          ],
        ],
        '#default_value' => $default_value,
        '#prefix' => '<div class="autocomplete-deluxe-value-container">',
        '#suffix' => '</div>',
        '#description' => isset($element['#description']) ? $element['#description'] : '',
      ];
      $element['textfield']['#attributes']['style'] = [
        'display: none',
      ];
    }
    else {
      $element['textfield']['#default_value'] = isset($element['#default_value']) ? $element['#default_value'] : '';
    }
    $js_settings[$html_id] += [
      'type' => 'ajax',
      'uri' => $element['#autocomplete_deluxe_path'],
    ];
  }
  else {

    // If there is no source (path or data), we don't want to add the js
    // settings and so the functions will be aborted.
    return $element;
  }

  // Do not attach js settings if element is disabled.
  if (!$element_disabled) {
    $element['#attached']['drupalSettings']['autocomplete_deluxe'] = $js_settings;
  }
  $element['#tree'] = TRUE;
  return $element;
}