You are here

public function MediaCombinedUploadForm::buildInputElement in Media Directories 2.x

Same name and namespace in other branches
  1. 8 modules/media_directories_ui/src/Form/MediaCombinedUploadForm.php \Drupal\media_directories_ui\Form\MediaCombinedUploadForm::buildInputElement()

Inheriting classes need to build the desired input element.

Overrides FileUploadForm::buildInputElement

File

modules/media_directories_ui/src/Form/MediaCombinedUploadForm.php, line 86

Class

MediaCombinedUploadForm
A form to upload media of different bundles.

Namespace

Drupal\media_directories_ui\Form

Code

public function buildInputElement(array $form, FormStateInterface $form_state) {
  $target_types = $this
    ->getTargetBundles($form_state);
  $validators_by_media_type = [];
  foreach ($target_types as $type) {
    $validators_by_media_type[$type] = $this
      ->getUploadValidators(MediaType::load($type));
  }
  $pre_render = (array) $this->elementInfo
    ->getInfoProperty('managed_file', '#pre_render', []);
  $form['container']['upload'] = [
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Select files'),
    '#description' => $this
      ->t('Allowed file extensions: @extensions', [
      '@extensions' => $this->mediaDirectoriesUiHelper
        ->getValidExtensions($target_types),
    ]),
    '#multiple' => TRUE,
    // Upload to temporary folder. Needs to be moved into correct folder after saving.
    '#upload_location' => 'temporary://',
    '#upload_validators' => [
      'media_directories_ui_file_validator' => [
        $validators_by_media_type,
      ],
      // We need to respect core's _file_save_upload_single, by giving all extensions again.
      'file_validate_extensions' => [
        $this->mediaDirectoriesUiHelper
          ->getValidExtensions($target_types),
      ],
    ],
    '#process' => [
      [
        'Drupal\\file\\Element\\ManagedFile',
        'processManagedFile',
      ],
      '::processUploadElement',
    ],
    '#pre_render' => array_merge($pre_render, [
      [
        static::class,
        'preRenderUploadElement',
      ],
    ]),
  ];
  return $form;
}