View source
<?php
namespace Drupal\autocomplete_deluxe\Element;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\CompositeFormElementTrait;
use Drupal\Core\Render\Element\FormElement;
class AutocompleteDeluxeElement extends FormElement {
use CompositeFormElementTrait;
public function getInfo() {
$class = get_class($this);
$info['#target_type'] = NULL;
$info['#selection_handler'] = 'default';
$info['#selection_settings'] = [];
$info['#tags'] = TRUE;
$info['#autocreate'] = NULL;
$info['#validate_reference'] = TRUE;
$info['#process_default_value'] = TRUE;
$info['#element_validate'] = [
[
'\\Drupal\\Core\\Entity\\Element\\EntityAutocomplete',
'validateEntityAutocomplete',
],
];
$info['#process'][] = [
$class,
'processElement',
];
return $info;
}
public static function processElement($element) {
$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)) {
$element['#attached']['library'][] = 'autocomplete_deluxe/assets.claro';
}
elseif ($active_theme
->getName() == 'seven' || array_key_exists('seven', $base_themes)) {
$element['#attached']['library'][] = 'autocomplete_deluxe/assets.seven';
}
}
$html_id = Html::getUniqueId('autocomplete-deluxe-input');
$element['#after_build'][] = [
get_called_class(),
'afterBuild',
];
$element['#multiple'] = isset($element['#multiple']) ? $element['#multiple'] : FALSE;
$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'] : '',
];
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'])) {
$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 {
return $element;
}
if (!$element_disabled) {
$element['#attached']['drupalSettings']['autocomplete_deluxe'] = $js_settings;
}
$element['#tree'] = TRUE;
return $element;
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
return [];
}
public static function afterBuild(array $element, FormStateInterface $form_state) {
unset($element['textfield']['#maxlength']);
$element['#value'] = isset($element['value_field']) ? $element['value_field']['#value'] : $element['textfield']['#value'];
if (isset($element['value_field'])) {
$element['#value'] = trim($element['#value']);
$element['#value'] = preg_replace('/"" +""/', ',', $element['#value']);
$element['#value'] = substr($element['#value'], 2, strlen($element['#value']) - 4);
unset($element['value_field']['#maxlength']);
}
$form_state
->setValueForElement($element, $element['#value']);
return $element;
}
}