You are here

front_page.module in Front Page 7.2

File

front_page.module
View source
<?php

/**
 *
 * This module allows the site admin to set advanced front page settings.
 *
 * This version is for Drupal 7. Earlier versions can be found at http://drupal.org/project/front.
 *
 * This module version was developed by timhilliard and various members of the drupal community.
 *
 * If you have any ideas/patches or requests, please post them at http://drupal.org/project/issues/front.
 *
 */

/**
 * Implements hook_help().
 */
function front_page_help($section) {
  switch ($section) {
    case 'admin/config/front/settings':
      return t('<p>Setup custom front pages for your site.</p>');
    case 'admin/config/front/arrange':
      return t('<p>Arrange the order in which roles will be checked for custom front page settings. Roles will be processed from top to bottom. To enable other roles you must first enable them in the !link.</p>', array(
        '!link' => l(t('Settings tab'), 'admin/config/front/settings'),
      ));
    case 'admin/config/front/home-links':
      return t('<p>If a HOME link is set, the &lt;front&gt; placeholder will be replaced with this value instead of the standard front page.</p>');
  }
}

/**
 * Implements hook_menu().
 */
function front_page_menu() {

  // Front page settings.
  $items['admin/config/front'] = array(
    'title' => 'Front Page',
    'description' => 'Configure front page.',
    'position' => 'right',
    'weight' => -15,
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'administer front page',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/config/front/settings'] = array(
    'title' => 'Settings',
    'description' => 'Administer custom front page settings.',
    'weight' => 0,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'front_page_admin',
    ),
    'access arguments' => array(
      'administer front page',
    ),
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
    'file' => 'front_page.admin.inc',
  );
  $items['admin/config/front/arrange'] = array(
    'title' => 'Arrange',
    'description' => 'Ability to re-arrange what order front page roles are processed.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'front_page_admin_arrange_form',
    ),
    'access arguments' => array(
      'administer front page',
    ),
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
    'file' => 'front_page.admin.inc',
    'weight' => 1,
  );
  $items['admin/config/front/home-links'] = array(
    'title' => 'Home links',
    'description' => 'Allows you to change the location of the &lt;front&gt; placeholder.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'front_page_admin_home_links',
    ),
    'access arguments' => array(
      'administer front page',
    ),
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
    'file' => 'front_page.admin.inc',
    'weight' => 2,
  );
  $items['front_page'] = array(
    'title' => '',
    'page callback' => 'front_page',
    'access callback' => TRUE,
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function front_page_permission() {
  return array(
    'administer front page' => array(
      'title' => t('Administer front page'),
      'description' => t('Allow users to make changes to the front page settings.'),
    ),
  );
}

/**
 * Implements hook_init().
 */
function front_page_init() {

  // Make sure front page module is not run when using cli (drush).
  // Make sur front page module does not run when installing Drupal either.
  if (drupal_is_cli() || drupal_installation_attempted()) {
    return;
  }

  // Don't run when site is in maintenance mode
  if (variable_get('maintenance_mode', 0)) {
    return;
  }

  // Ignore non index.php requests (like cron)
  if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
    return;
  }
  global $_front_page, $conf;

  // let administrator know that there is a config error.
  if (variable_get('site_frontpage', '') == 'front_page' && user_access('administer menu')) {
    drupal_set_message(t('There is a configuration error. The home page should not be set to the path "front_page". Please change this !link', array(
      '!link' => l(t('here'), 'admin/config/system/site-information'),
    )), 'error');
  }
  if (variable_get('front_page_enable', 0) && drupal_is_front_page()) {
    $_front_page = front_page_get_by_role();
  }
  if (user_access('administer menu') && preg_match('@^front_page/preview/([0-9]+)$@', $_GET['q'], $match)) {
    $_front_page = front_page_get_by_rid($match[1]);
  }
  if ($_front_page) {

    // Let other hooks modify
    drupal_alter('front_page', $_front_page);
    switch ($_front_page['mode']) {
      case 'themed':
      case 'full':
        $_GET['q'] = 'front_page';

        // need to set variable site_frontpage to current path so that it thinks it is the front page.
        $conf['site_frontpage'] = $_GET['q'];
        break;
      case 'redirect':
        $url = front_page_parse_url($_front_page['data']);
        drupal_goto($url['path'], $url['options']);
        break;
      case 'alias':
        $url = front_page_parse_url($_front_page['data']);
        $_GET['q'] = drupal_get_normal_path($url['path']);

        // need to set variable site_frontpage to current path so that it thinks it is the front page.
        $conf['site_frontpage'] = $_GET['q'];
        break;
    }

    // turn caching off for this page as it is dependant on role.
    $GLOBALS['conf']['cache'] = FALSE;
  }
}

/**
 * Function to handle the display of the front page themed and full types.
 */
