From c2cd14cfd56c4daf98f6ba748333ccaba2fa6c0d Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Fri, 11 Aug 2023 14:57:14 -0300 Subject: [PATCH] Add config option to redirect after media add. --- config/schema/islandora.schema.yml | 3 +++ islandora.install | 14 ++++++++++++++ islandora.module | 16 +++++++++------- src/Form/IslandoraSettingsForm.php | 9 +++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index 86b65fd0..89c1b58a 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -17,6 +17,9 @@ islandora.settings: delete_media_and_files: type: boolean label: 'Node Delete with Media and Files' + redirect_after_media_save: + type: boolean + label: 'Redirect to node after media save.' upload_form_location: type: string label: 'Upload Form Location' diff --git a/islandora.install b/islandora.install index ad2eb8e1..01e5a467 100644 --- a/islandora.install +++ b/islandora.install @@ -212,3 +212,17 @@ function islandora_update_8007() { // have the here, just in case? throw new UpdateException('Failed; hit the end of the update hook implementation, which is not expected.'); } + +/** + * Set config to no redirect after media save. + */ +function islandora_update_8008() { + $config = \Drupal::configFactory()->getEditable('islandora.settings'); + if ($config) { + $config->set('redirect_after_media_save', FALSE); + $config->save(TRUE); + return t('A new configuration option, "Redirect after media save" is now available. + It has been turned off to preserve existing behaviour. To enable this setting visit + Configuration > Islandora > Core Settings.'); + } +} diff --git a/islandora.module b/islandora.module index 8d4bc905..8d443e1c 100644 --- a/islandora.module +++ b/islandora.module @@ -392,14 +392,16 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) * Redirect submit handler for media save. */ function islandora_media_custom_form_submit(&$form, FormStateInterface $form_state) { - $params = \Drupal::request()->query->all(); - - if (!empty($params)) { - $target_id = $params['edit']['field_media_of']['widget'][0]['target_id']; - $url = Url::fromRoute('entity.node.canonical', ['node' => $target_id]); - $form_state->setRedirectUrl($url); + // Check configuration to see whether a redirect is desired. + $redirect = \Drupal::config('islandora.settings')->get('redirect_after_media_save'); + if ($redirect) { + $params = \Drupal::request()->query->all(); + if (!empty($params)) { + $target_id = $params['edit']['field_media_of']['widget'][0]['target_id']; + $url = Url::fromRoute('view.media_of.page_1', ['node' => $target_id]); + $form_state->setRedirectUrl($url); + } } - } /** diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php index 90e0b420..77d8aa27 100644 --- a/src/Form/IslandoraSettingsForm.php +++ b/src/Form/IslandoraSettingsForm.php @@ -43,6 +43,7 @@ class IslandoraSettingsForm extends ConfigFormBase { ]; const GEMINI_PSEUDO_FIELD = 'field_gemini_uri'; const NODE_DELETE_MEDIA_AND_FILES = 'delete_media_and_files'; + const REDIRECT_AFTER_MEDIA_SAVE = 'redirect_after_media_save'; /** * To list the available bundle types. @@ -210,6 +211,13 @@ class IslandoraSettingsForm extends ConfigFormBase { '#default_value' => (bool) $config->get(self::NODE_DELETE_MEDIA_AND_FILES), ]; + $form[self::REDIRECT_AFTER_MEDIA_SAVE] = [ + '#type' => 'checkbox', + '#title' => $this->t('Redirect after media save.'), + '#description' => $this->t('Redirect to node page after creation of media.'), + '#default_value' => (bool) $config->get(self::REDIRECT_AFTER_MEDIA_SAVE), + ]; + $form[self::FEDORA_URL] = [ '#type' => 'textfield', '#title' => $this->t('Fedora URL'), @@ -361,6 +369,7 @@ class IslandoraSettingsForm extends ConfigFormBase { ->set(self::UPLOAD_FORM_ALLOWED_MIMETYPES, $form_state->getValue(self::UPLOAD_FORM_ALLOWED_MIMETYPES)) ->set(self::GEMINI_PSEUDO, $new_pseudo_types) ->set(self::NODE_DELETE_MEDIA_AND_FILES, $form_state->getValue(self::NODE_DELETE_MEDIA_AND_FILES)) + ->set(self::REDIRECT_AFTER_MEDIA_SAVE, $form_state->getValue(self::REDIRECT_AFTER_MEDIA_SAVE)) ->save(); parent::submitForm($form, $form_state);