Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
Overrides WidgetInterface::formElement
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,
);
}