function EntityReferenceAutocompleteWidget::formElement

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::formElement()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::formElement()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::formElement()

Overrides WidgetInterface::formElement

1 call to EntityReferenceAutocompleteWidget::formElement()
EntityReferenceAutocompleteTagsWidget::formElement in core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteTagsWidget.php
Returns the form for a single field widget.
1 method overrides EntityReferenceAutocompleteWidget::formElement()
EntityReferenceAutocompleteTagsWidget::formElement in core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteTagsWidget.php
Returns the form for a single field widget.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php, line 96

Class

EntityReferenceAutocompleteWidget
Plugin implementation of the 'entity_reference_autocomplete' widget.

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $entity = $items->getEntity();
    $referenced_entities = $items->referencedEntities();
    // Append the match operation to the selection settings.
    $selection_settings = $this->getFieldSetting('handler_settings') + [
        'match_operator' => $this->getSetting('match_operator'),
        'match_limit' => $this->getSetting('match_limit'),
    ];
    $element += [
        '#type' => 'entity_autocomplete',
        '#target_type' => $this->getFieldSetting('target_type'),
        '#selection_handler' => $this->getFieldSetting('handler'),
        '#selection_settings' => $selection_settings,
        // Entity reference field items are handling validation themselves via
        // the 'ValidReference' constraint.
'#validate_reference' => FALSE,
        '#maxlength' => 1024,
        '#default_value' => $referenced_entities[$delta] ?? NULL,
        '#size' => $this->getSetting('size'),
        '#placeholder' => $this->getSetting('placeholder'),
    ];
    if ($bundle = $this->getAutocreateBundle()) {
        $element['#autocreate'] = [
            'bundle' => $bundle,
            'uid' => $entity instanceof EntityOwnerInterface ? $entity->getOwnerId() : \Drupal::currentUser()->id(),
        ];
    }
    return [
        'target_id' => $element,
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.