You are here

function path_redirect_load_by_source in Path redirect 6

Load a redirect by incoming path and language.

Parameters

$source: The incoming path.

$language: An optional language code. If not provided this will examine all language- neutral redirects.

$query: An optional query string to match.

3 calls to path_redirect_load_by_source()
path_redirect_edit_form_validate in ./path_redirect.admin.inc
path_redirect_goto in ./path_redirect.module
path_redirect_js_autocomplete_404 in ./path_redirect.admin.inc
Autocompletion callback for the add/edit redirect form. Returns a list of current 404s on the site.

File

./path_redirect.module, line 381

Code

function path_redirect_load_by_source($source, $language = '', $query = array()) {
  $where = $query ? "(source = '%s' OR source LIKE '%s%%')" : "source = '%s'";
  $args = $query ? array(
    $source,
    $source . '?',
  ) : array(
    $source,
  );
  $args[] = $language;
  $rid_query = db_query("SELECT rid FROM {path_redirect} WHERE {$where} AND language IN ('%s', '') ORDER BY language DESC, source DESC, rid DESC", $args);
  $rids = array();
  while ($rid = db_result($rid_query)) {
    $rids[] = $rid;
  }
  if ($rids && ($redirects = path_redirect_load_multiple($rids))) {

    // Narrow down the list of candidates.
    foreach ($redirects as $rid => $redirect) {
      if (!empty($redirect['source_query'])) {
        if (empty($query) || !path_redirect_compare_array($redirect['source_query'], $query)) {
          unset($redirects[$rid]);
          continue;
        }
      }

      // Add a case sensitive matches condition to be used in sorting.
      if ($source !== $redirect['source']) {
        $redirects[$rid]['weight'] = 1;
      }
    }
    if (!empty($redirects)) {

      // Sort the redirects in the proper order.
      uasort($redirects, '_path_redirect_uasort');

      // Allow other modules to alter the redirect candidates before selecting the top one.
      $context = array(
        'language' => $language,
        'query' => $query,
      );
      drupal_alter('path_redirect_load_by_source', $redirects, $source, $context);
      return !empty($redirects) ? reset($redirects) : FALSE;
    }
  }
  return FALSE;
}