0

I'm running a db_query to check if the user has taken a specific action. If they haven't, I'm redirecting them to the page where they can take that action.

function my_special_module_node_view(...) {
   db_query(...);

   if( $fails ) { 
     drupal_set_message(t('Error'), 'error');
     drupal_goto('new_page');
   }
}

hook_node_view is mostly working right. It redirects from most pages except any user account pages. The only problem is it doesn't catch when they are trying to create a node. What hook can I use to redirect the user before they view the node creation form?

Update

I've switched it to hook_page_alter. This catches all URLs including the node creation form, which is progress. I had to add in some logic to skip certain paths (including the new_page path to avoid an infinite loop). It seems to be working much better, except if the user does not have access to a page it still ends up in an infinite loop. My current code:

function my_special_module_page_alter() {
  db_query(...);

  if( $fails ) {
    $base_path = arg(0);
    if( !in_array($base_path, array('new_page', 'user')) ) {
      drupal_set_message(t('Error'), 'error');
      drupal_goto('new_page');
    }
  }
}

Any ideas why the pages the user does not have access to end up in a redirect loop? Is there a simple function I can call within hook_page_alter that checks "if user_has_access_to_this_page"?

  • I assume the page you are redirecting them to they have access to view? – rooby Mar 17 '14 at 2:43
  • Hooks aside, why are you trying to redirect a user somewhere else when they intend to create a node? Sounds like a user-experience nightmare. – Randell Mar 17 '14 at 3:41
  • @rooby yes, they do have access to that page. – user11312 Mar 17 '14 at 15:08
  • @Randell basically I'm checking if they've filled out certain profile fields. If they haven't, I redirect them to edit their profile with an error message explaining what they need to fill out first. After they fill it out they won't be redirected. It's a bit more complex than that, it couldn't be done with required fields. – user11312 Mar 17 '14 at 15:09
1

If I am understanding this correctly, this might be a job for hook_init(), eg something along these lines:

function YOURMODULE_init() {
  global $user;

  if ($user->uid) {

    $fails = /* whatever */ ;

    if ($_GET['q']!='node/add/foo') {

      if ($fails) {
        drupal_set_message('How about creating foo first...');
        drupal_goto('node/add/foo');
      }

    }

  }

}
  • Thank you! Yes, I wanted it as early as possible to avoid doing work needlessly only to have to redirect. Also this fixed the issue with the infinite loop on pages they didn't have access to. – user11312 Mar 17 '14 at 15:15
  • Note that hook_init doesn't run on cached pages. You need hook_boot for that. Depends on your requirements as to which one is right for you. – rooby Mar 17 '14 at 23:49
  • Very true @rooby but given the OP mentioned check if the user has taken a specific action, I am presuming the page won't be cached because it is being called for an authenticated user. – Jimajamma Mar 17 '14 at 23:53
  • Yeah sure, I agree that is most likely based on the question. – rooby Mar 18 '14 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy