You are here

public function WebformEntityReferenceAutocompleteWidget::getTargetIdElement in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/Field/FieldWidget/WebformEntityReferenceAutocompleteWidget.php \Drupal\webform\Plugin\Field\FieldWidget\WebformEntityReferenceAutocompleteWidget::getTargetIdElement()

Returns the target id element form for a single webform field widget.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: Array of default values for this field.

int $delta: The order of this item in the array of sub-elements (0, 1, 2, etc.).

array $element: A form element array containing basic properties for the widget.

array $form: The form structure where widgets are being attached to. This might be a full form structure, or a sub-element of a larger form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form elements for a single widget for this field.

Overrides WebformEntityReferenceWidgetTrait::getTargetIdElement

File

src/Plugin/Field/FieldWidget/WebformEntityReferenceAutocompleteWidget.php, line 28

Class

WebformEntityReferenceAutocompleteWidget
Plugin implementation of the 'webform_entity_reference_autocomplete' widget.

Namespace

Drupal\webform\Plugin\Field\FieldWidget

Code

public function getTargetIdElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

  // Get default value.
  $referenced_entities = $items
    ->referencedEntities();
  $default_value = isset($referenced_entities[$delta]) ? $referenced_entities[$delta] : NULL;

  // Append the match operation to the selection settings.
  $selection_settings = $this
    ->getFieldSetting('handler_settings') + [
    'match_operator' => $this
      ->getSetting('match_operator'),
  ];
  return [
    '#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' => $default_value,
    '#size' => $this
      ->getSetting('size'),
    '#placeholder' => $this
      ->getSetting('placeholder'),
  ];
}