summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Haug2007-04-16 06:17:44 (GMT)
committer Nathan Haug2007-04-16 06:17:44 (GMT)
commit23b11c717e1fa25ffe03313422e910d8e134bb17 (patch)
treee87a77b7dd64fddf1a3338a70b9490a5838b4c96
parent1b1ea9c3e2e7abd1bf320fc9075f90f517067fcd (diff)
Adding token support to link module.
-rw-r--r--link.module42
1 files changed, 33 insertions, 9 deletions
diff --git a/link.module b/link.module
index 0b54ef8..858fe2a 100644
--- a/link.module
+++ b/link.module
@@ -229,7 +229,21 @@ function link_widget($op, &$node, $field, &$node_field) {
'#type' => 'markup',
'#value' => '<div id="' . str_replace('_', '-', $field['field_name']) . '-wrapper" class="clear-block"></div>',
);
-
+
+ // Add token module replacements if available
+ if (module_exists('token')) {
+ $form[$field['field_name']]['tokens'] = array(
+ '#type' => 'fieldset',
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#title' => t('Placeholder tokens'),
+ '#description' => t("The following placeholder tokens can be used in both paths and titles. When used in a path or title, they will be replaced with the appropriate values."),
+ );
+ $form[$field['field_name']]['tokens']['help'] = array(
+ '#value' => theme('token_help', 'node'),
+ );
+ }
+
// Add 'More' Javascript Callback
$form[$field['field_name']]['more-url'] = array(
'#type' => 'hidden',
@@ -496,7 +510,7 @@ function link_field_formatter($field, $item, $formatter, $node) {
}
$type = link_validate_url($item['url']);
- $url = link_cleanup_url($item['url']);
+ $url = link_cleanup_url($item['url'], 'http', $node);
// Seperate out the anchor if any
if (strpos($url, '#') !== FALSE) {
@@ -519,6 +533,10 @@ function link_field_formatter($field, $item, $formatter, $node) {
}
// Build the link with a title
elseif (strlen(trim($item['title']))) {
+ // Replace tokens
+ if (module_exists('token')) {
+ $item['title'] = token_replace($item['title'], 'node', $node);
+ }
$output = l($item['title'], $url, $attributes, $query, $fragment);
}
@@ -644,9 +662,15 @@ function link_views_protocol_filter_handler($op, $filter, $filterinfo, &$query)
* @param string $url
* @param string $protocol The protocol to be prepended to the url if one is not specified
*/
-function link_cleanup_url($url, $protocol = "http") {
+function link_cleanup_url($url, $protocol = "http", $node = NULL) {
$url = trim($url);
$type = link_validate_url($url);
+
+ // Replace tokens
+ if (module_exists('token')) {
+ $url = token_replace($url, 'node', $node);
+ }
+
if ($type == LINK_EXTERNAL) {
// Check if there is no protocol specified
$protocol_match = preg_match("/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i",$url);
@@ -676,7 +700,7 @@ function link_validate_url($text) {
$allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
$protocol = '((' . implode("|", $allowed_protocols) . '):\/\/)';
- $domain = '(([a-z0-9]([a-z0-9\-_]*\.)+)('. LINK_DOMAINS .'|[a-z]{2}))';
+ $domain = '(([a-z0-9]([a-z0-9\-_\[\]]*\.)+)('. LINK_DOMAINS .'|[a-z]{2}))';
$ipv4 = '([0-9]{1,3}(\.[0-9]{1,3}){3})';
$ipv6 = '([0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
$port = '(:([0-9]{1,4}))';
@@ -685,16 +709,16 @@ function link_validate_url($text) {
$external_pattern = '/^' . $protocol . '?'. '(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
// Pattern specific to internal links
- $internal_pattern = "/^([a-z0-9_\-+]+)";
+ $internal_pattern = "/^([a-z0-9_\-+\[\]]+)";
- $directories = "(\/[a-z0-9_\-\.~+%=&,$'():;*@]*)*";
- $query = "(\/?\?[a-z0-9+_\-\.\/%=&,$'():;*@]*)";
- $anchor = "(#[a-z0-9_\-\.~+%=&,$'():;*@]*)";
+ $directories = "(\/[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)*";
+ $query = "(\/?\?[a-z0-9+_\-\.\/%=&,$'():;*@\[\]]*)";
+ $anchor = "(#[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)";
// the rest of the path for a standard URL
$end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
- $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
+ $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
$email_pattern = '/^mailto:' . $user . '@' . '(' . $domain . '|' . $ipv4 .'|'. $ipv6 . '|localhost)' . $query . '$/';
if (preg_match($external_pattern . $end, $text)) {