You are here

rename_admin_paths.module in Rename Admin Paths 7

Allows users to rename admin paths

File

rename_admin_paths.module
View source
<?php

// $Id;

/**
 * @file
 * Allows users to rename admin paths
 */

/**
 * Implements hook_help().
 */
function rename_admin_paths_help($path, $arg) {
  switch ($path) {
    case 'admin/help#rename_admin_paths':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Path Admin module allows users to rename admin paths (/admin/..) to /something/... and (/user/..) to /something_else/...') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl><dt>' . t('Rename admin paths') . '</dt>';
      $output .= '<dd>' . t('Choose how to rename admin paths by using <a href="@rename_admin_paths">Path Admin configuration</a>.', array(
        '@rename_admin_paths' => url('admin/config/user-interface/rename-admin-paths'),
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
    case 'admin/config/user-interface/rename-admin-paths':
      $output = '<p>' . t('Configure how to rename admin paths.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_permission().
 */
function rename_admin_paths_permission() {
  return array(
    'administer path admin' => array(
      'title' => t('Administer path admin'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function rename_admin_paths_menu() {
  $items['admin/config/user-interface/rename-admin-paths'] = array(
    'title' => t('Rename Admin Paths'),
    'description' => t('Configure how to rename admin paths.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'rename_admin_paths_settings',
    ),
    'access arguments' => array(
      'administer path admin',
    ),
  );
  return $items;
}

/**
 * Implements hook_outbound_alter().
 */
function rename_admin_paths_url_outbound_alter(&$path, &$options, $original_path) {

  // Admin path
  if (variable_get('rename_admin_path', NULL)) {
    $rename_admin_path_value = variable_get('rename_admin_path_value', 'backend');

    // Replace admin in path
    if (preg_match('|^admin(/{0,1}.*)|', $path, $matches)) {
      $path = urlencode($rename_admin_path_value) . $matches[1];
    }
  }

  // user path
  if (variable_get('rename_user_path', NULL)) {
    $rename_user_path_value = variable_get('rename_user_path_value', 'member');

    // Replace user in path
    if (preg_match('|^user(/{0,1}.*)|', $path, $matches)) {
      $path = urlencode($rename_user_path_value) . $matches[1];
    }
  }
}

/**
 * Implements hook_inbound_alter().
 */
function rename_admin_paths_url_inbound_alter(&$path, $original_path, $path_language) {

  // Admin path
  if (variable_get('rename_admin_path', NULL)) {
    $rename_admin_path_value = variable_get('rename_admin_path_value', 'backend');

    // Avoid 404 after overlay module is enabled
    // This is not a good way, check $_SESSION['overlay_enable_redirect'] every time
    if (!isset($_SESSION['overlay_enable_redirect']) || !$_SESSION['overlay_enable_redirect']) {

      // Get back default admin path
      if (preg_match('|^' . urlencode($rename_admin_path_value) . '(/{0,1}.*)|', $path, $matches)) {
        $path = 'admin' . $matches[1];
      }
    }

    // Get 404 for default admin path
    if (preg_match('|^admin(/{0,1}.*)|', $original_path, $matches)) {
      $path = '404';
    }
  }

  // user path
  if (variable_get('rename_user_path', NULL)) {
    $rename_user_path_value = variable_get('rename_user_path_value', 'member');

    // Get back default user path
    if (preg_match('|^' . urlencode($rename_user_path_value) . '(/{0,1}.*)|', $path, $matches)) {
      $path = 'user' . $matches[1];
    }

    // Get 404 for default user path
    if (preg_match('|^user(/{0,1}.*)|', $original_path, $matches)) {
      $path = '404';
    }
  }
}

/**
 * Implements hook_admin_paths_alter().
 */
function rename_admin_paths_admin_paths_alter(&$paths) {

  // Check if enable
  if (variable_get('rename_admin_path', NULL) || variable_get('rename_admin_path', NULL)) {
    foreach ($paths as $path => $enable) {

      // Add renamed admin paths
      if (variable_get('rename_admin_path', NULL)) {
        $rename_admin_path_value = variable_get('rename_admin_path_value', 'backend');
        $paths[str_replace('admin', urlencode($rename_admin_path_value), $path)] = $enable;
      }

      // Replace "user" in administrative paths
      if (variable_get('rename_user_path', NULL)) {
        $rename_user_path_value = variable_get('rename_user_path_value', 'member');
        $paths[str_replace('user', urlencode($rename_user_path_value), $path)] = $enable;
      }
    }
  }
}

/**
 * Implements hook_overlay_parent_initialize().
 * Override administrative paths defined by overlay module
 */
function rename_admin_paths_overlay_parent_initialize() {

  // Check if enable
  if (variable_get('rename_admin_path', NULL) || variable_get('rename_admin_path', NULL)) {

    // Override overlay paths
    $paths = path_get_admin_paths();
    drupal_add_js(array(
      'overlay' => array(
        'paths' => $paths,
      ),
    ), 'setting');
  }
}

/**
 * Implements hook_menu().
 */
function rename_admin_paths_settings() {
  $form = array();
  $form['rename_admin_path'] = array(
    '#type' => 'fieldset',
    '#title' => t('Rename Admin Path'),
  );
  $form['rename_admin_path']['rename_admin_path'] = array(
    '#type' => 'checkbox',
    '#title' => t('Rename Admin Path'),
    '#default_value' => variable_get('rename_admin_path', NULL),
    '#description' => t('If checked, "admin" will be replaced by the following term in admin path.'),
  );
  $form['rename_admin_path']['rename_admin_path_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Replace "admin" in admin path by'),
    '#default_value' => variable_get('rename_admin_path_value', 'backend'),
    '#description' => t('This value will replace "admin" in admin path.'),
    '#element_validate' => array(
      'rename_admin_paths_form_validate_path_field',
    ),
  );
  $form['rename_user_path'] = array(
    '#type' => 'fieldset',
    '#title' => t('Rename User Path'),
  );
  $form['rename_user_path']['rename_user_path'] = array(
    '#type' => 'checkbox',
    '#title' => t('Rename User Path'),
    '#default_value' => variable_get('rename_user_path', NULL),
    '#description' => t('If checked, "user" will be replaced by the following term in user path.'),
    '#element_validate' => array(
      'rename_admin_paths_form_validate_path_field',
    ),
  );
  $form['rename_user_path']['path_user_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Replace "user" in user path by'),
    '#default_value' => variable_get('rename_user_path_value', 'member'),
    '#description' => t('This value will replace "user" in user path.'),
    '#element_validate' => array(
      'rename_admin_paths_form_validate_path_field',
    ),
  );
  return system_settings_form($form);
}

/**
 * Validation for paths values.
 */
function rename_admin_paths_form_validate_path_field($element, &$form_state) {

  // Force path replacement values to contain only lowercase letters, numbers, and underscores.
  if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
    form_error($element, t('Path replacement value must contain only lowercase letters, numbers, and underscores.'));
  }
}

Functions

Namesort descending Description
rename_admin_paths_admin_paths_alter Implements hook_admin_paths_alter().
rename_admin_paths_form_validate_path_field Validation for paths values.
rename_admin_paths_help Implements hook_help().
rename_admin_paths_menu Implements hook_menu().
rename_admin_paths_overlay_parent_initialize Implements hook_overlay_parent_initialize(). Override administrative paths defined by overlay module
rename_admin_paths_permission Implements hook_permission().
rename_admin_paths_settings Implements hook_menu().
rename_admin_paths_url_inbound_alter Implements hook_inbound_alter().
rename_admin_paths_url_outbound_alter Implements hook_outbound_alter().