class AutocompleteWidget in Entity reference 8
Plugin implementation of the 'entityreference autocomplete' widget.
@todo: Check if the following statement is still correct The autocomplete path doesn't have a default here, because it's not the the two widgets, and the Field API doesn't update default settings when the widget changes.
Plugin annotation
@Plugin(
id = "entityreference_autocomplete",
module = "entityreference",
label = @Translation("Autocomplete"),
description = @Translation("An autocomplete text field."),
field_types = {
"entityreference"
},
settings = {
"match_operator" = "CONTAINS",
"size" = 60,
"path" = ""
}
)
Hierarchy
- class \Drupal\entityreference\Plugin\field\widget\AutocompleteWidgetBase extends \Drupal\field\Plugin\Type\Widget\WidgetBase
- class \Drupal\entityreference\Plugin\field\widget\AutocompleteWidget
Expanded class hierarchy of AutocompleteWidget
File
- lib/
Drupal/ entityreference/ Plugin/ field/ widget/ AutocompleteWidget.php, line 39 - Definition of Drupal\entityreference\Plugin\field\widget\AutocompleteWidget.
Namespace
Drupal\entityreference\Plugin\field\widgetView source
class AutocompleteWidget extends AutocompleteWidgetBase {
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
*/
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
// We let the Field API handles multiple values for us, only take
// care of the one matching our delta.
if (isset($items[$delta])) {
$items = array(
$items[$delta],
);
}
else {
$items = array();
}
$element = $this
->prepareElement($items, $delta, $element, $langcode, $form, $form_state, 'entityreference/autocomplete/single');
return array(
'target_id' => $element,
);
}
/**
* Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate()
*/
public function elementValidate($element, &$form_state) {
// If a value was entered into the autocomplete.
$value = '';
if (!empty($element['#value'])) {
// Take "label (entity id)', match the id from parenthesis.
if (preg_match("/.+\\((\\d+)\\)/", $element['#value'], $matches)) {
$value = $matches[1];
}
else {
// Try to get a match from the input string when the user didn't use the
// autocomplete but filled in a value manually.
$field = field_info_field($element['#field_name']);
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$handler = entityreference_get_selection_handler($field, $instance);
$value = $handler
->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
}
}
form_set_value($element, $value, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutocompleteWidget:: |
public | function |
Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate() Overrides AutocompleteWidgetBase:: |
|
AutocompleteWidget:: |
public | function |
Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides AutocompleteWidgetBase:: |
|
AutocompleteWidgetBase:: |
public | function | Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). | |
AutocompleteWidgetBase:: |
protected | function | Get the entity labels. | |
AutocompleteWidgetBase:: |
protected | function | Prepapre the element. | |
AutocompleteWidgetBase:: |
public | function | Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). |