You are here

protected function MediaLibraryUiBuilder::buildMediaTypeAddForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/MediaLibraryUiBuilder.php \Drupal\media_library\MediaLibraryUiBuilder::buildMediaTypeAddForm()

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.

1 call to MediaLibraryUiBuilder::buildMediaTypeAddForm()
MediaLibraryUiBuilder::buildLibraryContent in core/modules/media_library/src/MediaLibraryUiBuilder.php
Build the media library content area.

File

core/modules/media_library/src/MediaLibraryUiBuilder.php, line 289

Class

MediaLibraryUiBuilder
Service which builds the media library.

Namespace

Drupal\media_library

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 [];
  }

  // 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);
  return $this->formBuilder
    ->buildForm($plugin_definition['forms']['media_library_add'], $form_state);
}