Same name and namespace in other branches
  1. 4.6.x includes/common.inc \url()
  2. 4.7.x includes/common.inc \url()
  3. 6.x includes/common.inc \url()
  4. 7.x includes/common.inc \url()

Generate a URL from a Drupal menu path. Will also pass-through existing URLs.

Parameters

$path: The Drupal path being linked to, such as "admin/content/node", or an existing URL like "http://drupal.org/".

$query: A query string to append to the link or URL.

$fragment: A fragment identifier (named anchor) to append to the link. If an existing URL with a fragment identifier is used, it will be replaced. Note, do not include the '#'.

$absolute: Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.

Return value

a string containing a URL to the given path.

When creating links in modules, consider whether l() could be a better alternative than url().

Related topics

69 calls to url()
aggregator_page_category in modules/aggregator/aggregator.module
Menu callback; displays all the items aggregated in a particular category.
aggregator_page_last in modules/aggregator/aggregator.module
Menu callback; displays the most recent items gathered from any feed.
aggregator_page_rss in modules/aggregator/aggregator.module
Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
aggregator_page_sources in modules/aggregator/aggregator.module
Menu callback; displays all the feeds used by the aggregator.
block_admin_display in modules/block/block.module
Generate main block administration form.

... See full list

3 string references to 'url'
aggregator_form_feed_validate in modules/aggregator/aggregator.module
Validate aggregator_form_feed form submissions.
statistics_top_referrers in modules/statistics/statistics.module
Menu callback; presents the "referrer" page.
system_update_162 in modules/system/system.install

File

includes/common.inc, line 1293
Common functions that many Drupal modules will need to reference.

Code

function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) {
  if (isset($fragment)) {
    $fragment = '#' . $fragment;
  }

  // Return an external link if $path contains an allowed absolute URL.
  // Only call the slow filter_xss_bad_protocol if $path contains a ':' before any / ? or #.
  $colonpos = strpos($path, ':');
  if ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path)) {

    // Split off the fragment
    if (strpos($path, '#') !== FALSE) {
      list($path, $old_fragment) = explode('#', $path, 2);
      if (isset($old_fragment) && !isset($fragment)) {
        $fragment = '#' . $old_fragment;
      }
    }

    // Append the query
    if (isset($query)) {
      $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $query;
    }

    // Reassemble
    return $path . $fragment;
  }
  global $base_url;
  static $script;
  static $clean_url;
  if (!isset($script)) {

    // On some web servers, such as IIS, we can't omit "index.php". So, we
    // generate "index.php?q=foo" instead of "?q=foo" on anything that is not
    // Apache.
    $script = strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE ? 'index.php' : '';
  }

  // Cache the clean_url variable to improve performance.
  if (!isset($clean_url)) {
    $clean_url = (bool) variable_get('clean_url', '0');
  }
  $base = $absolute ? $base_url . '/' : base_path();

  // The special path '<front>' links to the default front page.
  if (!empty($path) && $path != '<front>') {
    $path = drupal_get_path_alias($path);
    $path = drupal_urlencode($path);
    if (!$clean_url) {
      if (isset($query)) {
        return $base . $script . '?q=' . $path . '&' . $query . $fragment;
      }
      else {
        return $base . $script . '?q=' . $path . $fragment;
      }
    }
    else {
      if (isset($query)) {
        return $base . $path . '?' . $query . $fragment;
      }
      else {
        return $base . $path . $fragment;
      }
    }
  }
  else {
    if (isset($query)) {
      return $base . $script . '?' . $query . $fragment;
    }
    else {
      return $base . $fragment;
    }
  }
}