Each user has a "Edit" tab/button on their profile. They click it, edit their profile, and when it is saved, they are kept on that page. I want the user to be taken back to his/her "View" or profile page.
6 Answers
As @Aboodred, if you already have the Rules module installed, adding a simple rule can do the job.
Create a rule with the React on event type
After updating an existing user account
.Add a
Page redirect
action. Set the 'Data selector' field toaccount:url
(either manually or using the dropdown).
-
1
If you want to do it with code instead of rules you can take a look at how the user_edit_cancel_submit
function does it in user.pages.inc.
First you define an submit function:
$form['#submit'][] = 'custom_redirect_after_save';
Then you add the function:
function custom_redirect_after_save($form, &$form_state) {
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
$form_state['redirect'] = array("user", array());
}
In Drupal 8/9:
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_user_form_alter(&$form, &$form_state) {
// Redirect users to their profile pages.
$form['actions']['submit']['#submit'][] = 'MYMODULE_user_edit_form_submit';
}
/**
* Redirect users to their profile pages when submitting the edit form.
*/
function MYMODULE_user_edit_form_submit($form, &$form_state) {
$form_state->setRedirect('entity.user.canonical', ['user' => \Drupal::currentUser()->id()]);
}
You can add additional submit function to user_profile_form
by using hook_form_alter
to return back to profile page after editing user profile.
Your code should look like the following:
function MODULE_NAME_form_user_profile_form_alter(&$form, $form_state, $form_id) {
$form['#submit'][] = 'MODULE_NAME_user_profile_form_submit';
}
function MODULE_NAME_user_profile_form_submit($form, &$form_state) {
global $user;
drupal_goto('user/' . $user->uid);
}
-
1You don't need to do a drupal_goto because you can use a $form_state['redirect'] instead. See the answer from @andeersg. Mar 25, 2016 at 1:10
-
-
@Aboodred1If you use
drupal_goto
you cut off every other submit function or alter hooks. If you set the redirect key in the form_state all the submit functions will execute before redirecting.– andeersgAug 15, 2016 at 11:24
On my system (Drupal 6) it was
1) Install Rules Module:
https://www.drupal.org/project/rules
2) Activate Rules Module in Administration Console
[site url]/admin/build/modules (Look for a new Module category called Rules)
3) Enable the following Module(s):
Rules Administration UI 6.x-1.5 Provides the administration UI for rules. Depends on: Rules (enabled)
Rules Forms support 6.x-1.5 Provides events, conditions and actions for rule-based form customization. Depends on: Rules (enabled)
Rules Scheduler 6.x-1.5 Schedule the execution of rule sets. Depends on: Rules (enabled)
4) Navigate to Rules area and configure the rule:
Rules | Triggered Rules | Add a new rule
....fill in as per Obliquely's instructions...
BUT - There was no DATA SELECTOR or dropdown.
Instead I had to fill it in by hand, which was easy:
To: [account:account-url]
It worked in Drupal 8
use Symfony\Component\HttpFoundation\RedirectResponse;
function YOUR_MODULE_user_update($account) {
$uid = \Drupal::currentUser()->id();
$url = '/u/' . $uid;
$response = new RedirectResponse($url);
$request = \Drupal::request();
$request->getSession()->save();
$response->prepare($request);
\Drupal::service('kernel')->terminate($request, $response);
$response->send();
}
-
This is a bad answer because terminating the kernel early while in a hook means that no other hook will get a chance to operation. Jul 25, 2021 at 19:22