View source
<?php
function elf_menu() {
$items['admin/settings/elf'] = array(
'title' => t('External links filter'),
'description' => t('Configure External links filter'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'elf_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
return $items;
}
function elf_init() {
if (variable_get('elf_css', TRUE)) {
require_once 'includes/common.inc';
$path = drupal_get_path('module', 'elf');
drupal_add_css($path . '/elf.css');
}
if (variable_get('elf_window', FALSE)) {
drupal_add_js('
$(document).ready(function() {
// Find all external links and let them open in a new window.
$("a.external-link").each(function() {
$(this).click(function() {
window.open($(this).attr("href"));
return false;
});
});
});
', 'inline');
}
}
function elf_admin_settings() {
$form = array();
$form['elf_css'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('elf_css', TRUE),
'#title' => t('Add CSS to place image next to all external and mailto links'),
'#description' => t('When enabled, this will include a CSS file on each page that adds a !link_icon to each external link and a !mailto_icon to each mailto link by using the class "external-link" or "mailto-link".', array(
'!link_icon' => theme_image(drupal_get_path('module', 'elf') . '/elf.png'),
'!mailto_icon' => theme_image(drupal_get_path('module', 'elf') . '/mlf.png'),
)),
);
$form['elf_window'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('elf_window', FALSE),
'#title' => t('Add JS to open external links in a new window'),
'#description' => t('When enabled, this will include inline JS that causes all external links to open in a new browser window.'),
);
return system_settings_form($form);
}
function elf_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('External links filter'),
);
case 'description':
return t('Adds a CSS class to all external and mailto links.');
case 'process':
$text = preg_replace_callback('!<a.*?href="([^"]+)".*?>!', 'elf_replace', $text);
return $text;
default:
return $text;
}
}
function elf_replace($match) {
$link = $match[0];
$site_url = url(NULL, array(
'absolute' => TRUE,
));
if (strpos($match[1], 'http') === 0 && strpos($match[1], $site_url) === FALSE) {
if (strpos($match[0], 'class="') === FALSE) {
$link = substr($match[0], 0, -1);
$link .= ' class="external-link">';
}
else {
$link = preg_replace('!class="([^"]+)"!', 'class="${1} external-link"', $match[0]);
}
}
else {
if (strpos($match[1], 'mailto:') === 0) {
if (strpos($match[0], 'class="') === FALSE) {
$link = substr($match[0], 0, -1);
$link .= ' class="mailto-link">';
}
else {
$link = preg_replace('!class="([^"]+)"!', 'class="${1} mailto-link"', $match[0]);
}
}
}
return $link;
}