You are here

protected function FileUploadForm::buildInputElement in Media Directories 3.x

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

Builds the element for submitting source field value(s).

The input element needs to have a submit handler to create media items from the user input and store them in the form state using ::processInputValues().

Parameters

array $form: The complete form.

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

Return value

array The complete form, with the element added.

Overrides AddFormBase::buildInputElement

See also

::processInputValues()

File

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

Class

FileUploadForm

Namespace

Drupal\media_directories_ui\Form

Code

protected function buildInputElement(array $form, FormStateInterface $form_state) {
  $max_filesize = \Drupal\Component\Utility\Environment::getUploadMaxSize();
  $media_type = $this
    ->getMediaType($form_state);
  $process = (array) \Drupal::service('element_info')
    ->getInfoProperty('managed_file', '#process', []);
  $source_field = $media_type
    ->getSource()
    ->getConfiguration()['source_field'];
  $field_config = $this->entityTypeManager
    ->getStorage('field_config')
    ->load('media.' . $media_type
    ->id() . '.' . $source_field);
  $form['container']['upload'] = [
    '#type' => 'managed_file',
    '#title' => $field_config
      ->label(),
    '#description' => $this
      ->t('Allowed file extensions: @extensions', [
      '@extensions' => $field_config
        ->getSetting('file_extensions'),
    ]),
    '#upload_validators' => [
      'file_validate_extensions' => [
        $field_config
          ->getSetting('file_extensions'),
      ],
    ],
    '#multiple' => TRUE,
    '#upload_location' => 'public://media-directories/',
    '#process' => array_merge([
      '::validateUploadElement',
    ], $process, [
      '::processUploadElement',
    ]),
  ];
  return $form;
}