You are here

function url in Drupal 4

Same name and namespace in other branches
  1. 5 includes/common.inc \url()
  2. 6 includes/common.inc \url()
  3. 7 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/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

96 calls to url()
aggregator_help in modules/aggregator.module
Implementation of hook_help().
aggregator_page_rss in modules/aggregator.module
Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
aggregator_page_sources in modules/aggregator.module
Menu callback; displays all the feeds used by the aggregator.
archive_help in modules/archive.module
Implementation of hook_help().
block_admin_display in modules/block.module
Generate main block administration form.

... See full list

6 string references to 'url'
aggregator_form_feed_validate in modules/aggregator.module
Validate aggregator_form_feed form submissions.
profile_form_profile in modules/profile.module
profile_validate_profile in modules/profile.module
profile_view_field in modules/profile.module
statistics_top_referrers in modules/statistics.module
Menu callback; presents the "referrer" page.

... See full list

File

includes/common.inc, line 995
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 (empty($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;
    }
  }
}