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

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.

Related

Drupal 7 Custom URL in Webform not redirecting

Has anyone found Drupal Webform is ignoring their custom URL in settings?
We set up the form correctly and it had been working previously, however now we're just automatically redirected to the home page with our confirmation message. Any support would be appreciated.
Thanks
In webform submit hook , you can specify redirection :
function my_custom_webform($form, &$form_state){
// Build your form with from api
// add a custom submit if you need , or use core hook_submit
// $form['#submit'][] = 'my_custom_callback_submit';
// for this answer i'll use core hook_submit
return $form;
}
function my_custom_webform_submit($form, &$form_state){
// [.. do some stuff]
// ensure to not have => $form_state['rebuild'] = TRUE;
// because this can't work with redirect
$form_state['redirect'] = 'my/url'; // go to your url
}

Custom Post Type Action Hook / Transients

This question is in regards to a plug-in I'm developing.
I'm trying to fire a function each time a custom post type called "Product" is added or edited. In particular, I need a hook that fires before the meta boxes load on the add/edit page, but that only fires on that "Product" custom post type's edit page.
The function that will fire makes an API request, and caches the response in a transient.
The reason for the action hook is because in my current code, when the transient has expired, the add/edit page is broken during the first page load. However if you refresh the page after that, it shows up as intended. I'm fairly certain this is happening because the current conditional statement that checks the transient is located inside of the function that generates the meta box. So my theory is if I can set up an action hook to check the transient before the meta box is generated, it might solve the problem.
However I've got a second theory that the problem is being caused because of the time it takes to make the API request and return the response is longer than the time it takes for the page to load. So if there is an action hook that will delay page loading until the function finishes executing it would be an ideal solution, but I don't believe such an action hook exists. I'm not even certain if such a delay is possible.
I'd really appreciate any help or alternative suggestions you guys might have. Thanks for your time guys.
Code Example:
add_action( 'edit_product', 'llc_hook_campaign_find_active' );
function llc_hook_campaign_find_active() {
if (!$t_campaign_find_active){
limelight_cart_campaign_find_active();
return false;
}
}
Since you are using an action hook, it is not waiting for your API response.
Try using a filter hook instead.
Try using wp_insert_post_data
function filter_handler( $data , $postarr ) {
//make your API call, get the response and store it in post meta or data wherever you want
$response = 'your API response';
//e.g. update_post_meta($postarr['ID'], 'meta_key', $response); OR
//$data['post_content'] = $response;
return $data;
}
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );
In your case, following should work -
add_filter( 'wp_insert_post_data', 'llc_hook_campaign_find_active', '99', 2 );
function llc_hook_campaign_find_active( $data , $postarr ) {
if (!$t_campaign_find_active){
limelight_cart_campaign_find_active();
return $data;
}
}
I was able to make the API request before the meta boxes loaded on the Admin Add/Edit screen by using the action filter edit_form_top. That particular action hook is fired as soon as the Add/Edit page for any post/page/custom post type is loaded. In order to narrow it down so that the function only fires on the Add/Edit screen for my "product" custom post type, I used get_current_screen() along with an if statement.
add_action('edit_form_top', 'llc_hook_campaign_find_active');
function llc_hook_campaign_find_active() {
//Fetch current screen information
$screen = get_current_screen();
//Check if post type is "product"
if($screen->post_type == "product") {
//API Request that checks for an existing transient
$t_campaign_find_active = get_transient('campaign_find_active');
if (!$t_campaign_find_active){
limelight_cart_campaign_find_active();
return false;
}
}
}
Works like a charm.

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

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.

WordPress redirect after successful form validation and submit

I have a custom page template with a form in page-report.php.
I do validation on it, so I need the action to lead to the same form, but I need to redirect to a different page on successful validation.
wp_redirect() is not working, because is spitting out the header() function after the output was started already.
if($_POST['report'])
{
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
I cannot use ob_start() and ob_flush() because the header is not included in this page template.
I tried to put a function in functions.php :
add_action('get_header','redirect_to');
function redirect_to($page){
if($page)
{
wp_redirect('http://www.google.com');
}
}
But that works only if I don't have the conditional if().
If I use it, the wp_redirect() is being spat out after the output was started.
What is my best approach to do this?
Thanks.
I think you have to use the save_post hook:
do_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($_POST['report']) {
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
Also you could just try using a plugin instead of your own code...Gravity Forms and Contact form 7 both work well.
}
I got it...
Since I was doing everything from inside an admin page, the header was fired up before the wp_redirect() as it was explained in the question.
So I ended up making a new function at the top:
add_action('admin_init','redirect_to');
function redirect_to()
{
if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
wp_redirect($redirect);
die();
}
}
}
That is making sure that the redirect_to() function will be fired up before the header (on admin_init). Maybe not the most elegant solution, but it works perfect.
So in the case anybody is looking for "Redirect after post" in wordpress, this is how you do it:
wp_redirect($_SERVER['REQUEST_URI']);
Try this
if( $_POST['report'] ) {
if( $validator->ValidateForm() ) {
header( 'Location: http://thankyou' ) ;
}
}

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