You are here

protected function MediaDirectoriesLibraryUiBuilder::buildMediaTypeAddForm in Media Directories 3.x

Get the add form for the selected media type.

Parameters

\Drupal\media_library\MediaLibraryState $state: The current state of the media library, derived from the current request.

Return value

array The render array for the media type add form.

Overrides MediaLibraryUiBuilder::buildMediaTypeAddForm

1 call to MediaDirectoriesLibraryUiBuilder::buildMediaTypeAddForm()
MediaDirectoriesLibraryUiBuilder::buildLibraryUpload in modules/media_directories_ui/src/MediaDirectoriesLibraryUiBuilder.php

File

modules/media_directories_ui/src/MediaDirectoriesLibraryUiBuilder.php, line 197

Class

MediaDirectoriesLibraryUiBuilder
Service which builds the media library.

Namespace

Drupal\media_directories_ui

Code

protected function buildMediaTypeAddForm(MediaLibraryState $state) {
  $selected_type_id = $state
    ->getSelectedTypeId();
  if (!$this->entityTypeManager
    ->getAccessControlHandler('media')
    ->createAccess($selected_type_id)) {
    return [];
  }
  $selected_type = $this->entityTypeManager
    ->getStorage('media_type')
    ->load($selected_type_id);
  $plugin_definition = $selected_type
    ->getSource()
    ->getPluginDefinition();
  if (empty($plugin_definition['forms']['media_library_add'])) {
    return [];
  }
  $source_field = $selected_type
    ->getSource()
    ->getConfiguration()['source_field'];
  $field_config = $this->entityTypeManager
    ->getStorage('field_config')
    ->load('media.' . $selected_type
    ->id() . '.' . $source_field);

  // After the form to add new media is submitted, we need to rebuild the
  // media library with a new instance of the media add form. The form API
  // allows us to do that by forcing empty user input.
  // @see \Drupal\Core\Form\FormBuilder::doBuildForm()
  $form_state = new FormState();
  if ($state
    ->get('_media_library_form_rebuild')) {
    $form_state
      ->setUserInput([]);
    $state
      ->remove('_media_library_form_rebuild');
  }
  $form_state
    ->set('media_library_state', $state);
  if (in_array($field_config
    ->getType(), [
    'file',
    'image',
  ])) {
    $form = \Drupal::formBuilder()
      ->buildForm(FileUploadForm::class, $form_state);
  }
  else {
    $form = \Drupal::formBuilder()
      ->buildForm(OEmbedForm::class, $form_state);
  }
  return $form;
}