public static function AutocompleteDeluxeElement::processElement in Autocomplete Deluxe 8
Same name and namespace in other branches
- 2.0.x 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\ElementCode
public static function processElement($element) {
$element['#attached']['library'][] = 'autocomplete_deluxe/assets';
// Workaround for problems with jquery css in seven theme.
$active_theme = \Drupal::theme()
->getActiveTheme();
if ($active_theme
->getName() == 'seven') {
$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'] = [
'#type' => 'textfield',
'#size' => isset($element['#size']) ? $element['#size'] : '',
'#attributes' => [
'class' => [
'autocomplete-deluxe-form',
],
'id' => $html_id,
],
'#default_value' => '',
'#prefix' => '<div class="autocomplete-deluxe-container">',
'#suffix' => '</div>',
'#description' => isset($element['#description']) ? $element['#description'] : '',
];
$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;
}
$element['#attached']['drupalSettings']['autocomplete_deluxe'] = $js_settings;
$element['#tree'] = TRUE;
return $element;
}