Tag: code

Drush Make: Avoid the Unexpected

There are two things that are no secret and which form the basis for this post:

  1. Using the development versions of Drupal modules is sometimes the best choice.
  2. Using development versions as the project[module_name][version] parameter in a Drush Make file is always a bad choice.

How can we reconcile these two seemingly irreconcilable truths? The answer is actually very straightforward, and it’s even documented in Drush Make’s README.txt file itself1!

I tweeted a link to this pastebin snippet the other day. Keep that open, and let’s take a closer look.

This example comes directly from a Drush Make file I actually use:

; Download the module with Git
projects[webform_userpoints][download][type] = "git"

; Find this revision by clicking View Commits on the project page
; in the lower right-hand column. Click on the most recent commit
; in the branch you were going to use. Copy and paste the full revision
; identifier.
projects[webform_userpoints][download][revision] = "25bfe11d2c6dc6480d0aabc79b1edb54dec06236"

; Tell Drush Make it's a module. This might not be necessary.
projects[webform_userpoints][type] = "module"

; You can erase or change the subdirectory. I like to separate
; my Git-downloaded modules.
projects[webform_userpoints][subdir] = "_custom"

The comments are there for explanatory purposes, of course. They explain the basics, so let’s look at the finer points. First of all, what was my thinking behind doing it this way? To answer that, take a quick look at the Webform Userpoints module and the available releases. I’ll wait.

OK, as you can see, this module only has a development release. However, I’ve also used this technique for modules where the development version had a feature (or even a bug fix) that I needed. The benefit of doing it this way instead of simply specifying the version as “1.x-dev” is that your site will never suddenly stop working after a Drush Make (or Aegir)-based deployment because of the latest development version having different code than you expect (i.e., new code). Git repository revisions are fixed points in time and code. You can rely on them not changing. This is a very good thing.

Finally, I thought I should include a screenshot of where to find the commit identifier. There are a couple places; let’s start with the one I used the first time:

Click the "View commits" link on a project to get here

That’s the View Commits page for Webform Userpoints. It’s accessible through the right sidebar. You usually want the latest one for the development version you need. Click on it, and then click here to skip ahead.

Sometimes, though, especially in actively-developed projects, you’ll find that there are so many commits that it’s impractical to find the revision that way.  In these cases, the Repository Viewer is the answer. To get there, click Repository Viewer on the Drupal.org project page of the module or theme with which you’re dealing, and then under the heads section, find the one that looks like the development version you wanted. This will typically be something like 6.x-1.x. Next to that, you’ll see a few links such as shortlog, log, and tree. Click log, and then click on the title of the latest commit.

With either approach, this type of page is your final destination:

Once you click a specific commit, you wind up on a page like this

The highlighted text is what you want. This will be different for you unless you’re doing this for the same revision of the same module.

Bonus developer method: Clone the repository with Git and check out the branch corresponding to the development version. Run git log and copy the full commit identifier. Stick it in the right place in the Drush Make file. You probably didn’t even need me to write all this if you’re using this method.

1 http://drupalcode.org/project/drush_make.git/blob/086793e8887008a7841a5ef6081f8cf2766347db:/README.txt#l268

Code Sprint Report: San Diego, Conference Organizing Distribution (COD)

On May 7, I attended a code sprint in San Diego. Fortunately, Jerad Bitner has already blogged about this. Given that, I’ll share my experience and impressions rather than the event breakdown.

My previous code sprint was at SANDCamp 2011. That one was different in that the goals were planned out in advance and most of the time was spent writing code. In contrast, most of the time in this first COD sprint was spent planning, tweaking make files, and re-building feature modules. Porting an installation profile is a bigger job than writing a module, that’s for sure!

I mostly worked on researching the state of COD’s dependencies and updating the make file and groups.drupal.org wiki page accordingly.

The sprint was fun, and I’d do it again. It’s even better that the sprint helped move it forward.

I’ll be sprinting again at DrupalCon London, so look for me!

Code Snippet – Open Link in New Window in Drupal 7

Although this is not recommended, I’ve seen the question mentioned in the topic come up a couple times over the last few weeks:

How do I get my links to open in a new window or tab in the user’s browser in Drupal 7?

I’ve heard that the target=”_blank” method is once again valid in HTML 5…however, it will still make your HTML fail validation under other specifications. The way around this is to use JavaScript.

Copy and paste this snippet into the appropriate area of your site – usually a JavaScript file in your theme. When you want to make a link open in a new window or tab, instead of target=_blank, add rel=”external”.

(function ($) {
Drupal.behaviors.externalLink = {
attach: function (context, settings) {
$('a[rel="external"]', context).attr("target", "_blank");
}
}
}(jQuery));

The reason this workaround is OK to use is that it does a little something called separating presentation from behavior. In other words, you aren’t relying on the browser to take a particular action because of the HTML itself; you’re telling it what action to take in the JavaScript.