function FileUploadForm::buildInputElement

Same name in other branches
  1. 9 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::buildInputElement()
  2. 8.9.x core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::buildInputElement()
  3. 10 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::buildInputElement()

Overrides AddFormBase::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(),
        // Set multiple to true only if available slots is not exactly one
        // to ensure correct language (singular or plural) in UI
'#multiple' => $slots != 1 ? TRUE : FALSE,
        // Do not limit the number uploaded. There is validation based on the
        // number selected in the media library that prevents overages.
        // @see Drupal\media_library\Form\AddFormBase::updateLibrary()
'#cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
        '#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
        ->renderInIsolation($file_upload_help);
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.