You are here

elf.module in External Links Filter 5

File

elf.module
View source
<?php

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

/**
 * 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, NULL, NULL, 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_menu Implementation of hook_menu().
elf_replace