CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • No Activation Email Sent when using drupal redirect
Pages: [1]

Author Topic: No Activation Email Sent when using drupal redirect  (Read 808 times)

vhilly

  • I’m new here
  • *
  • Posts: 17
  • Karma: 0
  • CiviCRM version: 3.4
  • CMS version: Drupal 7
  • MySQL version: 5
  • PHP version: php 5
No Activation Email Sent when using drupal redirect
October 20, 2011, 04:49:16 am
I am trying to redirect user to success page when registration completed. I use hook_user_insert and place drupal_goto("node/#") inside the hook. Page redirects successfully to page i created  but activation email was not sent to user.  Is there any fix for this bug? I am using drupal 7.
heres my code:

function modulename_user_insert(&$edit, $account, $category){

//do some stuff here eg. custom query

drupal_goto("node/#") //then redirect to success page


}

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: No Activation Email Sent when using drupal redirect
October 20, 2011, 04:14:14 pm
It sounds like there's a race between your module's hook_user and CiviCRM's hook_user -- one does a redirect; the other sends an email; and the order of those operations is undefined/unguaranteed. As it happens, your module's hook_user runs first, performs a redirect, and thereby aborts all processing.

Some folks will tell you to hack the order of module invocations by renaming the module or tweaking the {system} table. You could try that. I've avoided it because you could get boxed into a situation where one use-case (e.g. new user registration) requires a particular module-order while another use-case (e.g. user deletion) requires a different module-order -- and no amount of hacking {system} will satisfy both use-cases.

I prefer a two-hook hack: one hook (e.g. hook_user_insert) detects the interesting business event and a second hook (e.g. hook_civicrm_post or hook_page_build) carries out the final work. To coordinate the two hooks, you can use a global variable.

Example:

Code: [Select]
<?php
function modulename_user_insert(&$edit, $account, $category){
  global 
$_modulename_hackInsertedAccount;
  
$_modulename_hackInsertedAccount = $account;
}

function 
modulename_page_build(...) {
  global 
$_modulename_hackInsertedAccount;
  if (
$_modulename_hackInsertedAccount) {
    
drupal_goto('node/x');
  }
}

Or if you're feeling ambitious, you could generalize the pattern, e.g.

Code: [Select]
<?php
// FILE: modulename.module
function modulename_user_insert(&$edit, $account, $category){
  
// do some work

  // we want to redirect, but we'll wait until hook_page_build runs later on
  
defer('hook_page_build', 'drupal_goto', 'node/x');
}

// FILE: defer.module
/**
 * Defer execution of a function until a subsequent hook invocation
 *
 * ex: defer('hook_somehook', 'myfunction', 'myarg1', 'myarg2', ..);
 */
function defer() {
  
$args = func_get_args();
  
$hook = array_shift($args);

  global 
$_defer_actions;
  
$_defer_actions[$hook][] = $args;
}

// Hook stubs

function defer_page_build(...) {
  
_defer_execute_all('hook_page_build');
}
function 
defer_civicrm_post(...) {
  
_defer_execute_all('hook_civicrm_post');
}

/**
 * Execute any deferred actions which were registered for $hook
 */
function _defer_execute_all($hook) {
  global 
$_defer_actions;
  if (
is_array($_defer_actions[$hook])) {
    foreach (
$_defer_actions[$hook] as $callbackArgs) {
      
$func = array_shift($callbackArgs);
      
call_user_func_array($func, $callbackArgs);
    }
  }
}

FWIW, I don't know which hook would actually work best as the second-hook. The most obvious ones to me are hook_civicrm_post and hook_page_build, but that would require some testing/guessing/checking to figure out.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • No Activation Email Sent when using drupal redirect

This forum was archived on 2017-11-26.