View source
<?php
function elf_menu($may_cache) {
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/elf',
'title' => t('External links filter'),
'description' => t('Configure External links filter'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'elf_admin_settings',
),
'access' => user_access('administer site configuration'),
);
}
else {
if (variable_get('elf_css', TRUE)) {
$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 set target to new window
$("a.external-link").attr("target", "_blank");
});
', 'inline');
}
}
return $items;
}
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, NULL, NULL, 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;
}