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:

 * @file
 *   Custom tweaks for the 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;
// 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
  • 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.