Skip to content
Snippets Groups Projects
Select Git revision
  • 7.x-1.1
  • 7.x-1.x default
  • 7.x-1.0
3 results

path.inc

  • Steven Jones's avatar
    Steven Jones authored
    e5322b16
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    path.inc 8.26 KiB
    <?php
    
    /**
     * @file
     * Functions to handle paths in Drupal, including path aliasing.
     *
     * These functions are not loaded for cached pages, but modules that need
     * to use them in hook_boot() or hook exit() can make them available, by
     * executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);".
     *
     * Pathinc module makes these available as methods on a class.
     */
    
    /**
     * This is our main way of indirection for path.inc.
     *
     * @return PathincInterface
     *   An instance of a class version of path.inc.
     */
    function pathinc_get_singleton() {
      static $singleton;
    
      if (!isset($singleton)) {
        $class_name = variable_get('pathinc_class_name', 'DrupalPathinc');
        if (!class_exists($class_name)) {
          // Require the file that contains our base class.
          require_once dirname(__FILE__) . '/DrupalPathinc.class.inc';
          require_once dirname(__FILE__) . '/Pathinc.interface.inc';
          // Something has gone wrong here, so try to load our default class.
          $class_name = 'DrupalPathinc';
        }
    
        $singleton = new $class_name();
      }
    
      return $singleton;
    }
    
    /**
     * Initialize the $_GET['q'] variable to the proper normal path.
     */
    function drupal_path_initialize() {
      return pathinc_get_singleton()->drupal_path_initialize();
    }
    
    /**
     * Given an alias, return its Drupal system URL if one exists. Given a Drupal
     * system URL return one of its aliases if such a one exists. Otherwise,
     * return FALSE.
     *
     * @param $action
     *   One of the following values:
     *   - wipe: delete the alias cache.
     *   - alias: return an alias for a given Drupal system path (if one exists).
     *   - source: return the Drupal system URL for a path alias (if one exists).
     * @param $path
     *   The path to investigate for corresponding aliases or system URLs.
     * @param $path_language
     *   Optional language code to search the path with. Defaults to the page language.
     *   If there's no path defined for that language it will search paths without
     *   language.
     *
     * @return
     *   Either a Drupal system path, an aliased path, or FALSE if no path was
     *   found.
     */
    function drupal_lookup_path($action, $path = '', $path_language = NULL) {
      return pathinc_get_singleton()->drupal_lookup_path($action, $path, $path_language);
    }
    
    /**
     * Cache system paths for a page.
     *
     * Cache an array of the system paths available on each page. We assume
     * that aliases will be needed for the majority of these paths during
     * subsequent requests, and load them in a single query during
     * drupal_lookup_path().
     */
    function drupal_cache_system_paths() {
      return pathinc_get_singleton()->drupal_cache_system_paths();
    }
    
    /**
     * Given an internal Drupal path, return the alias set by the administrator.
     *
     * If no path is provided, the function will return the alias of the current
     * page.
     *
     * @param $path
     *   An internal Drupal path.
     * @param $path_language
     *   An optional language code to look up the path in.
     *
     * @return
     *   An aliased path if one was found, or the original path if no alias was
     *   found.
     */
    function drupal_get_path_alias($path = NULL, $path_language = NULL) {
      return pathinc_get_singleton()->drupal_get_path_alias($path, $path_language);
    }
    
    /**
     * Given a path alias, return the internal path it represents.
     *
     * @param $path
     *   A Drupal path alias.
     * @param $path_language
     *   An optional language code to look up the path in.
     *
     * @return
     *   The internal path represented by the alias, or the original alias if no
     *   internal path was found.
     */
    function drupal_get_normal_path($path, $path_language = NULL) {
      return pathinc_get_singleton()->drupal_get_normal_path($path, $path_language);
    }
    
    /**
     * Check if the current page is the front page.
     *
     * @return
     *   Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
     */
    function drupal_is_front_page() {
      return pathinc_get_singleton()->drupal_is_front_page();
    }
    
    /**
     * Check if a path matches any pattern in a set of patterns.
     *
     * @param $path
     *   The path to match.
     * @param $patterns
     *   String containing a set of patterns separated by \n, \r or \r\n.
     *
     * @return
     *   Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
     */
    function drupal_match_path($path, $patterns) {
      return pathinc_get_singleton()->drupal_match_path($path, $patterns);
    }
    
    /**
     * Return the current URL path of the page being viewed.
     *
     * Examples:
     * - http://example.com/node/306 returns "node/306".
     * - http://example.com/drupalfolder/node/306 returns "node/306" while
     *   base_path() returns "/drupalfolder/".
     * - http://example.com/path/alias (which is a path alias for node/306) returns
     *   "node/306" as opposed to the path alias.
     *
     * This function is not available in hook_boot() so use $_GET['q'] instead.
     * However, be careful when doing that because in the case of Example #3
     * $_GET['q'] will contain "path/alias". If "node/306" is needed, calling
     * drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL) makes this function available.
     *
     * @return
     *   The current Drupal URL path.
     *
     * @see request_path()
     */
    function current_path() {
      return pathinc_get_singleton()->current_path();
    }
    
    /**
     * Rebuild the path alias white list.
     *
     * @param $source
     *   An optional system path for which an alias is being inserted.
     *
     * @return
     *   An array containing a white list of path aliases.
     */
    function drupal_path_alias_whitelist_rebuild($source = NULL) {
      return pathinc_get_singleton()->drupal_path_alias_whitelist_rebuild($source);
    }
    
    /**
     * Fetches a specific URL alias from the database.
     *
     * @param $conditions
     *   A string representing the source, a number representing the pid, or an
     *   array of query conditions.
     *
     * @return
     *   FALSE if no alias was found or an associative array containing the
     *   following keys:
     *   - source: The internal system path.
     *   - alias: The URL alias.
     *   - pid: Unique path alias identifier.
     *   - language: The language of the alias.
     */
    function path_load($conditions) {
      return pathinc_get_singleton()->path_load($conditions);
    }
    
    /**
     * Save a path alias to the database.
     *
     * @param $path
     *   An associative array containing the following keys:
     *   - source: The internal system path.
     *   - alias: The URL alias.
     *   - pid: (optional) Unique path alias identifier.
     *   - language: (optional) The language of the alias.
     */
    function path_save(&$path) {
      return pathinc_get_singleton()->path_save($path);
    }
    
    /**
     * Delete a URL alias.
     *
     * @param $criteria
     *   A number representing the pid or an array of criteria.
     */
    function path_delete($criteria) {
      return pathinc_get_singleton()->path_delete($criteria);
    }
    
    /**
     * Determines whether a path is in the administrative section of the site.
     *
     * By default, paths are considered to be non-administrative. If a path does
     * not match any of the patterns in path_get_admin_paths(), or if it matches
     * both administrative and non-administrative patterns, it is considered
     * non-administrative.
     *
     * @param $path
     *   A Drupal path.
     *
     * @return
     *   TRUE if the path is administrative, FALSE otherwise.
     *
     * @see path_get_admin_paths()
     * @see hook_admin_paths()
     * @see hook_admin_paths_alter()
     */
    function path_is_admin($path) {
      return pathinc_get_singleton()->path_is_admin($path);
    }
    
    /**
     * Gets a list of administrative and non-administrative paths.
     *
     * @return array
     *   An associative array containing the following keys:
     *   'admin': An array of administrative paths and regular expressions
     *            in a format suitable for drupal_match_path().
     *   'non_admin': An array of non-administrative paths and regular expressions.
     *
     * @see hook_admin_paths()
     * @see hook_admin_paths_alter()
     */
    function path_get_admin_paths() {
      return pathinc_get_singleton()->path_get_admin_paths();
    }
    
    /**
     * Checks a path exists and the current user has access to it.
     *
     * @param $path
     *   The path to check.
     * @param $dynamic_allowed
     *   Whether paths with menu wildcards (like user/%) should be allowed.
     *
     * @return
     *   TRUE if it is a valid path AND the current user has access permission,
     *   FALSE otherwise.
     */
    function drupal_valid_path($path, $dynamic_allowed = FALSE) {
      return pathinc_get_singleton()->drupal_valid_path($path, $dynamic_allowed);
    }
    
    /**
     * Clear the path cache.
     *
     * @param $source
     *   An optional system path for which an alias is being changed.
     */
    function drupal_clear_path_cache($source = NULL) {
      return pathinc_get_singleton()->drupal_clear_path_cache($source);
    }