Drupal Twitter hashtag hack

Too many times I have written a post, got it ready to go, set up my Twitter module to tell the world, and then right after I hit submit, I realize that I forgot to add the Twitter hashtags that will carry my tweet to glory. It occurred to me that maybe I should give myself a reminder by adding some hashtags to my Tweet by default, and the taxonomy terms I am using for the post are a pretty good bet for general usefulness. I whipped up a site-specific hack to do just that and thought others might be interested. It could probably be generalized so that it could be made a patch to Twitter module, but I'm not sure that is a great idea anyway and I don't have the time to even think it through, so have at.

To do fun little hacks for a Drupal site, I always have a site-specific tweak module. The Twitter module hack I wanted was just adding some default text to the Twitter module's form field (the one that appears when you check the "Announce this post on Twitter" checkbox), so I added another case to my existing hook_form_alter (everyone's got at least one, right?). All I'm doing is checking to see if my node has some terms, in a particular vocabulary even, and if so, put a little pound sign in front, string 'em together and add it to the Twitter default form field. Then I can remove or edit as I please before I actually submit it. Even if I remove every single one of them, at least it will remind me to think about it, which has been my biggest frustration.

Big caveat: I never publish when I initially create my posts. This code hack assumes you are working on an edit form, and not a creation (node/add) form. My typical routine is to use Ecto to write my drafts and then push to the site in the "unpublished" state. I then review, tweak and publish. Doing this on node creation is understandably whack, since you don't have a term associated with the node until you've saved it, yah?

So here is the code I'm running on my site, in my rts_custom module:

<?php
//$Id:
/**
 * @file
 *   Custom tweaks for the rocktreesky.com site.
 */
/**
 * Implementation of hook_form_alter().
 */
function rts_custom_form_alter(&$form, $form_state, $form_id) {
  switch (
$form_id) {
   
// Snipped other form fun case....
   
case 'story_node_form':
     
// Only on node edit, not node add, since there are no terms yet on add.
     
if (arg(0) == 'node' && is_numeric(arg(1))) {
       
$node = node_load(arg(1));
       
// Get terms to turn into hash tags.
       
$terms = taxonomy_node_get_terms($node);
        if (
$terms) {
         
$hashtags = '';
          foreach (
$terms as $term) {
           
// Only hash the terms from the freetagging vocab (7).
           
$hashtags .= ($term->vid == 7) ? ' #' . $term->name : '';
          }
         
// Add to it, don't replace it.
         
$form['twitter']['status']['#default_value'] .= $hashtags;
        }
      }
      break;
  }
}
// Remove this closing PHP tag if you copy/paste this.
?>

One really important thing to remember is that the Twitter module fires fairly late in the process. By default, modules go in alphabetical order, so "T" is one of the last to go typically. My custom tweak module is named "rts_custom," so anything I do in my hook_form_alter is clobbered by twitter module, which fires after it (T coming after R). To fix that situation, I just needed to set my module's weight to be "heavier." Since this is just my quick and dirty tweaks module, I went into the database, found the rts_custom entry in the system table, and manually set the weight field to 1. If this was a module I wanted to deploy on a number of sites, I'd do that programmatically in a module .install file instead.

For those that are really new to this module thing, here is a rundown on how to make yer own:

  • Create a folder in yer sites/all/modules directory and call it whatever you want (site_tweaks is fine).
  • Create two files in the folder, a .module and a .info, using that same name (e.g. site_tweaks.module and site_tweaks.info).
  • Copy that code up above into yer .module file and rename the function with yer special name (e.g. site_tweaks_form_alter). Also, remove that closing ?>. It is there just to make it pretty in the blog post.
  • Copy this into the .info file:
    ; $Id$
    name = Site tweaks
    description = Does tweaky stuff to my site.
    core = 6.x
  • Enable the module and do that funky module weight thing I mentioned.
  • Bob's yer uncle.

Comments

Can you make the full rts_custom module available for review or download? I would like to see the hacks used by Drupal developers.

I try to avoid site specific hacks, but it seems like a necessary thing for Drupal..

There actually isn't much more to mine. The only other hack I have in there is to remove the website field on the comment form. Too much spam based just on that, but I still want people to be able to put their name. So, literally the only other thing I have is:

case 'comment_form':
      unset($form['homepage']);
      break;

placed where I have // Snipped other form fun case.... in the code above.

Excellent Post, Very nice...

excellent article, thank you

Excellent Post. mercy

i wrote a small hack to let twitter send you a direct message on new comments or other events.

http://www.lopsta.com/en/story/send-yourself-a-direct-tweet-new-comments...

Thanks for the idea and the great explanation.

http://drupal.org/project/hashtags - here is a module which implements hashtags for Drupal 6 and 7

Thanks chuwack for the latest link. I am new to drupal but learning quickly! Good info in this post.

Add new comment