function WidgetInterface::singleElementObject
Returns the form for a single field widget.
Field widget form elements should be based on the passed-in $element, which contains the base form element properties derived from the field configuration.
The BaseWidget methods will set the weight, field name and delta values for each form element. If there are multiple values for this field, the formElement() method will be called as many times as needed.
Other modules may alter the form element provided by this function using hook_field_widget_single_element_form_alter() or hook_field_widget_single_element_WIDGET_TYPE_form_alter().
The FAPI element callbacks (such as #process, #element_validate, #value_callback, etc.) used by the widget do not have access to the original $field_definition passed to the widget's constructor. Therefore, if any information is needed from that definition by those callbacks, the widget implementing this method, or a hook_field_widget[_WIDGET_TYPE]_form_alter() implementation, must extract the needed properties from the field definition and set them as ad-hoc $element['#custom'] properties, for later use by its element callbacks.
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.).
\Drupal\Core\Render\Element\Widget $widget: A widget element.
\Drupal\Core\Render\Element\ElementInterface $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
\Drupal\Core\Render\Element\ElementInterface The wrapper object. Some widgets need to change the type of it so the returned object might not be a Wrapper object.
See also
hook_field_widget_single_element_form_alter()
hook_field_widget_single_element_WIDGET_TYPE_form_alter()
1 method overrides WidgetInterface::singleElementObject()
- WidgetBase::singleElementObject in core/
lib/ Drupal/ Core/ Field/ WidgetBase.php - Returns the form for a single field widget.
File
-
core/
lib/ Drupal/ Core/ Field/ WidgetInterface.php, line 151
Class
- WidgetInterface
- Interface definition for field widget plugins.
Namespace
Drupal\Core\FieldCode
public function singleElementObject(FieldItemListInterface $items, $delta, Widget $widget, ElementInterface $form, FormStateInterface $form_state) : ElementInterface;
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.