protected function FileUploadForm::buildInputElement in Drupal 9
Same name and namespace in other branches
- 8 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::buildInputElement()
Builds the element for submitting source field value(s).
The input element needs to have a submit handler to create media items from the user input and store them in the form state using ::processInputValues().
Parameters
array $form: The complete form.
\Drupal\Core\Form\FormStateInterface $form_state: The current form state.
Return value
array The complete form, with the element added.
Overrides AddFormBase::buildInputElement
See also
::processInputValues()
File
- core/
modules/ media_library/ src/ Form/ FileUploadForm.php, line 131
Class
- FileUploadForm
- Creates a form to create media entities from uploaded files.
Namespace
Drupal\media_library\FormCode
protected function buildInputElement(array $form, FormStateInterface $form_state) {
// Create a file item to get the upload validators.
$media_type = $this
->getMediaType($form_state);
$item = $this
->createFileItem($media_type);
/** @var \Drupal\media_library\MediaLibraryState $state */
$state = $this
->getMediaLibraryState($form_state);
if (!$state
->hasSlotsAvailable()) {
return $form;
}
$slots = $state
->getAvailableSlots();
// Add a container to group the input elements for styling purposes.
$form['container'] = [
'#type' => 'container',
];
$process = (array) $this->elementInfo
->getInfoProperty('managed_file', '#process', []);
$form['container']['upload'] = [
'#type' => 'managed_file',
'#title' => $this
->formatPlural($slots, 'Add file', 'Add files'),
// @todo Move validation in https://www.drupal.org/node/2988215
'#process' => array_merge([
'::validateUploadElement',
], $process, [
'::processUploadElement',
]),
'#upload_validators' => $item
->getUploadValidators(),
'#multiple' => $slots > 1 || $slots === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'#cardinality' => $slots,
'#remaining_slots' => $slots,
];
$file_upload_help = [
'#theme' => 'file_upload_help',
'#upload_validators' => $form['container']['upload']['#upload_validators'],
'#cardinality' => $slots,
];
// The file upload help needs to be rendered since the description does not
// accept render arrays. The FileWidget::formElement() method adds the file
// upload help in the same way, so any theming improvements made to file
// fields would also be applied to this upload field.
// @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formElement()
$form['container']['upload']['#description'] = $this->renderer
->renderPlain($file_upload_help);
return $form;
}