function FileUploadForm::validateUploadElement

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

Validates the upload element.

Parameters

array $element: The upload element.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array The processed upload element.

File

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

Class

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

Namespace

Drupal\media_library\Form

Code

public function validateUploadElement(array $element, FormStateInterface $form_state) {
    if ($form_state::hasAnyErrors()) {
        // When an error occurs during uploading files, remove all files so the
        // user can re-upload the files.
        $element['#value'] = [];
    }
    $values = $form_state->getValue('upload', []);
    if (count($values['fids']) > $element['#cardinality'] && $element['#cardinality'] !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
        $form_state->setError($element, $this->t('A maximum of @count files can be uploaded.', [
            '@count' => $element['#cardinality'],
        ]));
        $form_state->setValue('upload', []);
        $element['#value'] = [];
    }
    return $element;
}

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