You are here

elf.module in External Links Filter 5.2

File

elf.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function elf_menu($may_cache) {
  $items = array();
  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 let them open in a new window.
          $("a.external-link").each(function() {
            $(this).click(function() {
              window.open($(this).attr("href"));
              return false;
            });
          });
        });
      ', '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 an !icon next to each external and mailto link with the class "external-link" or "mailto-link".', array(
      '!icon' => theme_image(drupal_get_path('module', 'elf') . '/elf.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);
}

/**
 * Implementation of hook_filter().
 */
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':

      // find all <a> tags and match their href so we can test if the link is external or not
      $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 the link is external (e.g., it starts with http) and it's not an absolute link to the current site
  if (strpos($match[1], 'http') === 0 && strpos($match[1], $site_url) === FALSE) {

    // if there is no class
    if (strpos($match[0], 'class="') === FALSE) {

      // it is faster to use PHP's string functions then complex regexes
      // in this case we strip off the last > in the <a> and then append our class and close the tag
      $link = substr($match[0], 0, -1);
      $link .= ' class="external-link">';
    }
    else {

      // append external class if this <a> already has a class
      $link = preg_replace('!class="([^"]+)"!', 'class="${1} external-link"', $match[0]);
    }
  }
  else {
    if (strpos($match[1], 'mailto:') === 0) {

      // if there is no class
      if (strpos($match[0], 'class="') === FALSE) {

        // it is faster to use PHP's string functions then complex regexes
        // in this case we strip off the last > in the <a> and then append our class and close the tag
        $link = substr($match[0], 0, -1);
        $link .= ' class="mailto-link">';
      }
      else {

        // append external class if this <a> already has a class
        $link = preg_replace('!class="([^"]+)"!', 'class="${1} mailto-link"', $match[0]);
      }
    }
  }
  return $link;
}

Functions

Namesort descending Description
elf_admin_settings
elf_filter Implementation of hook_filter().
elf_menu Implementation of hook_menu().
elf_replace