url

Versions
4.6 – 5
url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE)
6
url($path = NULL, $options = array())
7
url($path = NULL, array $options = array())

Generate a URL.

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

Parameters

$path The Drupal path being linked to, such as "admin/content", or an existing URL like "http://drupal.org/". The special path '<front>' may also be given and will generate the site's base URL.

$options An associative array of additional options, with the following keys:

  • 'query': An array of query key/value-pairs (without any URL-encoding) to append to the link.
  • 'fragment': A fragment identifier (or named anchor) to append to the link. Do not include the leading '#' character.
  • 'absolute': Defaults to FALSE. 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 a RSS feed.
  • 'alias': Defaults to FALSE. Whether the given path is a URL alias already.
  • 'external': Whether the given path is an external URL.
  • 'language': An optional language object. Used to build the URL to link to and look up the proper alias for the link.
  • 'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on http or https respectively. TRUE enforces HTTPS and FALSE enforces HTTP, but HTTPS can only be enforced when the variable 'https' is set to TRUE.
  • 'base_url': Only used internally, to modify the base URL when a language dependent URL requires so.
  • 'prefix': Only used internally, to modify the path when a language dependent URL requires so.

Return value

A string containing a URL to the given path.

▾ 171 functions call url()

aggregator_block_view in modules/aggregator/aggregator.module
Implement hook_block_view().
aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implement hook_form_aggregator_admin_form_alter().
aggregator_form_feed in modules/aggregator/aggregator.admin.inc
Form builder; Generate a form to add/edit feed sources.
aggregator_form_opml in modules/aggregator/aggregator.admin.inc
Form builder; Generate a form to import feeds from OPML.
aggregator_help in modules/aggregator/aggregator.module
Implement hook_help().
aggregator_page_categories in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the categories used by the aggregator.
aggregator_page_category in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the items aggregated in a particular category.
aggregator_page_last in modules/aggregator/aggregator.pages.inc
Menu callback; displays the most recent items gathered from any feed.
aggregator_page_sources in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the feeds used by the aggregator.
aggregator_view in modules/aggregator/aggregator.admin.inc
Displays the aggregator administration page.
ajax_process_form in includes/ajax.inc
Add AJAX information about a form element to the page to communicate with JavaScript.
batch_process in includes/form.inc
Processes the batch.
block_admin_display_form in modules/block/block.admin.inc
Generate main blocks administration form.
block_custom_block_form in modules/block/block.module
Define the custom block form.
block_help in modules/block/block.module
Implement hook_help().
blog_block_view in modules/blog/blog.module
Implement hook_block_view().
blog_feed_last in modules/blog/blog.pages.inc
Menu callback; displays an RSS feed containing recent blog entries of all users.
blog_feed_user in modules/blog/blog.pages.inc
Menu callback; displays an RSS feed containing recent blog entries of a given user.
blog_help in modules/blog/blog.module
Implement hook_help().
blog_page_last in modules/blog/blog.pages.inc
Menu callback; displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog/blog.pages.inc
Menu callback; displays a Drupal page containing recent blog entries of a given user.
book_help in modules/book/book.module
Implement hook_help().
color_help in modules/color/color.module
Implement hook_help().
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
comment_help in modules/comment/comment.module
Implement hook_help().
comment_node_view in modules/comment/comment.module
Implement hook_node_view().
comment_tokens in modules/comment/comment.tokens.inc
Implement hook_tokens().
contact_form_user_profile_form_alter in modules/contact/contact.module
Implement hook_form_FORM_ID_alter().
contact_help in modules/contact/contact.module
Implement hook_help().
contact_mail in modules/contact/contact.module
Implement hook_mail().
contact_site_form in modules/contact/contact.pages.inc
Form builder; the site-wide contact form.
dashboard_admin in modules/dashboard/dashboard.module
Dashboard page callback.
dblog_form_system_logging_settings_alter in modules/dblog/dblog.module
Implement hook_form_FORM_ID_alter().
drupal_goto in includes/common.inc
Send the user to a different Drupal page.
field_ui_display_overview_form in modules/field_ui/field_ui.admin.inc
Menu callback; presents a listing of fields display settings for a content type.
field_ui_help in modules/field_ui/field_ui.module
Implement hook_help().
filter_help in modules/filter/filter.module
Implement hook_help().
filter_update_7005 in modules/filter/filter.install
Integrate text formats with the user permissions system.
forum_block_view_pre_render in modules/forum/forum.module
A #pre_render callback. Lists nodes based on the element's #query property. *
forum_confirm_delete in modules/forum/forum.admin.inc
Returns a confirmation page for deleting a forum taxonomy term.
forum_help in modules/forum/forum.module
Implement hook_help().
forum_overview in modules/forum/forum.admin.inc
Returns an overview list of existing forums and containers
help_help in modules/help/help.module
Implement hook_help().
hook_help in modules/help/help.api.php
Provide online user help.
hook_mail in modules/system/system.api.php
Prepare a message based on parameters; called from drupal_mail().
hook_requirements in modules/system/system.api.php
Check installation requirements and do status reporting.
hook_search_execute in modules/search/search.api.php
Execute a search for a set of key words.
hook_user_login in modules/user/user.api.php
The user just logged in.
image_help in modules/image/image.module
Implement of hook_help().
image_style_url in modules/image/image.module
Return the URL for an image derivative given a style and image path.
install_finished in ./install.php
Installation task; perform final steps and display a 'finished' page.
l in includes/common.inc
Format an internal Drupal link.
locale_form_node_type_form_alter in modules/locale/locale.module
Implement hook_form_FORM_ID_alter().
locale_help in modules/locale/locale.module
Implement hook_help().
locale_languages_predefined_form_submit in includes/locale.inc
Process the language addition form submission.
menu_get_active_help in includes/menu.inc
Returns the help associated with the active menu item.
menu_help in modules/menu/menu.module
Implement hook_help().
menu_overview_form in modules/menu/menu.admin.inc
Form for editing an entire menu tree at once.
node_block_view in modules/node/node.module
Implement hook_block_view().
node_feed in modules/node/node.module
A generic function for generating RSS feeds from a set of nodes.
node_help in modules/node/node.module
Implement hook_help().
node_overview_types in modules/node/content_types.inc
Displays the content type admin overview page.
node_page_default in modules/node/node.module
Menu callback; Generate a listing of promoted nodes.
node_page_view in modules/node/node.module
Menu callback; view a single node.
node_search_execute in modules/node/node.module
Implement hook_search_execute().
node_tokens in modules/node/node.tokens.inc
Implement hook_tokens().
openid_authentication in modules/openid/openid.module
Authenticate a user or attempt registration.
openid_authentication_request in modules/openid/openid.module
openid_login_validate in modules/openid/openid.module
Login form _validate hook
openid_test_html_openid1 in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with OpenID 1.0 <link> element.
openid_test_html_openid2 in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with OpenID 2.0 <link> element.
openid_test_yadis_http_equiv in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with <meta> element.
openid_test_yadis_xrds in modules/openid/tests/openid_test.module
Menu callback; XRDS document that references the OP Endpoint URL.
openid_test_yadis_x_xrds_location in modules/openid/tests/openid_test.module
Menu callback; regular HTML page with an X-XRDS-Location HTTP header.
openid_user_add_submit in modules/openid/openid.pages.inc
path_admin_form in modules/path/path.admin.inc
Return a form for editing or creating an individual URL alias.
path_admin_overview in modules/path/path.admin.inc
Return a listing of all defined URL aliases.
php_help in modules/php/php.module
Implement hook_help().
poll_help in modules/poll/poll.module
Implement hook_help().
profile_block_configure in modules/profile/profile.module
Implement hook_block_configure().
profile_field_delete in modules/profile/profile.admin.inc
Menu callback; deletes a field from all user profiles.
rdf_preprocess_comment in modules/rdf/rdf.module
Implements MODULE_preprocess_HOOK().
rdf_preprocess_username in modules/rdf/rdf.module
Implements MODULE_preprocess_HOOK().
rdf_preprocess_user_profile in modules/rdf/rdf.module
Implements MODULE_preprocess_HOOK().
search_admin_settings in modules/search/search.admin.inc
Menu callback; displays the search module settings page.
search_form in modules/search/search.module
Render a search form.
search_help in modules/search/search.module
Implement hook_help().
shortcut_set_switch_submit in modules/shortcut/shortcut.admin.inc
Submit handler for the form that switches shortcut sets.
simpletest_help in modules/simpletest/simpletest.module
Implement hook_help().
simpletest_requirements in modules/simpletest/simpletest.install
Check that the cURL extension exists for PHP.
simpletest_result_form in modules/simpletest/simpletest.pages.inc
Test results form for $test_id.
statistics_access_log in modules/statistics/statistics.admin.inc
Menu callback; Displays recent page accesses.
statistics_help in modules/statistics/statistics.module
Implement hook_help().
statistics_settings_form in modules/statistics/statistics.admin.inc
Form builder; Configure access logging.
syslog_help in modules/syslog/syslog.module
Implement hook_help().
system_admin_config_page in modules/system/system.admin.inc
Menu callback; Provide the administration overview page.
system_authorized_get_url in modules/system/system.module
Return the URL for the authorize.php script.
system_check_http_request in modules/system/system.module
Checks whether the server is capable of issuing HTTP requests.
system_configure_date_formats_form in modules/system/system.admin.inc
Allow users to add additional date formats.
system_date_time_formats in modules/system/system.admin.inc
Displays the date format strings overview page.
system_date_time_settings in modules/system/system.admin.inc
Form builder; Configure the site date and time settings.
system_help in modules/system/system.module
Implement hook_help().
system_image_toolkit_settings in modules/system/system.admin.inc
Form builder; Configure site image toolkit usage.
system_main_admin_page in modules/system/system.admin.inc
Menu callback; Provide the administration overview page.
system_modules in modules/system/system.admin.inc
Menu callback; provides module enable/disable interface.
system_modules_uninstall in modules/system/system.admin.inc
Builds a form of currently disabled modules.
system_page_build in modules/system/system.module
Implement hook_page_build().
system_performance_settings in modules/system/system.admin.inc
Form builder; Configure site performance settings.
system_requirements in modules/system/system.install
Test and report Drupal installation requirements.
system_site_information_settings in modules/system/system.admin.inc
Form builder; The general site information form.
system_site_maintenance_mode in modules/system/system.admin.inc
Form builder; Configure the site's maintenance status.
system_test_redirect in modules/simpletest/tests/system_test.module
system_tokens in modules/system/system.tokens.inc
Implement hook_tokens().
system_update_7021 in modules/system/system.install
Add new blocks to new regions, migrate custom variables to blocks.
system_user_login in modules/system/system.module
Implement hook_user_login().
taxonomy_overview_terms in modules/taxonomy/taxonomy.admin.inc
Form builder for the taxonomy terms overview.
taxonomy_term_feed in modules/taxonomy/taxonomy.pages.inc
Generate the content feed for a taxonomy term.
taxonomy_term_page in modules/taxonomy/taxonomy.pages.inc
Menu callback; displays all nodes associated with a term.
taxonomy_tokens in modules/taxonomy/taxonomy.tokens.inc
Implement hook_tokens().
template_preprocess_aggregator_feed_source in modules/aggregator/aggregator.pages.inc
Process variables for aggregator-feed-source.tpl.php.
template_preprocess_aggregator_item in modules/aggregator/aggregator.pages.inc
Process variables for aggregator-item.tpl.php.
template_preprocess_book_navigation in modules/book/book.module
Process variables for book-navigation.tpl.php.
template_preprocess_forums in modules/forum/forum.module
Process variables for forums.tpl.php
template_preprocess_forum_list in modules/forum/forum.module
Process variables to format a forum listing.
template_preprocess_forum_topic_list in modules/forum/forum.module
Preprocess variables to format the topic listing.
template_preprocess_maintenance_page in includes/theme.inc
The variables generated here is a mirror of template_preprocess_page(). This preprocessor will run it's course when theme_maintenance_page() is invoked. It is also used in theme_install_page() and theme_update_page() to keep all the variables...
template_preprocess_node in modules/node/node.module
Process variables for node.tpl.php
template_preprocess_page in includes/theme.inc
Preprocess variables for page.tpl.php
theme_aggregator_page_rss in modules/aggregator/aggregator.pages.inc
Theme the RSS output.
theme_comment_post_forbidden in modules/comment/comment.module
Theme a "you can't post comments" notice.
theme_dashboard_disabled_blocks in modules/dashboard/dashboard.module
Theme a set of disabled blocks, for display in dashboard customization mode.
theme_image_style_list in modules/image/image.admin.inc
Display the page containing the list of image styles.
theme_image_style_preview in modules/image/image.admin.inc
Theme callback for displaying a preview of an image style.
theme_link in includes/theme.inc
Return a themed link.
theme_node_add_list in modules/node/node.pages.inc
Display the list of available node types for node creation.
theme_taxonomy_overview_vocabularies in modules/taxonomy/taxonomy.admin.inc
Theme the vocabulary overview as a sortable list of vocabularies.
theme_textfield in includes/form.inc
Theme a textfield form element.
theme_toolbar_toggle in modules/toolbar/toolbar.module
Formats an element used to toggle the toolbar drawer's visibility.
translation_form_node_type_form_alter in modules/translation/translation.module
Implement hook_form_FORM_ID_alter().
translation_help in modules/translation/translation.module
Implement hook_help().
trigger_assign_form in modules/trigger/trigger.admin.inc
Returns the form for assigning an action to a trigger.
trigger_example_help in developer/examples/trigger_example.module
Implementation of hook_help().
trigger_help in modules/trigger/trigger.module
Implement hook_help().
update_help in modules/update/update.module
Implement hook_help().
update_mail in modules/update/update.module
Implement hook_mail().
update_manager_confirm_update_form in modules/update/update.manager.inc
Build the form to confirm that an update should proceed (after downloading).
update_settings in modules/update/update.settings.inc
Form builder for the update settings tab.
upload_admin_settings in modules/upload/upload.admin.inc
Menu callback for the upload settings form.
upload_form_alter in modules/upload/upload.module
upload_help in modules/upload/upload.module
Implement hook_help().
user_account_form_validate in modules/user/user.module
Form validation handler for user_account_form().
user_admin_settings in modules/user/user.admin.inc
Form builder; Configure user settings for this site.
user_cancel_url in modules/user/user.module
Generate a URL to confirm an account cancellation request.
user_help in modules/user/user.module
Implement hook_help().
user_login_block in modules/user/user.module
user_login_final_validate in modules/user/user.module
The final validation handler on the login form.
user_pass_reset in modules/user/user.pages.inc
Menu callback; process one time login link and redirects to the user page on success.
user_pass_reset_url in modules/user/user.module
user_permission in modules/user/user.module
Implement hook_permission().
user_register_submit in modules/user/user.module
Submit handler for the user registration form.
user_search_execute in modules/user/user.module
Implement hook_search_execute().
user_tokens in modules/user/user.tokens.inc
Implement hook_tokens().
_batch_progress_page_js in includes/batch.inc
Output a batch processing page with JavaScript support.
_batch_progress_page_nojs in includes/batch.inc
Output a batch processing page without JavaScript support.
_menu_site_is_offline in includes/menu.inc
Checks whether the site is in maintenance mode.
_openid_test_endpoint_authenticate in modules/openid/tests/openid_test.module
OpenID endpoint; handle "authenticate" requests.
_openid_user_login_form_alter in modules/openid/openid.module
_php_filter_tips in modules/php/php.module
Tips callback for php filter.
_shortcut_link_form_elements in modules/shortcut/shortcut.admin.inc
Helper function for building a form for adding or editing shortcut links.
_update_message_text in modules/update/update.module
Helper function to return the appropriate message text when the site is out of date or missing a security update.
_update_no_data in modules/update/update.module
Prints a warning message when there is no data about available updates.

