You are here

public function FileUploadForm::processUploadElement in Media Directories 8

Same name and namespace in other branches
  1. 3.x modules/media_directories_ui/src/Form/FileUploadForm.php \Drupal\media_directories_ui\Form\FileUploadForm::processUploadElement()
  2. 2.x modules/media_directories_ui/src/Form/FileUploadForm.php \Drupal\media_directories_ui\Form\FileUploadForm::processUploadElement()

Processes an upload (managed_file) element.

Parameters

array $element: The upload element.

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

Return value

array The processed upload element.

File

modules/media_directories_ui/src/Form/FileUploadForm.php, line 132

Class

FileUploadForm
A form to upload files.

Namespace

Drupal\media_directories_ui\Form

Code

public function processUploadElement(array $element, FormStateInterface $form_state) {
  $element['upload_button']['#submit'] = [
    '::uploadButtonSubmit',
  ];

  /** @var \Drupal\media\MediaTypeInterface|string $media_type */
  $media_type = $form_state
    ->get('media_type');

  // Limit the validation errors to make sure
  // FormValidator::handleErrorsWithLimitedValidation doesn't remove the
  // current selection from the form state.
  // @see Drupal\Core\Form\FormValidator::handleErrorsWithLimitedValidation()
  $element['upload_button']['#limit_validation_errors'] = [
    [
      'upload',
    ],
    [
      'current_selection',
    ],
  ];
  $element['upload_button']['#ajax'] = [
    'callback' => '::updateFormCallback',
    'wrapper' => 'media-library-wrapper',
    // Add a fixed URL to post the form since AJAX forms are automatically
    // posted to <current> instead of $form['#action'].
    // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115
    //   is fixed.
    'url' => Url::fromRoute('media_directories_ui.media.add'),
    'options' => [
      'query' => [
        'media_type' => is_object($media_type) ? $media_type
          ->id() : $media_type,
        'target_bundles' => $this
          ->getTargetBundles($form_state),
        'active_directory' => $this
          ->getDirectory($form_state),
        'cardinality' => $this
          ->getCardinality($form_state),
        'selection_mode' => $this
          ->getSelectionMode($form_state),
        FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
      ],
    ],
  ];

  // If a remove button is present, also allow to submit the from using a new button,
  // as the upload_button will be hidden with .hide-js by core.
  if (isset($element['remove_button'])) {
    $element['proceed_button'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Proceed with all files from the list'),
    ];
    if (isset($element['upload_button']['#validate'])) {
      $element['proceed_button']['#validate'] = $element['upload_button']['#validate'];
    }
    if (isset($element['upload_button']['#submit'])) {
      $element['proceed_button']['#submit'] = $element['upload_button']['#submit'];
    }
    if (isset($element['upload_button']['#limit_validation_errors'])) {
      $element['proceed_button']['#limit_validation_errors'] = $element['upload_button']['#limit_validation_errors'];
    }
    if (isset($element['upload_button']['#ajax'])) {
      $element['proceed_button']['#ajax'] = $element['upload_button']['#ajax'];
    }
    if (isset($element['upload_button']['#weight'])) {
      $element['proceed_button']['#weight'] = $element['upload_button']['#weight'];
    }
  }
  return $element;
}