Hello all, thanks for a brilliant module.

I'm a student and design a website aimed at other students, as such we have quite a lot of facebook links. I'd like to differentiate links to facebook from other external links by giving them a different icon.

I've made a new icon and a new css style and tried to pile some code together to make it all work, but haven't had much luck.

I'll paste what I've done so far below and it'd be great if someone could point out where I have gone wrong and how I should update what I have so far to make it work.

Many thanks,

Phil.

a.fb {
  background: url(fblink.gif) right center no-repeat;
  padding-right: 12px;
}

In the code below the '&& url.indexOf('media.iconradio.co.uk') == -1' is because the site is hosted on that server, just with a masked domain name - without this any absolute (but still internal) links were classes as external links - I hope that makes sense.

if (Drupal.jsEnabled) {
  $(document).ready(function() {
    // Strip the host name down, removing subdomains or www.
    var host = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
    var subdomain = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$1');

    // Determine what subdomains are considered internal.
    if (Drupal.settings.extlink.extSubdomains) {
      var subdomains = "([^/]*)?";
    }
    else if (subdomain == 'www.' || subdomain == '') {
      var subdomains = "(www\.)?";
    }
    else {
      var subdomains = subdomain.replace(".", "\.");
    }

    // Build regular expressions that define an internal link.
    var internal_link = new RegExp("^https?://" + subdomains + host, "i");

    // Find all links which are NOT internal and begin with http (as opposed
    // to ftp://, javascript:, etc. other kinds of links.
    // When operating on the 'this' variable, the host has been appended to
    // all links by the browser, even local ones.
    // In jQuery 1.1 and higher, we'd us a filter method here, but it is not   media.iconradio.co.uk
    // available in jQuery 1.0 (Drupal 5 default).
    var external_links = new Array();
    var mailto_links = new Array();
    $("a").each(function(el) {
      try {
        var url = this.href.toLowerCase();
        if (url.indexOf('http') == 0 && !url.match(internal_link) && url.indexOf('media.iconradio.co.uk') == -1 && url.indexOf('facebook.com') == -1 ){
          external_links.push(this);
        }
        else if (url.indexOf('mailto:') == 0) {
          mailto_links.push(this);
        }
		else if (url.indexOf('facebook.com') >= 0) {
          fb_links.push(this);
        }
      }
      // IE7 throws errors often when dealing with irregular links, such as:
      // <a href="node/10"></a> Empty tags.
      // <a href="http://user:pass@example.com">example</a> User:pass syntax.
      catch(error) {
        return false;
      }
    });

    if (Drupal.settings.extlink.extClass) {
      // Apply the "ext" class to all links not containing images.
      if (parseFloat($().jquery) < 1.2) {
        $(external_links).not('[img]').addClass(Drupal.settings.extlink.extClass);
      }
      else {
        $(external_links).not($(external_links).find('img').parents('a')).addClass(Drupal.settings.extlink.extClass);
      }
    }
	
	    if (Drupal.settings.extlink.fbClass) {
      // Apply the "fb" class to all facebook links.
      if (parseFloat($().jquery) < 1.2) {
        $(fb_links).not('[img]').addClass(Drupal.settings.extlink.fbClass);
      }
      else {
        $(fb_links).not($(fb_links).find('img').parents('a')).addClass(Drupal.settings.extlink.fbClass);
      }
    }
	

    if (Drupal.settings.extlink.mailtoClass) {
      // Apply the "mailto" class to all mailto links not containing images.
      if (parseFloat($().jquery) < 1.2) {
        $(mailto_links).not('[img]').addClass(Drupal.settings.extlink.mailtoClass);
      }
      else {
        $(mailto_links).not($(mailto_links).find('img').parents('a')).addClass(Drupal.settings.extlink.mailtoClass);
      }
    }

    if (Drupal.settings.extlink.extTarget) {
      // Apply the target attribute to all links.
      $(external_links).attr('target', Drupal.settings.extlink.extTarget);
    }
  });
}

and finally as part of the module file:

  $form['extlink_mailto_class'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add icon to mailto links'),
    '#return_value' => 'mailto',
    '#default_value' => variable_get('extlink_mailto_class', 'mailto'),
    '#description' => t('Places an !icon icon next to external links.', array('!icon' => theme_image(drupal_get_path('module', 'extlink') .'/mailto.png'))),
  );
  
  $form['extlink_fb_class'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add icon to facebook links'),
    '#return_value' => 'fb',
    '#default_value' => variable_get('extlink_fb_class', 'fb'),
    '#description' => t('Places an !icon icon next to facebook links.', array('!icon' => theme_image(drupal_get_path('module', 'extlink') .'/fblink.gif'))),
  );

  $form['extlink_subdomains'] = array(
    '#type' => 'checkbox',
    '#title' => t('Consider subdomains internal'),
    '#default_value' => variable_get('extlink_subdomains', 1),
    '#description' => t('If checked, links with the same primary domain will all be considered internal. A link from www.example.com to my.example.com would be considered internal. Links between the www. and non-www. domain are always considered internal.'),
  );

Comments

quicksketch’s picture

This might be possible with #402184: URLIcon module feature add, but I doubt External Links will provide site-specific icons for certain sites.

Phil Wolstenholme’s picture

Ah thank you - I'll check that out. I wasn't hoping for this feature to be added, more for some help in modifying the module slightly. Of course, using a module designed for this specific purpose is much better practice!

Thanks,

Phil

quicksketch’s picture

Status: Active » Closed (fixed)