You are here

protected function FileUploadForm::buildInputElement in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::buildInputElement()
  2. 9 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::buildInputElement()

File

core/modules/media_library/src/Form/FileUploadForm.php, line 143

Class

FileUploadForm
Creates a form to create media entities from uploaded files.

Namespace

Drupal\media_library\Form

Code

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;
}