1

I've been trying to implement a redirect to user register page if an anonymous user tries to access a specific page. I used the following code in my custom module.

function myregistration_init(){
  global $user;
  if ($user->uid == 0  && $_GET['q'] == 'order_translaton' ) {
    drupal_goto('user/register');
  }
}

However, this code did not work until I wrote myregistration_init() after the function body. I know that in Drupal modules function works automatically without the need to call it. For instance, the following code works without function call:

function myregistration_form_user_register_form_alter(&$form,&$form_state){
$form['#submit'][] = 'myregistration_submit';
function myregistration_submit(&$form,&$form_state) {
$form_state['redirect']= 'order_translaton';
}
}

Please explain how all this works in Drupal modules.

4

Do not get confused with drupal hooks and functions.

Drupal Hooks:

They allow you to interact with core functionality without hacking it. In our custom module we implement hooks which are invoked by drupal at the event the hook is exposed for automatically. So consider the hook_init(), so when drupal is initialized the system will look inside each contributed, custom module if any of the module has implemented hook_init(), and if so, drupal will invoke functionality written inside the hook implementation for every single module in the order of their module weight.Basically all we do is implement the hook in custom module and clear the caches (clearing the caches is important step as drupal caches all hook implementations so that it doesn't have to look inside every enabled module files to find if there is a matching hook implementation in order to be executed, but since a new hook is added to system we have to clear the cache so that drupal can rebuild information about implemented hooks.), we never explicitly call the hooks, as they are invoked by drupal system itself. So hooks are kind of functions invoked by drupal itself.

functions:

Set of code which gets executed when called explicitly.

About your code

  /**
    * Implements hook_init()
    */
    function myregistration_init(){
      global $user;
      if ($user->uid == 0  && $_GET['q'] == 'order_translaton' ) {
        drupal_goto('user/register');
      }
    }

About your code:

function myregistration_form_user_register_form_alter(&$form,&$form_state){
  $form['#submit'][] = 'myregistration_submit';
  function myregistration_submit(&$form,&$form_state) {
    $form_state['redirect']= 'order_translaton';
  }
}

I am not sure why this code is working, it should be rather like following.

/**
  * Implements hook_form_alter()
  */
function myregistration_form_user_register_form_alter(&$form,&$form_state){
  $form['#submit'][] = 'myregistration_submit';
}

/**
  * Custom submit hanlder.
  */
function myregistration_submit(&$form,&$form_state) {
  $form_state['redirect']= 'order_translaton';
}

Both hook implementation codes above should work, but you might need to clear your caches after implementing the code. But do not call it explicitly anywhere in code.

|improve this answer|||||
  • glad it helped you :) – arpitr Feb 4 '14 at 9:01
0

What is that page.. is it a node? if yes then you need to implement hook_node_view()

and inside it you need to check if it's the node you want to restrict access to it.. and check if the user is anonymous then redirect him where you want

function YOURMODULE_node_view($node, $view_mode) {
  if ($node->type=="YOURTYPE"){
    if ($view_mode == 'full'){          
      global $user;
      if (!$user->uid) {
        drupal_goto('user/register');
      }
    }
  }    
}

If You want to do that another way.. which will help in case the page is not a node then you need to do that check in your theme

function THEMENAME_preprocess_page(&$variables, $hook) {}
|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.