Avoiding Broken Links with Drupal's Twitter Module


Drupal's Twitter module is a neat addition to any blog. It allows you to integrate Twitter with Drupal in two different ways:

  1. It will import your latest tweets into your user profile, or into a block
  2. It will automatically post to your Twitter account when you author new content

Downloading and installing the module is pretty simple, just visit the project page and grab the latest stable release. After enabling the module, go to Administer > Site configuration > Twitter setup. You should end up at /admin/settings/twitter. In the Twitter posting section, pay attention to the Default format string field. I decided to use this format string:

New post: !title (!url)

Do not use this format string. It will generate 404 errors for any user that visits your page. Here's what happens: Drupal generates a long URL to replace the "!url" macro. Twitter takes that long URL and shortens it to the form http://tr.im/XXXX. Unfortunately, when Twitter shortens the URL, it will append the closing parentheses to it, causing a 404 error. To fix this, use a format string with the "!url" macro at the end, such as:

New post: !title !url

Final note: If you're using pathauto, you may notice that your URLs are showing up as node/55 instead of the URL alias that you want to use. The Twitter is being updated with a new, heavier module weight in order to have it run after pathauto. In the meantime, you can give the Twitter module a heavier weight yourself, by issuing a SQL command like:

UPDATE system SET weight=10 WHERE name='twitter'

More information here.

Did you enjoy this post? Please spread the word.