3 namespace Drupal\Core\Field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Symfony\Component\Validator\ConstraintViolationInterface;
9 * Interface definition for field widget plugins.
11 * This interface details the methods that most plugin implementations will want
12 * to override. See Drupal\Core\Field\WidgetBaseInterface for base
13 * wrapping methods that should most likely be inherited directly from
14 * Drupal\Core\Field\WidgetBase..
16 * @ingroup field_widget
18 interface WidgetInterface extends WidgetBaseInterface {
21 * Returns a form to configure settings for the widget.
23 * Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow
24 * administrators to configure the widget. The field_ui module takes care of
25 * handling submitted form values.
28 * The form where the settings form is being included in.
29 * @param \Drupal\Core\Form\FormStateInterface $form_state
30 * The current state of the form.
33 * The form definition for the widget settings.
35 public function settingsForm(array $form, FormStateInterface $form_state);
38 * Returns a short summary for the current widget settings.
40 * If an empty result is returned, a UI can still be provided to display
41 * a settings form in case the widget has configurable settings.
44 * A short summary of the widget settings.
46 public function settingsSummary();
49 * Returns the form for a single field widget.
51 * Field widget form elements should be based on the passed-in $element, which
52 * contains the base form element properties derived from the field
55 * The BaseWidget methods will set the weight, field name and delta values for
56 * each form element. If there are multiple values for this field, the
57 * formElement() method will be called as many times as needed.
59 * Other modules may alter the form element provided by this function using
60 * hook_field_widget_form_alter() or
61 * hook_field_widget_WIDGET_TYPE_form_alter().
63 * The FAPI element callbacks (such as #process, #element_validate,
64 * #value_callback, etc.) used by the widget do not have access to the
65 * original $field_definition passed to the widget's constructor. Therefore,
66 * if any information is needed from that definition by those callbacks, the
67 * widget implementing this method, or a
68 * hook_field_widget[_WIDGET_TYPE]_form_alter() implementation, must extract
69 * the needed properties from the field definition and set them as ad-hoc
70 * $element['#custom'] properties, for later use by its element callbacks.
72 * @param \Drupal\Core\Field\FieldItemListInterface $items
73 * Array of default values for this field.
75 * The order of this item in the array of sub-elements (0, 1, 2, etc.).
76 * @param array $element
77 * A form element array containing basic properties for the widget:
78 * - #field_parents: The 'parents' space for the field in the form. Most
79 * widgets can simply overlook this property. This identifies the
80 * location where the field values are placed within
81 * $form_state->getValues(), and is used to access processing
82 * information for the field through the getWidgetState() and
83 * setWidgetState() methods.
84 * - #title: The sanitized element label for the field, ready for output.
85 * - #description: The sanitized element description for the field, ready
87 * - #required: A Boolean indicating whether the element value is required;
88 * for required multiple value fields, only the first widget's values are
90 * - #delta: The order of this item in the array of sub-elements; see $delta
93 * The form structure where widgets are being attached to. This might be a
94 * full form structure, or a sub-element of a larger form.
95 * @param \Drupal\Core\Form\FormStateInterface $form_state
96 * The current state of the form.
99 * The form elements for a single widget for this field.
101 * @see hook_field_widget_form_alter()
102 * @see hook_field_widget_WIDGET_TYPE_form_alter()
104 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state);
107 * Assigns a field-level validation error to the right widget sub-element.
109 * Depending on the widget's internal structure, a field-level validation
110 * error needs to be flagged on the right sub-element.
112 * @param array $element
113 * An array containing the form element for the widget, as generated by
115 * @param \Symfony\Component\Validator\ConstraintViolationInterface $violation
116 * A constraint violation reported during the validation phase.
118 * The form structure where field elements are attached to. This might be a
119 * full form structure, or a sub-element of a larger form.
120 * @param \Drupal\Core\Form\FormStateInterface $form_state
121 * The current state of the form.
124 * The element on which the error should be flagged, or FALSE to completely
125 * ignore the violation (use with care!).
127 public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state);
130 * Massages the form values into the format expected for field values.
132 * @param array $values
133 * The submitted form values produced by the widget.
134 * - If the widget does not manage multiple values itself, the array holds
135 * the values generated by the multiple copies of the $element generated
136 * by the formElement() method, keyed by delta.
137 * - If the widget manages multiple values, the array holds the values
138 * of the form element generated by the formElement() method.
140 * The form structure where field elements are attached to. This might be a
141 * full form structure, or a sub-element of a larger form.
142 * @param \Drupal\Core\Form\FormStateInterface $form_state
146 * An array of field values, keyed by delta.
148 public function massageFormValues(array $values, array $form, FormStateInterface $form_state);
151 * Returns if the widget can be used for the provided field.
153 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
154 * The field definition that should be checked.
157 * TRUE if the widget can be used, FALSE otherwise.
159 public static function isApplicable(FieldDefinitionInterface $field_definition);