You are here

elf.module in External Links Filter 6.2

File

elf.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
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;
}

/**
 * Implementation of hook_init().
 */
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);
}

/**
 * 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 test their href values.
      $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,
  ));

  // The link is external, but does not point to the site itself.
  if (strpos($match[1], 'http') === 0 && strpos($match[1], $site_url) === FALSE) {

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

      // It is faster to use PHP's string functions than complex regexes. In
      // this case we strip off the greater than sign, add our class and
      // close the tag.
      $link = substr($match[0], 0, -1);
      $link .= ' class="external-link">';
    }
    else {

      // The class attribute already has a value. Append our own to it.
      $link = preg_replace('!class="([^"]+)"!', 'class="${1} external-link"', $match[0]);
    }
  }
  else {
    if (strpos($match[1], 'mailto:') === 0) {

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

        // It is faster to use PHP's string functions than complex regexes. In
        // this case we strip off the greater than sign, add our class and
        // close the tag.
        $link = substr($match[0], 0, -1);
        $link .= ' class="mailto-link">';
      }
      else {

        // The class attribute already has a value. Append our own to it.
        $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_init Implementation of hook_init().
elf_menu Implementation of hook_menu().
elf_replace