public function MediaCombinedUploadForm::updateFormCallback in Media Directories 2.x
Same name and namespace in other branches
- 8 modules/media_directories_ui/src/Form/MediaCombinedUploadForm.php \Drupal\media_directories_ui\Form\MediaCombinedUploadForm::updateFormCallback()
AJAX callback to update the entire form based on source field input.
Parameters
array $form: The complete form.
\Drupal\Core\Form\FormStateInterface $form_state: The current form state.
Return value
\Drupal\Core\Ajax\AjaxResponse|array The form render array or an AJAX response object.
Overrides AddMediaFormBase::updateFormCallback
File
- modules/
media_directories_ui/ src/ Form/ MediaCombinedUploadForm.php, line 155
Class
- MediaCombinedUploadForm
- A form to upload media of different bundles.
Namespace
Drupal\media_directories_ui\FormCode
public function updateFormCallback(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
$wrapper_id = $triggering_element['#ajax']['wrapper'];
$added_media = $form_state
->get('media');
$response = new AjaxResponse();
// When the source field input contains errors, replace the existing form to
// let the user change the source field input. If the user input is valid,
// the entire modal is replaced with the second step of the form to show the
// form fields for each media item.
if ($form_state::hasAnyErrors()) {
$response
->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $form));
return $response;
}
// Check if the remove button is clicked.
if (end($triggering_element['#parents']) === 'remove_button') {
// When the list of added media is empty, return to the media library and
// shift focus back to the first tabbable element (which should be the
// source field).
if (empty($added_media)) {
$build = [
'#theme' => 'media_directories_add',
'#selected_type' => 'combined_upload',
'#active_directory' => -1,
'#target_bundles' => $this
->getTargetBundles($form_state),
'#media_library_form_rebuild' => TRUE,
];
$form_state
->setRebuild();
$response
->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $build));
}
else {
$response
->addCommand(new ReplaceCommand("#{$wrapper_id}", $form));
}
}
else {
$response
->addCommand(new ReplaceCommand("#{$wrapper_id}", $form));
}
return $response;
}