diff options
author | Dries Buytaert <dries@buytaert.net> | 2007-02-15 11:40:19 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2007-02-15 11:40:19 +0000 |
commit | dc5843bd30a614cb074f70750ba2f631e61f8cb8 (patch) | |
tree | cd977b194f73b16e3dc1dfb40c9c9c5c1988f47f /includes | |
parent | e57b926e8d4385e399731159bd90f862962a86ab (diff) | |
download | brdo-dc5843bd30a614cb074f70750ba2f631e61f8cb8.tar.gz brdo-dc5843bd30a614cb074f70750ba2f631e61f8cb8.tar.bz2 |
- Patch #111347 by Steven: refactor url() and l().
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 144 | ||||
-rw-r--r-- | includes/file.inc | 2 | ||||
-rw-r--r-- | includes/form.inc | 4 | ||||
-rw-r--r-- | includes/pager.inc | 2 | ||||
-rw-r--r-- | includes/tablesort.inc | 2 | ||||
-rw-r--r-- | includes/theme.inc | 14 |
6 files changed, 96 insertions, 72 deletions
diff --git a/includes/common.inc b/includes/common.inc index a5deeae12..175f9c9c1 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -303,7 +303,7 @@ function drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response extract(parse_url(urldecode($_REQUEST['edit']['destination']))); } - $url = url($path, $query, $fragment, TRUE); + $url = url($path, array('query' => $query, 'fragment' => $fragment, 'absolute' => TRUE)); // Before the redirect, allow modules to react to the end of the page request. module_invoke_all('exit', $url); @@ -663,7 +663,7 @@ function locale_initialize() { * - !variable, which indicates that the text should be inserted as-is. This is * useful for inserting variables into things like e-mail. * @code - * $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", NULL, NULL, TRUE))); + * $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE)))); * @endcode * * - @variable, which indicates that the text should be run through check_plain, @@ -1120,25 +1120,39 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL * @param $path * The Drupal path being linked to, such as "admin/content/node", or an existing URL * like "http://drupal.org/". - * @param $query - * A query string to append to the link or URL. - * @param $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 '#'. - * @param $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. + * @param $options + * An associative array of additional options, with the following keys: + * 'query' + * A query string to append to the link, or an array of query key/value + * properties. + * 'fragment' + * A fragment identifier (or named anchor) to append to the link. + * Do not include the '#' character. + * 'absolute' (default 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 an RSS feed. + * 'alias' (default FALSE) + * Whether the given path is an alias already. * @return * a string containing a URL to the given path. * * When creating links in modules, consider whether l() could be a better * alternative than url(). */ -function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) { - if (isset($fragment)) { - $fragment = '#'. $fragment; +function url($path = NULL, $options = array()) { + // Merge in defaults + $options += array( + 'fragment' => '', + 'query' => '', + 'absolute' => FALSE, + 'alias' => FALSE, + ); + if ($options['fragment']) { + $options['fragment'] = '#'. $options['fragment']; + } + if (is_array($options['query'])) { + $options['query'] = drupal_query_string_encode($options['query']); } // Return an external link if $path contains an allowed absolute URL. @@ -1148,16 +1162,16 @@ function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) { // Split off the fragment if (strpos($path, '#') !== FALSE) { list($path, $old_fragment) = explode('#', $path, 2); - if (isset($old_fragment) && !isset($fragment)) { - $fragment = '#'. $old_fragment; + if (isset($old_fragment) && !$options['fragment']) { + $options['fragment'] = '#'. $old_fragment; } } // Append the query - if (isset($query)) { - $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $query; + if ($options['query']) { + $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $options['query']; } // Reassemble - return $path . $fragment; + return $path . $options['fragment']; } global $base_url; @@ -1176,35 +1190,37 @@ function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) { $clean_url = (bool)variable_get('clean_url', '0'); } - $base = ($absolute ? $base_url . '/' : base_path()); + $base = $options['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); + if (!$options['alias']) { + $path = drupal_get_path_alias($path); + } $path = drupal_urlencode($path); if (!$clean_url) { - if (isset($query)) { - return $base . $script .'?q='. $path .'&'. $query . $fragment; + if ($options['query']) { + return $base . $script .'?q='. $path .'&'. $options['query'] . $options['fragment']; } else { - return $base . $script .'?q='. $path . $fragment; + return $base . $script .'?q='. $path . $options['fragment']; } } else { - if (isset($query)) { - return $base . $path .'?'. $query . $fragment; + if ($options['query']) { + return $base . $path .'?'. $options['query'] . $options['fragment']; } else { - return $base . $path . $fragment; + return $base . $path . $options['fragment']; } } } else { - if (isset($query)) { - return $base . $script .'?'. $query . $fragment; + if ($options['query']) { + return $base . $script .'?'. $options['query'] . $options['fragment']; } else { - return $base . $fragment; + return $base . $options['fragment']; } } } @@ -1237,40 +1253,54 @@ function drupal_attributes($attributes = array()) { * @param $text * The text to be enclosed with the anchor tag. * @param $path - * The Drupal path being linked to, such as "admin/content/node". Can be an external - * or internal URL. - * - If you provide the full URL, it will be considered an - * external URL. - * - If you provide only the path (e.g. "admin/content/node"), it is considered an - * internal link. In this case, it must be a system URL as the url() function - * will generate the alias. - * @param $attributes - * An associative array of HTML attributes to apply to the anchor tag. - * @param $query - * A query string to append to the link. - * @param $fragment - * A fragment identifier (named anchor) to append to the link. - * @param $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. - * @param $html - * Whether the title is HTML, or just plain-text. For example for making an - * image a link, this must be set to TRUE, or else you will see the encoded - * HTML. + * The Drupal path being linked to, such as "admin/content/node". Can be an + * external or internal URL. + * - If you provide the full URL, it will be considered an external URL. + * - If you provide only the path (e.g. "admin/content/node"), it is + * considered an internal link. In this case, it must be a system URL + * as the url() function will generate the alias. + * - If you provide a path, and 'alias' is set to TRUE (see below), it is + * used as is. + * @param $options + * An associative array of additional options, with the following keys: + * 'attributes' + * An associative array of HTML attributes to apply to the anchor tag. + * 'query' + * A query string to append to the link, or an array of query key/value + * properties. + * 'fragment' + * A fragment identifier (named anchor) to append to the link. + * Do not include the '#' character. + * 'absolute' (default 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 an RSS feed. + * 'html' (default FALSE) + * Whether the title is HTML, or just plain-text. For example for making + * an image a link, this must be set to TRUE, or else you will see the + * escaped HTML. + * 'alias' (default FALSE) + * Whether the given path is an alias already. * @return * an HTML string containing a link to the given path. */ -function l($text, $path, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = FALSE) { +function l($text, $path, $options = array()) { + // Merge in defaults + $options += array( + 'attributes' => array(), + 'html' => FALSE, + ); + + // Append active class if ($path == $_GET['q']) { - if (isset($attributes['class'])) { - $attributes['class'] .= ' active'; + if (isset($options['attributes']['class'])) { + $options['attributes']['class'] .= ' active'; } else { - $attributes['class'] = 'active'; + $options['attributes']['class'] = 'active'; } } - return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>'; + return '<a href="'. check_url(url($path, $options)) .'"'. drupal_attributes($options['attributes']) .'>'. ($options['html'] ? $text : check_plain($text)) .'</a>'; } /** diff --git a/includes/file.inc b/includes/file.inc index 4b8e3c6e3..5afc66b0d 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -35,7 +35,7 @@ function file_create_url($path) { case FILE_DOWNLOADS_PUBLIC: return $GLOBALS['base_url'] .'/'. file_directory_path() .'/'. str_replace('\\', '/', $path); case FILE_DOWNLOADS_PRIVATE: - return url('system/files/'. $path, NULL, NULL, TRUE); + return url('system/files/'. $path, array('absolute' => TRUE)); } } diff --git a/includes/form.inc b/includes/form.inc index 49d8de0cf..8df277379 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -1038,7 +1038,7 @@ function theme_fieldset($element) { } } - return '<fieldset' . drupal_attributes($element['#attributes']) .'>' . ($element['#title'] ? '<legend>'. $element['#title'] .'</legend>' : '') . ($element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . $element['#value'] . "</fieldset>\n"; + return '<fieldset' . drupal_attributes($element['#attributes']) .'>' . ($element['#title'] ? '<legend>'. $element['#title'] .'</legend>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . $element['#value'] . "</fieldset>\n"; } /** @@ -1401,7 +1401,7 @@ function theme_textfield($element) { if ($element['#autocomplete_path']) { drupal_add_js('misc/autocomplete.js'); $class[] = 'form-autocomplete'; - $extra = '<input class="autocomplete" type="hidden" id="'. $element['#id'] .'-autocomplete" value="'. check_url(url($element['#autocomplete_path'], NULL, NULL, TRUE)) .'" disabled="disabled" />'; + $extra = '<input class="autocomplete" type="hidden" id="'. $element['#id'] .'-autocomplete" value="'. check_url(url($element['#autocomplete_path'], array('absolute' => TRUE))) .'" disabled="disabled" />'; } _form_set_class($element, $class); diff --git a/includes/pager.inc b/includes/pager.inc index edf2ba009..cabbe9e8b 100644 --- a/includes/pager.inc +++ b/includes/pager.inc @@ -392,7 +392,7 @@ function theme_pager_link($text, $page_new, $element, $parameters = array(), $at } } - return l($text, $_GET['q'], $attributes, count($query) ? implode('&', $query) : NULL); + return l($text, $_GET['q'], array('attributes' => $attributes, 'query' => count($query) ? implode('&', $query) : NULL)); } /** diff --git a/includes/tablesort.inc b/includes/tablesort.inc index 48a445828..b7f5d2003 100644 --- a/includes/tablesort.inc +++ b/includes/tablesort.inc @@ -83,7 +83,7 @@ function tablesort_header($cell, $header, $ts) { if (!empty($ts['query_string'])) { $ts['query_string'] = '&'. $ts['query_string']; } - $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], NULL, FALSE, TRUE); + $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE)); unset($cell['field'], $cell['sort']); } diff --git a/includes/theme.inc b/includes/theme.inc index de1be95dd..7d1972ac8 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -563,19 +563,13 @@ function theme_links($links, $attributes = array('class' => 'links')) { } $output .= '<li class="'. $extra_class . $class .'">'; - // Is the title HTML? - $html = isset($link['html']) && $link['html']; - - // Initialize fragment and query variables. - $link['query'] = isset($link['query']) ? $link['query'] : NULL; - $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL; - if (isset($link['href'])) { - $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html); + // Pass in $link as $options, they share the same keys. + $output .= l($link['title'], $link['href'], $link); } else if ($link['title']) { - //Some links are actually not links, but we wrap these in <span> for adding title and class attributes - if (!$html) { + // Some links are actually not links, but we wrap these in <span> for adding title and class attributes + if (empty($link['html'])) { $link['title'] = check_plain($link['title']); } $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>'; |