1 redirect.module redirect_load_by_source($source, $langcode = LANGUAGE_NONE, array $query = array())

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

Parameters

$source: The source of the URL redirect.

$langcode: Language code of the source URL.

$query: Array of URL query parameters.

Return value

Redirect|FALSE: 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

File

core/modules/redirect/redirect.module, line 462

Code

function redirect_load_by_source($source, $langcode = LANGUAGE_NONE, array $query = array()) {
  // Run a case-insensitive query for matching RIDs first.
  $rid_query = db_select('redirect');
  $rid_query->addField('redirect', 'rid');
  $system_config = config('system.core');
  if ($source != $system_config->get('site_frontpage')) {
    $rid_query->condition('source', db_like($source), 'LIKE');
  }
  else {
    $source_condition = db_or();
    $source_condition->condition('source', db_like($source), 'LIKE');
    $source_condition->condition('source', '');
    $rid_query->condition($source_condition);
  }
  $rid_query->condition('langcode', array($langcode, LANGUAGE_NONE));
  $rids = $rid_query->execute()->fetchCol();

  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('langcode' => $langcode, 'query' => $query);
      backdrop_alter('redirect_load_by_source', $redirects, $source, $context);

      return !empty($redirects) ? reset($redirects) : FALSE;
    }
  }

  return FALSE;
}