You are here

public function AddMediaFormBase::updateLibrary in Media Directories 8

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

AJAX callback to send the new media item(s) to the media library.

Parameters

array $form: The complete form.

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

Return value

array|\Drupal\Core\Ajax\AjaxResponse The form array if there are validation errors, or an AJAX response to add the created items to the current selection.

File

modules/media_directories_ui/src/Form/AddMediaFormBase.php, line 756

Class

AddMediaFormBase
Class AddMediaFormBase.

Namespace

Drupal\media_directories_ui\Form

Code

public function updateLibrary(array &$form, FormStateInterface $form_state) {
  if ($form_state::hasAnyErrors()) {
    return $form;
  }
  $form_state
    ->setStorage([]);
  $form_state
    ->setRebuild();
  $response = new AjaxResponse();
  $response
    ->addCommand(new CloseModalDialogCommand());
  $response
    ->addCommand(new RefreshDirectoryTree($form_state
    ->getValue('most_choosen_directory_tid'), $form_state
    ->getValue('newly_added_media_ids')));
  $status_messages = [
    '#type' => 'status_messages',
  ];
  $messages = \Drupal::service('renderer')
    ->renderRoot($status_messages);
  if (!empty($messages)) {
    $response
      ->addCommand(new OpenModalDialogCommand('', $messages));
  }
  return $response;
}