3

I would like to know what is the best option to setup a Drupal redirect process that will look at the current time of day and then redirect the user to a page based on this information?

Since I have not had to do this before in Drupal, I'm looking for one or two suggestions on how to do this with the user starting the action from a menu item.

One thought I had is that I could create a page that is PHP content and determine the redirect there, but I'm unsure of how to code that in that location.

Also, I'm not sure if I've overlooked a module that could help with a conditional redirect or if all the references to form redirects might be applicable to what I'm trying to do.

1 Answer 1

4

You could do something along the lines of this:

function MYMODULE_menu() {

  $items['MYMODULE/whattodo'] = array(
    'title' => 'Where should I go now, George',
    'page callback' => 'MYMODULE_whattodo',
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function MYMODULE_whattodo() {

  $foo= // code to get the current time of day in whatever form you want that matches the switch cases below

  switch($foo) {
     case // morning:
        drupal_goto(// page for the morning);
        break;
     case // afternoon:
        drupal_goto(// page for the afternoon);
        break;
     case // evening:
        drupal_goto(// page for the evening);
        break;
  }
}

which means when a user went to www.mysite.com/MYMODULE/whattodo, they would be redirected to whatever page you set up via drupal_goto()

1
  • 1
    One note on this is that you need to make sure the module [Menu Access] and [Menu Item Access] are disabled, otherwise, the menu item is blocked. I'm sure you could go and enable this in the example, but I didn't go that far to figure it out.
    – KodeTitan
    Commented Mar 4, 2012 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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