function FileWidget::formElement
Same name in other branches
- 9 core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formElement()
- 10 core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formElement()
- 11.x core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formElement()
Overrides WidgetInterface::formElement
1 call to FileWidget::formElement()
- ImageWidget::formElement in core/
modules/ image/ src/ Plugin/ Field/ FieldWidget/ ImageWidget.php - Returns the form for a single field widget.
1 method overrides FileWidget::formElement()
- ImageWidget::formElement in core/
modules/ image/ src/ Plugin/ Field/ FieldWidget/ ImageWidget.php - Returns the form for a single field widget.
File
-
core/
modules/ file/ src/ Plugin/ Field/ FieldWidget/ FileWidget.php, line 207
Class
- FileWidget
- Plugin implementation of the 'file_generic' widget.
Namespace
Drupal\file\Plugin\Field\FieldWidgetCode
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$field_settings = $this->getFieldSettings();
// The field settings include defaults for the field type. However, this
// widget is a base class for other widgets (e.g., ImageWidget) that may act
// on field types without these expected settings.
$field_settings += [
'display_default' => NULL,
'display_field' => NULL,
'description_field' => NULL,
];
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$defaults = [
'fids' => [],
'display' => (bool) $field_settings['display_default'],
'description' => '',
];
// Essentially we use the managed_file type, extended with some
// enhancements.
$element_info = $this->elementInfo
->getInfo('managed_file');
$element += [
'#type' => 'managed_file',
'#upload_location' => $items[$delta]->getUploadLocation(),
'#upload_validators' => $items[$delta]->getUploadValidators(),
'#value_callback' => [
get_class($this),
'value',
],
'#process' => array_merge($element_info['#process'], [
[
get_class($this),
'process',
],
]),
'#progress_indicator' => $this->getSetting('progress_indicator'),
// Allows this field to return an array instead of a single value.
'#extended' => TRUE,
// Add properties needed by value() and process() methods.
'#field_name' => $this->fieldDefinition
->getName(),
'#entity_type' => $items->getEntity()
->getEntityTypeId(),
'#display_field' => (bool) $field_settings['display_field'],
'#display_default' => $field_settings['display_default'],
'#description_field' => $field_settings['description_field'],
'#cardinality' => $cardinality,
];
$element['#weight'] = $delta;
// Field stores FID value in a single mode, so we need to transform it for
// form element to recognize it correctly.
if (!isset($items[$delta]->fids) && isset($items[$delta]->target_id)) {
$items[$delta]->fids = [
$items[$delta]->target_id,
];
}
$element['#default_value'] = $items[$delta]->getValue() + $defaults;
$default_fids = $element['#extended'] ? $element['#default_value']['fids'] : $element['#default_value'];
if (empty($default_fids)) {
$file_upload_help = [
'#theme' => 'file_upload_help',
'#description' => $element['#description'],
'#upload_validators' => $element['#upload_validators'],
'#cardinality' => $cardinality,
];
$element['#description'] = \Drupal::service('renderer')->renderPlain($file_upload_help);
$element['#multiple'] = $cardinality != 1 ? TRUE : FALSE;
if ($cardinality != 1 && $cardinality != -1) {
$element['#element_validate'] = [
[
get_class($this),
'validateMultipleCount',
],
];
}
}
return $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.