You are here

function redirect_load_by_source in Redirect 7

Same name and namespace in other branches
  1. 7.2 redirect.module \redirect_load_by_source()

Load multiple URL redirects from the database by {redirect}.source.

Parameters

$source: The source of the URL redirect.

$language: Language of the source URL.

$query: Array of URL query parameters.

$enabled_only: Boolean that indicates whether to only load enabled redirects.

Return value

The first matched URL redirect object, or FALSE if there aren't any.

See also

redirect_load_multiple()

_redirect_uasort()

redirect_compare_array_recursive()

Related topics

5 calls to redirect_load_by_source()
RedirectUnitTest::testLoadRedirectsBySource in ./redirect.test
Test redirect_load_by_source().
redirect_edit_form_validate in ./redirect.admin.inc
Form validate handler; validate an URL redirect.
redirect_get_current_redirect in ./redirect.module
redirect_path_delete in ./redirect.module
Implements hook_path_delete().
redirect_url_inbound_alter in ./redirect.module
Implements hook_url_inbound_alter().

File

./redirect.module, line 630

Code

function redirect_load_by_source($source, $language = LANGUAGE_NONE, array $query = array(), $enabled_only = TRUE) {
  $rids = redirect_fetch_rids_by_path($source, $language, $enabled_only);
  if ($rids && ($redirects = redirect_load_multiple($rids))) {

    // Narrow down the list of candidates.
    foreach ($redirects as $rid => $redirect) {
      if (!empty($redirect->source_options['query'])) {
        if (empty($query) || !redirect_compare_array_recursive($redirect->source_options['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, '_redirect_uasort');

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