function WidgetBase::form
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::form()
- 10 core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::form()
- 11.x core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::form()
Overrides WidgetBaseInterface::form
3 calls to WidgetBase::form()
- MediaLibraryWidget::form in core/
modules/ media_library/ src/ Plugin/ Field/ FieldWidget/ MediaLibraryWidget.php - Creates a form element for a field.
- ModerationStateWidget::form in core/
modules/ content_moderation/ src/ Plugin/ Field/ FieldWidget/ ModerationStateWidget.php - Creates a form element for a field.
- TestFieldWidgetMultilingual::form in core/
modules/ field/ tests/ modules/ field_test/ src/ Plugin/ Field/ FieldWidget/ TestFieldWidgetMultilingual.php - Creates a form element for a field.
3 methods override WidgetBase::form()
- MediaLibraryWidget::form in core/
modules/ media_library/ src/ Plugin/ Field/ FieldWidget/ MediaLibraryWidget.php - Creates a form element for a field.
- ModerationStateWidget::form in core/
modules/ content_moderation/ src/ Plugin/ Field/ FieldWidget/ ModerationStateWidget.php - Creates a form element for a field.
- TestFieldWidgetMultilingual::form in core/
modules/ field/ tests/ modules/ field_test/ src/ Plugin/ Field/ FieldWidget/ TestFieldWidgetMultilingual.php - Creates a form element for a field.
File
-
core/
lib/ Drupal/ Core/ Field/ WidgetBase.php, line 67
Class
- WidgetBase
- Base class for 'Field widget' plugin implementations.
Namespace
Drupal\Core\FieldCode
public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
$field_name = $this->fieldDefinition
->getName();
$parents = $form['#parents'];
// Store field information in $form_state.
if (!static::getWidgetState($parents, $field_name, $form_state)) {
$field_state = [
'items_count' => count($items),
'array_parents' => [],
];
static::setWidgetState($parents, $field_name, $form_state, $field_state);
}
// Collect widget elements.
$elements = [];
// If the widget is handling multiple values (e.g Options), or if we are
// displaying an individual element, just get a single form element and make
// it the $delta value.
if ($this->handlesMultipleValues() || isset($get_delta)) {
$delta = $get_delta ?? 0;
$element = [
'#title' => $this->fieldDefinition
->getLabel(),
'#description' => $this->getFilteredDescription(),
];
$element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
if (isset($get_delta)) {
// If we are processing a specific delta value for a field where the
// field module handles multiples, set the delta in the result.
$elements[$delta] = $element;
}
else {
// For fields that handle their own processing, we cannot make
// assumptions about how the field is structured, just merge in the
// returned element.
$elements = $element;
}
}
}
else {
$elements = $this->formMultipleElements($items, $form, $form_state);
}
// Allow modules to alter the field multi-value widget form element.
// This hook can also be used for single-value fields.
$context = [
'form' => $form,
'widget' => $this,
'items' => $items,
'default' => $this->isDefaultValueWidget($form_state),
];
\Drupal::moduleHandler()->alterDeprecated('Deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use hook_field_widget_complete_form_alter or hook_field_widget_complete_WIDGET_TYPE_form_alter instead. See https://www.drupal.org/node/3180429.', [
'field_widget_multivalue_form',
'field_widget_multivalue_' . $this->getPluginId() . '_form',
], $elements, $form_state, $context);
// Populate the 'array_parents' information in $form_state->get('field')
// after the form is built, so that we catch changes in the form structure
// performed in alter() hooks.
$elements['#after_build'][] = [
static::class,
'afterBuild',
];
$elements['#field_name'] = $field_name;
$elements['#field_parents'] = $parents;
// Enforce the structure of submitted values.
$elements['#parents'] = array_merge($parents, [
$field_name,
]);
// Most widgets need their internal structure preserved in submitted values.
$elements += [
'#tree' => TRUE,
];
$field_widget_complete_form = [
// Aid in theming of widgets by rendering a classified container.
'#type' => 'container',
// Assign a different parent, to keep the main id for the widget itself.
'#parents' => array_merge($parents, [
$field_name . '_wrapper',
]),
'#attributes' => [
'class' => [
'field--type-' . Html::getClass($this->fieldDefinition
->getType()),
'field--name-' . Html::getClass($field_name),
'field--widget-' . Html::getClass($this->getPluginId()),
],
],
'widget' => $elements,
];
// Allow modules to alter the field widget form element.
$context = [
'form' => $form,
'widget' => $this,
'items' => $items,
'default' => $this->isDefaultValueWidget($form_state),
];
\Drupal::moduleHandler()->alter([
'field_widget_complete_form',
'field_widget_complete_' . $this->getPluginId() . '_form',
], $field_widget_complete_form, $form_state, $context);
return $field_widget_complete_form;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.