class AutocompleteWidget

Plugin implementation of the 'entity_reference 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 = "entity_reference_autocomplete",
  module = "entity_reference",
  label = @Translation("Autocomplete"),
  description = @Translation("An autocomplete text field."),
  field_types = {
    "entity_reference"
  },
  settings = {
    "match_operator" = "CONTAINS",
    "size" = 60,
    "autocomplete_path" = "entity_reference/autocomplete/single",
    "placeholder" = ""
  }
)

Hierarchy

Expanded class hierarchy of AutocompleteWidget

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php, line 38
Contains \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidget.

Namespace

Drupal\entity_reference\Plugin\field\widget
View source
class AutocompleteWidget extends AutocompleteWidgetBase {

  /**
   * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::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();
    }
    return parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
  }

  /**
   * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate()
   */
  public function elementValidate($element, &$form_state, $form) {
    $auto_create = isset($this->instance['settings']['handler_settings']['auto_create']) ? $this->instance['settings']['handler_settings']['auto_create'] : FALSE;

    // 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.
        $handler = entity_reference_get_selection_handler($this->field, $this->instance);
        $value = $handler
          ->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create);
      }
      if (!$value && $auto_create && count($this->instance['settings']['handler_settings']['target_bundles']) == 1) {

        // Auto-create item. see entity_reference_field_presave().
        $value = array(
          'target_id' => 0,
          'entity' => $this
            ->createNewEntity($element['#value'], $element['#autocreate_uid']),
          // Keep the weight property.
          '_weight' => $element['#weight'],
        );

        // Change the element['#parents'], so in form_set_value() we
        // populate the correct key.
        array_pop($element['#parents']);
      }
    }
    form_set_value($element, $value, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutocompleteWidget::elementValidate public function Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate() Overrides AutocompleteWidgetBase::elementValidate
AutocompleteWidget::formElement public function Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::formElement(). Overrides AutocompleteWidgetBase::formElement
AutocompleteWidgetBase::createNewEntity protected function Creates a new entity from a label entered in the autocomplete input.
AutocompleteWidgetBase::errorElement public function Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::errorElement(). Overrides WidgetBase::errorElement
AutocompleteWidgetBase::getLabels protected function Gets the entity labels.
AutocompleteWidgetBase::settingsForm public function Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::settingsForm(). Overrides WidgetBase::settingsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::extractFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). Overrides WidgetBaseInterface::extractFormValues
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 2
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct 3