22

I want to set the Drupal front page programmatically at run time.
How can I do this? Is this possible?

22

You can use variable_set() for that.

variable_set('site_frontpage', $value);
  • 18
    This is correct, but it's not something that should be done too frequently or based on a use action. A variable_set() call always results in "cache_clear_all('variables', 'cache_bootstrap');" so the next request will need to re-fetch the variables and the bootstrap information from the database. – Berdir Apr 5 '11 at 10:57
  • 1
    @Berdir - this affect somehow the site performance? – Ek Kosmos Apr 5 '11 at 17:48
  • 7
    Yes, it will affect performance, cache_bootstrap contains multiple caches that will only be rebuilt over time, for example hook implementations. Alternative solutions would be using Views, Panels or a custom module to dynamically display content on the front page. For example, you could build a view to show the most recent node of a content type and then point site_frontpage to that. – Berdir Apr 5 '11 at 18:32
  • Using this in a hook_install or hook_update_N then should be fine. – leymannx Sep 30 at 9:12
16

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);
  }
}
9

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

4

I fixed like this:

/**
 * Implements hook_page_alter.
 * @param type $page
 */
function my_module_page_alter(&$page){
  global $user;

  $is_fron_page = drupal_is_front_page();
  if($is_fron_page && $user->uid){
    $redirect = 'users/'.$user->name.'/my-argument';
    drupal_goto($redirect);
  }
}
  • 1
    Hello. Nice of you to share your code. Would be even nicer if you could tell us how exactly your code fixes it. You know, reasoning others can follow if for some reason copy-pasted code will not work for them. – Mołot Jan 30 '14 at 11:57
  • @molot - this method, that should be added to your custom module, implements hook_page_alter, and uses drupal_is_front_page to redirect users, using drupal_goto, based on some condition (in this case $user->uid should not be 0) – commonpike Apr 21 '15 at 15:22
  • @commonpike it's not that I don't understand it. My comment was about answer's quality overall, and specifically to it's usability for new, inexperienced users. – Mołot Apr 21 '15 at 15:25
1

The Rules module can be used also to set the frontpage "programatically".

The actually rule to be created, would be a variation of the answer at Redirect logged in (authenticated) users to a different frontpage using rules or php code.

And optionally combining it with the Flag module, might possibly further enhance the user experience. Eg for logged in users to indicate which of the possible frontpages they consider as their "favorite".

1

If you're looking to programmatically set this globally, you can set the variable in settings.php:

$conf['site_frontpage'] = 'some/custom/path';
0

I've altered the homepage node (on subdomains) by settings the variable in a hook_url_outbound_alter()

variable_set('site_frontpage', $value);

And then redirecting:

$_GET['q'] = 'node/' . $domain_homepage_node_id;

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.