Commit 23b11c71 authored by Nathan Haug's avatar Nathan Haug
Browse files

Adding token support to link module.

parent 1b1ea9c3
......@@ -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)) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment