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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">//$Id:<br>/**<br> * @file<br> * Custom tweaks for the rocktreesky.com site.<br> */<br>/**<br> * Implementation of hook_form_alter().<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">rts_custom_form_alter</span><span style="color: #007700">(&</span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_id</span><span style="color: #007700">) {<br> switch (</span><span style="color: #0000BB">$form_id</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Snipped other form fun case....<br> </span><span style="color: #007700">case </span><span style="color: #DD0000">'story_node_form'</span><span style="color: #007700">:<br> </span><span style="color: #FF8000">// Only on node edit, not node add, since there are no terms yet on add.<br> </span><span style="color: #007700">if (</span><span style="color: #0000BB">arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) == </span><span style="color: #DD0000">'node' </span><span style="color: #007700">&& </span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">arg</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">))) {<br> </span><span style="color: #0000BB">$node </span><span style="color: #007700">= </span><span style="color: #0000BB">node_load</span><span style="color: #007700">(</span><span style="color: #0000BB">arg</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br> </span><span style="color: #FF8000">// Get terms to turn into hash tags.<br> </span><span style="color: #0000BB">$terms </span><span style="color: #007700">= </span><span style="color: #0000BB">taxonomy_node_get_terms</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br> if (</span><span style="color: #0000BB">$terms</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$hashtags </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br> foreach (</span><span style="color: #0000BB">$terms </span><span style="color: #007700">as </span><span style="color: #0000BB">$term</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Only hash the terms from the freetagging vocab (7).<br> </span><span style="color: #0000BB">$hashtags </span><span style="color: #007700">.= (</span><span style="color: #0000BB">$term</span><span style="color: #007700">-></span><span style="color: #0000BB">vid </span><span style="color: #007700">== </span><span style="color: #0000BB">7</span><span style="color: #007700">) ? </span><span style="color: #DD0000">' #' </span><span style="color: #007700">. </span><span style="color: #0000BB">$term</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">: </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br> }<br> </span><span style="color: #FF8000">// Add to it, don't replace it.<br> </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'twitter'</span><span style="color: #007700">][</span><span style="color: #DD0000">'status'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#default_value'</span><span style="color: #007700">] .= </span><span style="color: #0000BB">$hashtags</span><span style="color: #007700">;<br> }<br> }<br> break;<br> }<br>}<br></span><span style="color: #FF8000">// Remove this closing PHP tag if you copy/paste this.<br></span><span style="color: #0000BB">?></span></span>
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$<br>name = Site tweaks<br>description = Does tweaky stuff to my site.<br>core = 6.x
- Enable the module and do that funky module weight thing I mentioned.
- Bob's yer uncle.