How can I redirect a Drupal user after they create new content - drupal

I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!

As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.

This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.

Related

How do you prevent Drupal 8 from redirecting to the page when a new page has been created?

When you create a new page in Drupal 8 it redirects the user to that page on the site, I would like it to redirect back to the content section of the Admin interface.
Is there a way to do this?
You need to create a custom module and use
hook_form_alter()
You would start by checking what form it is and then add a custom submit callback like so
function your_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// test you are altering the correct form so wrap the below in an IF
$form['actions']['submit']['#submit'][] = '_your_module_custom_redirect';
}
function _your_module_custom_redirect($form, FormStateInterface $form_state) {
$form_state->setRedirect('your route name');
}
or possibly use hook_ENTITY_TYPE_insert() as I think hook_form_alter will run regardless of validation, but I am not 100% on that

How to redirect user after registration on Drupal 8?

How to redirect user to a particular page after the user is successfully registered into the site?
Here drupal_goto() does not work. We need to work with the form redirection. How can this be achieved?
// include the Url namespace in your custom module
use Drupal\Core\Url;
// Add CUSTOM SUBMIT HANDLER FOR REGISTRATION
$form['actions']['submit']['#submit'][] = '__tmp_registration_submit';
function __tmp_registration_submit(&$form, FormStateInterface $form_state) {
// set relative internal path
$redirect_path = "\user\uid\payment-history";
$url = url::fromUserInput($redirect_path);
// set redirect
$form_state->setRedirectUrl($url);
}
Maybe a bit heavy, but you could also use the Rules module.

How to logout and display message after login under certain condition?

Given a Drupal 7 website I want to customize log in behavior: When a users logs in I want to check if they are in a blacklist. If they are in that list I want them to be automatically logged out and told about the reason they are being kicked out. So In one of the custom modules of this drupal I have add the following hook:
function mymodule_user_login(&$edit, $account) {
if(blacklist(&$edit, $account)) {
drupal_goto("/user/logout/");
drupal_set_message('Acces denied','error');
}
}
However, my code doesn't work. What I'm doing wrong? I'm sure the hook is executed because I checked this using a watchdog. However, I also discovered the hook only gets executed if admin is the user who is logging in.
Are there any alternatives (maybe using Context module)? Any suggestions would be aprreciated!
Thanks!
Your code does not work because drupal_goto() calls drupal_exit() so the rest of your script does not get executed.
What you are trying to do will not work because user_logout() itself calls drupal_goto() to the front page.
You can however add an extra validation callback in the user login form so you can prevent users from logging in altogether.
<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login':
case 'user_login_block':
$form['#validate'][] = 'mymodule_custom_user_validation',
break;
}
}
function mymodule_custom_user_valudation($form, &$form_state) {
if (// Add your blacklist conditons here. ) {
form_set_error('', t('Your account is blacklisted, therefore you cannot login to this account'))
}
}
?>
If your interested in using the Login Destination Module, then you could create some type of rule to hook into your blacklist check and then redirect that user to the "user/logout" path. Let me know if you have any questions.
What exactly is your "blacklist()" function doing?

Close Overlay and Redirect to Custom URL on Save-Node in Drupal 7?

When my node form saves, I want to close the admin overlay and redirect to a custom URL that is stored with the node. hook_form_alter() is setting $form['#redirect'] but I think that would only work with no admin overlay.
I have never used it before myself, but i think you can call the overlay_close_dialog(...) function from your hook_submit
See http://api.drupal.org/api/drupal/modules--overlay--overlay.module/function/overlay_close_dialog/7 for more info
Kept googling my way here as well.. this is the working solution:
Inside your form:
$form['somebutton']['#submit'] = array('your_custom_callback');
Add a custom callback
function your_custom_callback($form, &$form_state) {
//redirect users to Drupal.org
$url = "http://drupal.org";
if (module_exists('overlay') && overlay_get_mode() == 'child') {
unset($_GET['destination']);
overlay_close_dialog($url, array('external' => TRUE));
$form_state['redirect'] = FALSE;
} else {
$form_state['redirect'] = $url;
}
}
Kept googling my way here and wanted to post a final solution that worked for me on Drupal 7 (got here thanks to jakraska's suggestion). First use hook_form_FORM_ID_alter() to hook into the specific form you want to alter
/**
* Implementation of hook_form_FORM_ID_alter()
*
**/
function mymodule_form_FORM_ID_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_callback';
}
Then write your callback that will render the page. In my case I simply wanted to close the overlay, so that's why I used the $form_state['redirect'] = FALSE;
function mymodule_callback(&$form, &$form_state) {
// Form API will re-render the current page and pass the redirect information to the overlay JavaScript
overlay_close_dialog();
// stay on the same page after all submit callbacks have been processed.
$form_state['redirect'] = FALSE;
}
If you want to redirect to another path,
See https://api.acquia.com/api/drupal/modules!overlay!overlay.module/function/overlay_form_submit/7 - I believe that should get you there.
Make sure to use $form_state['rebuild'] = TRUE; inside your hook_submit function, otherwise it would not close overlay after form is submitted.

Drupal - page route - content profile

I can't seem to get the page route module to move to the next page, after it creates the profile page.
1. I created a route called Registration
2. Inside the route I have two pages,
a) Content Profile Editing Form
b) Node Add form
The page route should take the user to the profile create page, and then route to the create a group node page.
Problem is after the user is directed to the content profile editing form and clicks next, it redirects back to the profile form instead of going to the next page.
Any ideas, this does not seem normal at all.
Charles
thanks for the help. I found the solution. I thought it might be a module or a coding change I have made so I decided to create a new drupal install and only install the modules needed.
I still had the same problem, very strange indeed. I eventually found the this article, with someone whom has had the same issues.
http://drupal.org/node/699458
The following needs to be added to the pageroute.module
<?php
/**
* Submit function for all pageroute forms, except submit-like tab buttons
* Redirect to the set target.
*/
function pageroute_page_form_submit($form, &$form_state) {
$page = &$form_state['page'];
$route = &$form_state['storage']['route'];
/* hack saturnino part */
if(!empty( $page->options['neighbours']['forward']) )
{
drupal_redirect_form($form, $route->path.'/'.$page->options['neighbours']['forward']);
return;
}
/* hack saturnino part */
// no page access -> try redirect
if (!$route->checkPageAccess($page->name, $form_state['target'])) {
unset($form_state['storage']);
$form_state['rebuild'] = FALSE;
if ($route->options['redirect_path']) {
drupal_redirect_form($form, pageroute_get_redirect_path($page));
return;
}
drupal_not_found();
pageroute_exit_now();
return;
}
$form_state['rebuild'] = TRUE;
}
?>
thanks for your help

Resources