From 0836e9e25b44dc83836f04f063d70f123243f530 Mon Sep 17 00:00:00 2001 From: William Hearn Date: Wed, 15 Nov 2017 23:18:20 -0500 Subject: [PATCH] Issue #2817109 by rachel_norfolk, ericras: How to redirect to the owning group after adding a gnode? --- config/install/group.settings.yml | 1 + config/schema/group.schema.yml | 3 +++ src/Entity/Form/GroupContentForm.php | 23 +++++++++++++++++++++++ src/Form/GroupSettingsForm.php | 17 +++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/config/install/group.settings.yml b/config/install/group.settings.yml index 4950007..146e224 100644 --- a/config/install/group.settings.yml +++ b/config/install/group.settings.yml @@ -1 +1,2 @@ use_admin_theme: TRUE +redirect_to: 'group_content_entity' diff --git a/config/schema/group.schema.yml b/config/schema/group.schema.yml index 340838e..f8832c7 100644 --- a/config/schema/group.schema.yml +++ b/config/schema/group.schema.yml @@ -7,6 +7,9 @@ group.settings: use_admin_theme: type: 'boolean' label: 'Use admin theme when editing or creating groups' + redirect_to: + type: 'text' + label: 'On entity creation, redirect to this entity canonical url' group.type.*: type: 'config_entity' diff --git a/src/Entity/Form/GroupContentForm.php b/src/Entity/Form/GroupContentForm.php index dc5fd0f..89bc75e 100644 --- a/src/Entity/Form/GroupContentForm.php +++ b/src/Entity/Form/GroupContentForm.php @@ -142,6 +142,29 @@ class GroupContentForm extends ContentEntityForm { $form_state->setRedirect(''); } + // The below redirects ensure the user will be redirected to configured + // default location. But only if there was no destination set in the URL. + $config = $this->config('group.settings'); + switch ($config->get('redirect_to')) { + case 'entity': + $entity_type = $group_content->getEntity()->getEntityTypeId(); + $route_params = [$entity_type => $group_content->getEntity()->id()]; + $form_state->setRedirect('entity.' . $entity_type . '.canonical', $route_params); + break; + case 'group_content_entity': + $route_params = [ + 'group' => $group_content->getGroup()->id(), + 'group_content' => $group_content->id(), + ]; + $form_state->setRedirect('entity.group_content.canonical', + $route_params); + break; + case 'group': + $route_params = ['group' => $group_content->getGroup()->id()]; + $form_state->setRedirect('entity.group.canonical', $route_params); + break; + } + return $return; } diff --git a/src/Form/GroupSettingsForm.php b/src/Form/GroupSettingsForm.php index 68e56fe..eb29862 100644 --- a/src/Form/GroupSettingsForm.php +++ b/src/Form/GroupSettingsForm.php @@ -37,6 +37,17 @@ class GroupSettingsForm extends ConfigFormBase { '#description' => $this->t("Enables the administration theme for editing groups, members, etc."), '#default_value' => $config->get('use_admin_theme'), ]; + $form['redirect_to'] = [ + '#type' => 'radios', + '#title' => $this->t('On entity creation, redirect to'), + '#description' => $this->t('When an entity is created via the UI, the user will, by default, be directed to the created entity or the group content entity display.'), + '#default_value' => $config->get('redirect_to'), + '#options' => array( + 'entity' => $this->t('The created entity'), + 'group_content_entity' => $this->t('The created group content entity'), + 'group' => $this->t('The group the entity belongs to'), + ), + ]; return $form; } @@ -55,6 +66,12 @@ class GroupSettingsForm extends ConfigFormBase { \Drupal::service('router.builder')->setRebuildNeeded(); } + $config_redirect_to = $config->get('redirect_to'); + $form_redirect_to = $form_state->getValue('redirect_to'); + if ($config_redirect_to != $form_redirect_to) { + $config->set('redirect_to', $form_redirect_to)->save(); + } + parent::submitForm($form, $form_state); } -- 2.5.4 (Apple Git-61)