You are here

function elf_replace in External Links Filter 6.3

Same name and namespace in other branches
  1. 5.3 elf.module \elf_replace()
  2. 5 elf.module \elf_replace()
  3. 5.2 elf.module \elf_replace()
  4. 6.2 elf.module \elf_replace()
  5. 7.3 elf.module \elf_replace()

Add classes to external or mailto links in a text.

Parameters

$text string: The text to filter.

Return value

string The (filtered) text.

1 call to elf_replace()
elf_filter in ./elf.module
Implementation of hook_filter().

File

./elf.module, line 132
Adds an icon to external and mailto links.

Code

function elf_replace($text, $format) {
  $document = elf_filter_dom_load($text);
  $links = $document
    ->getElementsByTagName('a');
  foreach ($links as $a) {
    if ($href = $a
      ->getAttribute('href')) {

      // This is a mailto link.
      if (strpos($href, 'mailto:') === 0) {
        $a
          ->setAttribute('class', $a
          ->getAttribute('class') ? $a
          ->getAttribute('class') . ' elf-mailto elf-icon' : 'elf-mailto elf-icon');
      }
      elseif (elf_url_external($href)) {

        // Add external class.
        $a
          ->setAttribute('class', $a
          ->getAttribute('class') ? $a
          ->getAttribute('class') . ' elf-external elf-icon' : 'elf-external elf-icon');
        if ($a
          ->getElementsByTagName('img')->length > 0) {
          $a
            ->setAttribute('class', $a
            ->getAttribute('class') ? $a
            ->getAttribute('class') . ' elf-img' : 'elf-img');
        }

        // Add nofollow.
        $nofollow = variable_get("filter_html_nofollow_{$format}", FALSE);
        if ($nofollow) {
          $rel = array_filter(explode(' ', $a
            ->getAttribute('rel')));
          if (!in_array('nofollow', $rel)) {
            $rel[] = 'nofollow';
            $a
              ->setAttribute('rel', implode(' ', $rel));
          }
        }

        // Add redirect.
        if (variable_get('elf_redirect', FALSE)) {
          $a
            ->setAttribute('href', url('elf/go', array(
            'query' => array(
              'url' => $a
                ->getAttribute('href'),
            ),
          )));
        }
      }
    }
  }
  return elf_filter_dom_serialize($document);
}