You are here

function domain_path_lookup_path in Domain Path 7

Heavily modeled after drupal_lookup_path().

Given an domain specific alias, return its Drupal system URL if one exists. Given a Drupal system URL return one of its domain specific aliases if such a one exists. Otherwise, return FALSE.

Parameters

$action: One of the following string 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).

$path: The path string to investigate for corresponding domain aliases or system URLs.

$domain_id: The id of the domain to load the associated path for.

$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 value

Either a Drupal system path, an aliased path, or FALSE if no path was found.

3 calls to domain_path_lookup_path()
domain_path_domain_path in ./domain_path.module
Implements hook_domainpath().
domain_path_form_node_form_alter in ./domain_path.module
Implements hook_form_NODE_FORM_alter().
domain_path_url_inbound_alter in ./domain_path.module
Implements hook_url_inbound_alter().
2 string references to 'domain_path_lookup_path'
domain_path_path_save in ./domain_path.module
Save a domain path alias to the database.
domain_path_save_paths in ./domain_path.module
A helper function to save multiple domain paths at once.

File

./domain_path.module, line 124
Path alias handling for multiple domains.

Code

function domain_path_lookup_path($action, $path = '', $domain_id = NULL, $path_language = NULL) {
  global $language_url;
  global $_domain;

  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['cache'] =& drupal_static(__FUNCTION__);
  }
  $cache =& $drupal_static_fast['cache'];
  if ($action == 'wipe' || !isset($cache)) {
    $cache = array(
      'map' => array(),
      'no_aliases' => array(),
      'no_sources' => array(),
      'domain_id' => $_domain['domain_id'],
    );
  }

  // Check and load domain_id and cache if necessary.
  if ($domain_id == NULL || $domain_id == $cache['domain_id']) {

    // Null or the same don't do anything.
    $domain_id = $cache['domain_id'];
  }
  elseif ($domain = domain_load($domain_id)) {

    // A differnt domain is wanted.
    $cache['domain_id'] = $domain_id = $domain['domain_id'];
  }
  else {

    // Bad id? Oh well load current domain again just to make sure.
    $cache['domain_id'] = $domain_id = $_domain['domain_id'];
  }

  // If no language is explicitly specified we default to the current URL
  // language. If we used a language different from the one conveyed by the
  // requested URL, we might end up being unable to check if there is a path
  // alias matching the URL path.
  $path_language = $path_language ? $path_language : $language_url->language;

  // If the alias has already been loaded, return it.
  if (isset($cache['map'][$domain_id][$path_language][$path])) {
    return $cache['map'][$domain_id][$path_language][$path];
  }

  // Lookup Drupal system path.
  if ($action == 'source') {

    // Check $cache['no_sources'] for this $path in case we've already determined that there
    // isn't a path that has this alias.
    if (!isset($cache['no_sources'][$domain_id][$path_language][$path])) {
      $args = array(
        ':alias' => $path,
        ':domain_id' => $domain_id,
        ':language' => $path_language,
        ':language_none' => LANGUAGE_NONE,
      );

      // Always get the language-specific alias before the language-neutral
      // one. For example 'de' is less than 'und' so the order needs to be
      // ASC, while 'xx-lolspeak' is more than 'und' so the order needs to
      // be DESC. We also order by pid ASC so that fetchAllKeyed() returns
      // the most recently created alias for each source. Subsequent queries
      // using fetchField() must use pid DESC to have the same effect.
      // For performance reasons, the query builder is not used here.
      if ($path_language == LANGUAGE_NONE) {
        unset($args[':language']);
        $source = db_query("SELECT source FROM {domain_path} WHERE alias = :alias AND domain_id = :domain_id AND language = :language_none ORDER BY dpid DESC", $args)
          ->fetchField();
      }
      elseif ($path_language > LANGUAGE_NONE) {
        $source = db_query("SELECT source FROM {domain_path} WHERE alias = :alias AND domain_id = :domain_id AND language IN (:language, :language_none) ORDER BY language DESC, dpid DESC", $args)
          ->fetchField();
      }
      else {
        $source = db_query("SELECT source FROM {domain_path} WHERE alias = :alias AND domain_id = :domain_id AND language IN (:language, :language_none) ORDER BY language ASC, dpid DESC", $args)
          ->fetchField();
      }
      if ($source) {

        // Source was found, store and return it.
        $cache['map'][$domain_id][$path_language][$path] = $source;
      }
      else {

        // No source found store in no_sources for future lookups.
        $cache['no_sources'][$domain_id][$path_language][$path] = TRUE;
      }
      return $source;
    }
  }
  elseif ($action == 'alias') {

    // Check $cache['no_aliases'] for this $path in case we've already determined that there
    // isn't an alias for this path.
    if (!isset($cache['no_aliases'][$domain_id][$path_language][$path])) {
      $args = array(
        ':source' => $path,
        ':domain_id' => $domain_id,
        ':language' => $path_language,
        ':language_none' => LANGUAGE_NONE,
      );

      // See the queries above.
      if ($path_language == LANGUAGE_NONE) {
        unset($args[':language']);
        $alias = db_query("SELECT alias FROM {domain_path} WHERE source = :source AND domain_id = :domain_id AND language = :language_none ORDER BY dpid DESC", $args)
          ->fetchField();
      }
      elseif ($path_language > LANGUAGE_NONE) {
        $alias = db_query("SELECT alias FROM {domain_path} WHERE source = :source AND domain_id = :domain_id AND language IN (:language, :language_none) ORDER BY language DESC, dpid DESC", $args)
          ->fetchField();
      }
      else {
        $alias = db_query("SELECT alias FROM {domain_path} WHERE source = :source AND domain_id = :domain_id AND language IN (:language, :language_none) ORDER BY language ASC, dpid DESC", $args)
          ->fetchField();
      }

      // Alias was found, store and return it.
      if ($alias) {
        $cache['map'][$domain_id][$path_language][$path] = $alias;
      }
      else {
        $cache['no_aliases'][$domain_id][$path_language][$path] = TRUE;
      }
      return $alias;
    }
  }

  // Otherwise always FALSE.
  return FALSE;
}