03
May
2013

Extending the Follow Module to Add Custom Networks

Extending the follow module is really pretty simple.  

Follow along, I'll show you how it's done!
(Bad pun may or may not have been intended)

I'm using the follow module on this site to provide links to my various social network profiles and whatnot across the net, and it's pretty handy for that.  

However, when I recently found myself added to imdb, I thought it would be nice to include a link to that page along with my others.  I also thought it might be nice to link to my profile on Drupal.org, seeing as how this blog primarily focuses on Drupal related topics.

As it turns out, doing so is really straight forward. All that is involved is implementing an alter hook in a custom module, and adding a single line of css to your theme for the icon.

Lets get to it!

As I hadn't previously created a 'glue code' module for this site, I did so for this project by creating a module called wv_custom.  The hook I needed to implement to add additional networks to the follow module is HOOK_follow_networks_alter(). It's probably worth noting that with this alter hook you could also change the title or domain of an existing network, or remove networks from the list too, but we're not going to go into that here, we're focused on adding a couple new ones.

So, I wanted to add imdb and drupal.org to the list, here's the code to do that:

function wv_custom_follow_networks_alter(&$networks, $uid = 0) {
  // Add a network.
  $networks[$uid]['imdb'] = array(
    'title' => t('IMDB'),
    'domain' => 'imdb.com',
  );

  $networks[$uid]['drupal'] = array(
    'title' => t('Drupal.org'),
    'domain' => 'drupal.org',
  );
}

Simple, right?  the networks array is passed in by reference, so there's no need for a return value. All we had to do was tack on a couple extra options.

Note: If the new options don't show up for you, it's probably a caching issue.  For me the APC opcode cache was preventing them from showing up in the list until I restarted the php process on the server.  As I've told many clients over the past several years when they have weird issues with a Drupal site, "when in doubt, clear cache."

Ok, now for the css.

First, I dropped my icons into the icon directory for the follow module. This probably isn't ideal since they could theoretically go away if I update the module, putting them into the theme would make more sense.

Next, and final, step was to add a couple lines of css to my stylesheet:

a.follow-link-imdb {
  background-image: url(/sites/all/modules/follow/icons/small/icon-imdb.png);
}

a.follow-link-drupal {
  background-image: url(/sites/all/modules/follow/icons/small/icon-drupal.png);
}

See those selectors?  a.follow-link-[something] the "something" is the array key used previously in the alter hook.  So any time an imdb link is added it will have a class of follow-link-imdb.

Easy Peasy.