Alfred Workflow to Find Code Examples in Drupal

Here’s something that happens to me everyday: I’m working on coding a Drupal module and I need to use a function that I know I’ve used elsewhere, and I want to use that earlier case as an example.

What I’ve done to this point is to try to remember where I used the function, then open that code in BBEdit and do a multi-file search for the function name. This is neither reliable nor particularly efficient, so I took a moment to create an Alfred Workflow to solve the problem (Alfred is an excellent “how did I live without this?!” app that is very good for solving this sort of problem).

Here’s how I did it:

First, I created a new Workflow (from the Alfred “Workflows” tab I clicked the “plus”) and selected the “File filter from keyword and open” template:

Alfred Workflows Templates

I then modified the template to create an Alfred keyword “example”:

Alfred Workflow Basic Setup

Under the “Search Scope” tab I dragged-and-dropped the Subversion working copy of my Drupal “sites” filter, where all my custom Drupal code lives:

Alfred Workflow Search Scope

And, finally, under the “Advanced” tab I modified the “Fields” section to use the “kMDItemTextContent” field (the contents of the files):

Alfred Workflow Advanced

With this Workflow in place, I can now simply activate Alfred (Control+Space) and type something like example node_load (if I want to see code where I’ve used the node_load function); I see a list of files that contain that string and I can select one, hit “enter” and the file in question opens in BBEdit.

Alfred Example

Nothing about this is Drupal (or BBEdit) specific, of course: you could use a similar Workflow in many different environments to solve the same sort of problem.