function EntityReferenceAutocompleteWidget::formElement

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::formElement()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::formElement()
  3. 10 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 95

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();
    $selection_settings = [];
    // Append the match operation to the selection settings.
    if ($this->getFieldSetting('handler_settings') !== NULL) {
        $selection_settings = $this->getFieldSetting('handler_settings');
    }
    $selection_settings += [
        'match_operator' => $this->getSetting('match_operator'),
        'match_limit' => $this->getSetting('match_limit'),
    ];
    // Append the entity if it is already created.
    if (!$entity->isNew()) {
        $selection_settings['entity'] = $entity;
    }
    $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.