class AutocompleteDeluxeElement in Autocomplete Deluxe 8
Same name and namespace in other branches
- 2.0.x src/Element/AutocompleteDeluxeElement.php \Drupal\autocomplete_deluxe\Element\AutocompleteDeluxeElement
Provides an Autocomplete Deluxe Form API element.
Plugin annotation
@FormElement("autocomplete_deluxe");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\autocomplete_deluxe\Element\AutocompleteDeluxeElement uses CompositeFormElementTrait
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AutocompleteDeluxeElement
1 #type use of AutocompleteDeluxeElement
- AutocompleteDeluxeWidget::formElement in src/
Plugin/ Field/ FieldWidget/ AutocompleteDeluxeWidget.php - Returns the form for a single field widget.
File
- src/
Element/ AutocompleteDeluxeElement.php, line 15
Namespace
Drupal\autocomplete_deluxe\ElementView source
class AutocompleteDeluxeElement extends FormElement {
use CompositeFormElementTrait;
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
// Apply default form element properties.
$info['#target_type'] = NULL;
$info['#selection_handler'] = 'default';
$info['#selection_settings'] = [];
$info['#tags'] = TRUE;
$info['#autocreate'] = NULL;
// This should only be set to FALSE if proper validation by the selection
// handler is performed at another level on the extracted form values.
$info['#validate_reference'] = TRUE;
// IMPORTANT! This should only be set to FALSE if the #default_value
// property is processed at another level (e.g. by a Field API widget) and
// its value is properly checked for access.
$info['#process_default_value'] = TRUE;
$info['#element_validate'] = [
[
'\\Drupal\\Core\\Entity\\Element\\EntityAutocomplete',
'validateEntityAutocomplete',
],
];
$info['#process'][] = [
$class,
'processElement',
];
return $info;
}
/**
* Autocomplete Deluxe element process callback.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
return [];
}
/**
* Form API after build callback for the duration parameter type form.
*
* Fixes up the form value by applying the multiplier.
*/
public static function afterBuild(array $element, FormStateInterface $form_state) {
// By default Drupal sets the maxlength to 128 if the property isn't
// specified, but since the limit isn't useful in some cases,
// we unset the property.
unset($element['textfield']['#maxlength']);
// Set the elements value from either the value field or text field input.
$element['#value'] = isset($element['value_field']) ? $element['value_field']['#value'] : $element['textfield']['#value'];
if (isset($element['value_field'])) {
$element['#value'] = trim($element['#value']);
// Replace all cases of double double quotes and one or more spaces with a
// comma. This will allow us to keep entries in double quotes.
$element['#value'] = preg_replace('/"" +""/', ',', $element['#value']);
// Remove the double quotes at the beginning and the end from the first
// and the last term.
$element['#value'] = substr($element['#value'], 2, strlen($element['#value']) - 4);
unset($element['value_field']['#maxlength']);
}
$form_state
->setValueForElement($element, $element['#value']);
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutocompleteDeluxeElement:: |
public static | function | Form API after build callback for the duration parameter type form. | |
AutocompleteDeluxeElement:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
AutocompleteDeluxeElement:: |
public static | function | Autocomplete Deluxe element process callback. | |
AutocompleteDeluxeElement:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
CompositeFormElementTrait:: |
public static | function | Adds form element theming to an element if its title or description is set. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |