Skip to content

Commit

Permalink
Add config option to redirect after media add.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosiel committed Sep 22, 2023
1 parent fdfdd87 commit c2cd14c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions config/schema/islandora.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 14 additions & 0 deletions islandora.install
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
16 changes: 9 additions & 7 deletions islandora.module
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Form/IslandoraSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -210,6 +211,13 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#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'),
Expand Down Expand Up @@ -361,6 +369,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->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);
Expand Down

0 comments on commit c2cd14c

Please sign in to comment.