Drupal - How to programmatically set the front page?

You can use variable_set() for that.

variable_set('site_frontpage', $value);

There is a module (Frontpage, which is one I maintained) that allows to set a different front page for anonymous users, and authenticated users. The module allows third-party modules to alter the page to which the users are instead redirected, or to alter the structure array used to render the page. Third-party modules are allowed to change the redirection URL only when the front page for anonymous or authenticated has not been set within the Frontpage module, or when there has been an error during the node loading; the module will probably be changed in the future to allow third-party modules to redirect the users to a specific page they select.

As alternative, you can create a custom module that, using code similar to the one used by Frontpage, redirect users to a specific page, basing on specific criteria.

The module should implement hook_menu() and associate a menu callback to, for example, http://example.com/frontpage; the page callback of that menu item should simply verify a condition is verified, and then redirect the users to a specific URL.

The code skeleton could be something similar to the following:

/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items = array();

  $items['frontpage'] = array(
    'page callback' => 'mymodule_frontpage_view',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function mymodule_frontpage_view() {
  // These variables can be useful to redirect the users
  // to specific pages, basing on the language currently set for
  // the content, or on the fact the user is logged in.
  $langcode = $GLOBALS['language_content']->language;
  $logged_in = user_is_logged_in();

  if ($condition) {
    drupal_goto($redirect);
  }
}

A very flexible solution would be to use Panels. Different displays can be presented based on any criteria, and is controlled through a UI.

Tags:

Nodes

7