Code

includes/common.inc, line 2453

<?php
function url($path = NULL, array $options = array()) {
  static $url_outbound;

  // Merge in defaults.
  $options += array(
    'fragment' => '',
    'query' => array(),
    'absolute' => FALSE,
    'alias' => FALSE,
    'prefix' => ''
  );

  if (!isset($options['external'])) {
    // 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 #.
    // Note: we could use url_is_external($path) here, but that would
    // requre another function call, and performance inside url() is critical.
    $colonpos = strpos($path, ':');
    $options['external'] = ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path));
  }

  // Preserve the original path before altering or aliasing.
  $original_path = $path;

  // Allow other modules to alter the outbound URL and options.
  // Since PHP code cannot be unloaded, we statically cache the implementations
  // of hook_url_outbound_alter() and only invoke them in case there are any.
  if (!isset($url_outbound)) {
    $url_outbound = (bool) module_implements('url_outbound_alter');
  }
  if ($url_outbound) {
    drupal_alter('url_outbound', $path, $options, $original_path);
  }

  if ($options['fragment']) {
    $options['fragment'] = '#' . $options['fragment'];
  }

  if ($options['external']) {
    // Split off the fragment.
    if (strpos($path, '#') !== FALSE) {
      list($path, $old_fragment) = explode('#', $path, 2);
      // If $options contains no fragment, take it over from the path.
      if (isset($old_fragment) && !$options['fragment']) {
        $options['fragment'] = '#' . $old_fragment;
      }
    }
    // Append the query.
    if ($options['query']) {
      $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options['query']);
    }
    if (isset($options['https']) && variable_get('https', FALSE)) {
      if ($options['https'] === TRUE) {
        $path = str_replace('http://', 'https://', $path);
      }
      elseif ($options['https'] === FALSE) {
        $path = str_replace('https://', 'http://', $path);
      }
    }
    // Reassemble.
    return $path . $options['fragment'];
  }

  global $base_url, $base_secure_url, $base_insecure_url;
  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static = array();
  isset($drupal_static[__FUNCTION__]) || ($drupal_static[__FUNCTION__] = &drupal_static(__FUNCTION__));
  $script = &$drupal_static[__FUNCTION__];

  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' : '';
  }

  // The base_url might be rewritten from the language rewrite in domain mode.
  if (!isset($options['base_url'])) {
    if (isset($options['https']) && variable_get('https', FALSE)) {
      if ($options['https'] === TRUE) {
        $options['base_url'] = $base_secure_url;
        $options['absolute'] = TRUE;
      }
      elseif ($options['https'] === FALSE) {
        $options['base_url'] = $base_insecure_url;
        $options['absolute'] = TRUE;
      }
    }
    else {
      $options['base_url'] = $base_url;
    }
  }

  // The special path '<front>' links to the default front page.
  if ($path == '<front>') {
    $path = '';
  }
  elseif (!empty($path) && !$options['alias']) {
    $language = isset($options['language']) && isset($options['language']->language) ? $options['language']->language : '';
    $alias = drupal_get_path_alias($original_path, $language);
    if ($alias != $original_path) {
      $path = $alias;
    }
  }

  $base = $options['absolute'] ? $options['base_url'] . '/' : base_path();
  $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];

  // With Clean URLs.
  if (!empty($GLOBALS['conf']['clean_url'])) {
    $path = drupal_encode_path($prefix . $path);
    if ($options['query']) {
      return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment'];
    }
    else {
      return $base . $path . $options['fragment'];
    }
  }
  // Without Clean URLs.
  else {
    $path = $prefix . $path;
    $query = array();
    if (!empty($path)) {
      $query['q'] = $path;
    }
    if ($options['query']) {
      // We do not use array_merge() here to prevent overriding $path via query
      // parameters.
      $query += $options['query'];
    }
    if ($query) {
      return $base . $script . '?' . drupal_http_build_query($query) . $options['fragment'];
    }
    else {
      return $base . $options['fragment'];
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.