public function AutocompleteWidgetBase::formElement

Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

Overrides WidgetInterface::formElement

1 call to AutocompleteWidgetBase::formElement()
AutocompleteWidget::formElement in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php
Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::formElement().
1 method overrides AutocompleteWidgetBase::formElement()
AutocompleteWidget::formElement in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php
Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::formElement().

File

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

Class

AutocompleteWidgetBase
Parent plugin for entity reference autocomplete widgets.

Namespace

Drupal\entity_reference\Plugin\field\widget

Code

public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
  global $user;
  $instance = $this->instance;
  $field = $this->field;
  $entity = isset($element['#entity']) ? $element['#entity'] : NULL;

  // Prepare the autocomplete path.
  $autocomplete_path = $this
    ->getSetting('autocomplete_path');
  $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';

  // Use <NULL> as a placeholder in the URL when we don't have an entity.
  // Most web servers collapse two consecutive slashes.
  $id = 'NULL';
  if ($entity && ($entity_id = $entity
    ->id())) {
    $id = $entity_id;
  }
  $autocomplete_path .= $id;
  $element += array(
    '#type' => 'textfield',
    '#maxlength' => 1024,
    '#default_value' => implode(', ', $this
      ->getLabels($items)),
    '#autocomplete_path' => $autocomplete_path,
    '#size' => $this
      ->getSetting('size'),
    '#placeholder' => $this
      ->getSetting('placeholder'),
    '#element_validate' => array(
      array(
        $this,
        'elementValidate',
      ),
    ),
    // @todo: Use wrapper to get the user if exists or needed.
    '#autocreate_uid' => isset($entity->uid) ? $entity->uid : $user->uid,
  );
  return array(
    'target_id' => $element,
  );
}