function front_page() {

  // $_front_page variable should already have been loaded in front_page_init() function.
  global $_front_page;
  if ($_front_page) {
    switch ($_front_page['mode']) {
      case 'themed':
        return check_markup($_front_page['data'], $_front_page['filter_format']);
      case 'full':
        print check_markup($_front_page['data'], $_front_page['filter_format']);
        exit;
    }
  }

  // set page not found as there was no themed or full option set for the front page.
  drupal_not_found();
  exit;
}

/**
 * Function to parse a full URL including GET variables and fragment
 * to an array ready for drupal_goto(), url(), or l() functions.
 */
function front_page_parse_url($path) {
  $url['path'] = $path;
  $url['options'] = array();
  if (preg_match('@^(?P<path>[^?#]+)(\\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$@', $path, $match)) {
    $url['path'] = $match['path'];
    if (!empty($match['query'])) {
      foreach (explode('&', $match['query']) as $query_part) {
        list($key, $value) = explode('=', $query_part);
        $url['options']['query'][$key] = $value;
      }
    }
    if (!empty($match['fragment'])) {
      $url['options']['fragment'] = $match['fragment'];
    }
  }

  // if the token module exists then replace any tokens in the path
  if (module_exists('token')) {
    $url['path'] = token_replace($url['path']);
  }
  return $url;
}

/**
 * Function to return the first role enabled in front page, ordered by weight.
 */
function front_page_get_by_role($index = 0, $number = 1) {
  global $user;
  $roles = array();
  foreach (array_keys($user->roles) as $rid) {
    $roles[] = front_page_get_by_rid($rid);
  }
  $roles = array_filter($roles);
  if (empty($roles)) {
    return FALSE;
  }
  uasort($roles, '_front_page_sort');
  return reset($roles);
}

/**
 * Function to return the first role enabled in front page, ordered by weight.
 */
function front_page_get_by_rid($rid) {
  $role = _front_page_get_by_rid_with_default($rid);
  return !empty($role['mode']) ? $role : FALSE;
}

/**
 * Function to return all the roles in front page, ordered by weight.
 */
function front_page_get_all() {
  $roles = array();
  foreach (user_roles() as $rid => $role) {
    $roles[$rid] = _front_page_get_by_rid_with_default($rid);
  }
  uasort($roles, '_front_page_sort');
  return $roles;
}

/**
 * Implements hook_page_url_outbound_alter().
 *
 * This alters anything parsed through the url() function to replace the <front> string
 *  with an alternative string instead of the site_frontpage setting.
 */
function front_page_url_outbound_alter(&$path, &$options, &$original_path) {

  // check if path is set to 'front_page' as this is our virtual home
  // page and doesn't work properly unless used from home page.
  if ($path == 'front_page') {
    $original_path = $path = '';
  }
  $newpath = variable_get('front_page_home_link_path', '');
  if (($path == '<front>' || empty($path)) && !empty($newpath)) {
    $original_path = $path = $newpath;
  }
}

/**
 * Implements hook_theme().
 */
function front_page_theme() {
  return array(
    'front_page_admin_arrange_form' => array(
      'file' => 'front_page.admin.inc',
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_user_role_delete().
 */
function front_page_user_role_delete($role) {

  // Delete Front configuration for the role being deleted.
  variable_del("front_page_role_{$role->rid}");
}

/**
 * Function to return the first role enabled in front page, with defaults.
 */
function _front_page_get_by_rid_with_default($rid) {
  return variable_get("front_page_role_{$rid}", array(
    'rid' => $rid,
    'mode' => '',
    'data' => '',
    'filter_format' => '',
    'weight' => 0,
  ));
}

/**
 * Sort by weight ASC and rid DESC.
 */
function _front_page_sort($a, $b) {
  $a_value = is_array($a) && isset($a['weight']) ? $a['weight'] : 0;
  $b_value = is_array($b) && isset($b['weight']) ? $b['weight'] : 0;
  if ($a_value != $b_value) {
    return $a_value < $b_value ? -1 : 1;
  }
  $a_value = is_array($a) && isset($a['rid']) ? $a['rid'] : 0;
  $b_value = is_array($b) && isset($b['rid']) ? $b['rid'] : 0;
  if ($a_value != $b_value) {
    return $a_value > $b_value ? -1 : 1;
  }
  return 0;
}

Functions

Namesort descending Description
front_page Function to handle the display of the front page themed and full types.
front_page_get_all Function to return all the roles in front page, ordered by weight.
front_page_get_by_rid Function to return the first role enabled in front page, ordered by weight.
front_page_get_by_role Function to return the first role enabled in front page, ordered by weight.
front_page_help Implements hook_help().
front_page_init Implements hook_init().
front_page_menu Implements hook_menu().
front_page_parse_url Function to parse a full URL including GET variables and fragment to an array ready for drupal_goto(), url(), or l() functions.
front_page_permission Implements hook_permission().
front_page_theme Implements hook_theme().
front_page_url_outbound_alter Implements hook_page_url_outbound_alter().
front_page_user_role_delete Implements hook_user_role_delete().
_front_page_get_by_rid_with_default Function to return the first role enabled in front page, with defaults.
_front_page_sort Sort by weight ASC and rid DESC.