Let's write a browser inside the browser, in Javascript!
Blog
Getting Ahead of Drupal's Coding Standards w/ Drupal Code Sniffer, Vim and a little Regex
If you've submitted a project for consideration in a Drupal.org project application or issue queue, you may have received a review that looks something like this, a seemigly-endless stream of errors relating to whitespace in comments, function argument documentation format, and horribly malformed "else" statements. We can thank Drupal Code Sniffer and it's web-service offspring, the Automated project review tool at ventral.org, for much of this turn toward strict compliance reviewing. While we all know that it's "the right way to go", it can often be quite discouraging to hvae to churn through a list of what feels like miniscule issues in order to share code with the community, especially when many of those issues are caused by inconsistencies between standard code editor templates, documentation auto-generators, and what Drupal.org requires.
The good news, though, is that we can get one step ahead of the review process, and integrate these new tools into our development environment to ensure that the code we write for Drupal is alway 100% compiant, from the start.
Print Search Box Form in Template Theme File, Drupal 7
Neither $search_form nor $search_box is available in Zen or, likely, any other themes in Drupal 7. Instead, there's a block.
If you need to print the search box in page.tpl.php, you'll need to render the block and then print it like so:
To break that down, 'search' is the module invoking the block. 'block_view' is the operation we want to call, that is, we want to view the block, and 'form' is the machine name of the block you want to print.
OS X 10.7 Lion Development: MacPorts
OS X Lion comes with most of the tools you would need to do "MAMP" (Mac OS X, Apache, MySQL/MariaDB, PHP) development, as outlined in my previous posts once you add a database. So then why would you want to use MacPorts? Setting your development environment up in MacPorts isolates the binaries, libraries, and configuration files, completely separate from the existing OS X install (with the exception of startup scripts). You can also tweak the configuration files on your own, apply your own patches, and apply updates that MacPorts may get before Apple pushes them. It will take more time because you'll be compiling everything, but you have all of the control. Read on for how to get things set up.
Mixing Context and Drupal's Native Block Ordering System
I work on a lot of legacy sites that were built without the Context module, and I would say that at least once on each of these legacy sites, I turn to Context when I have a very specific problem: the visibility settings for a particular block are so complex that (in Drupal 6 at least) they can only be expressed by writing code. For instance: suppose you're using Organic Groups, and you want to show or hide a block based on whether the user is a member of that group.
OS X 10.7 Lion Development: Native Apache & PHP with Homebrew MySQL or MariaDB
OS X Lion ships with Apache and PHP, which both require a little bit of tweaking to get fully-functional for "MAMP" local development. The one thing Lion does not ship with is a database. This will be very similar to my previous post on local development but this time we'll be using Homebrew to install either MySQL or MariaDB for the database. Since we'll be using a compiler for Homebrew, I'll also cover how to add APC and other PECL modules that you can add to OS X.
OS X 10.7 Lion Development: Native MAMP with MySQL installer
With the release of Lion, there are some subtle differences to setting up a local MAMP (Mac OS X, Apache, MySQL, PHP) environment compared to Snow Leopard. In an effort to keep this from being overly wordy and just get to the good stuff, we'll dive right in, so read on to get started.
Use Syslog instead of Watchdog
It seems to be standard for Drupal, at least in version 6, that Watchdog is enabled and used to write errors/warnings/notices to the watchdog table in the database. Depending on the verbosity of your PHP output, amount of 404s, or other information that would be going into this log, it could mean a lot of writes on your database. While you should trace errors/warnings and solve them, sometimes you may want this increased information for debugging. If your site receives a lot of traffic, this could mean decreased database performance. A solution to this is to instead utilize the core Syslog module instead of Watchdog. Here's how I got it set up in Drupal 6 on CentOS 5 servers.
Drupal 7 hook_block changes, with example template module
Drupal seven broke out the old hook_block($op = 'list', $delta = 0, $edit = array()) function, which took an operation as an argument, into four functions, hook_block_info, hook_block_save, hook_b
Views Search by Link Module's URL Title: More Views Hacking
So recently I wanted to filter by URL title, having used it extensively as a text field with an optional link in a site.
The code below is supposed to apply to an exposed Link module field filter in Views. Instead of searching to see if the search string matches the URL, this will match the URL title that was provided.
In the code below, you would replace node_data_{field_publication_author.field_publication_author} with the name of your field, in all places that "field_publication_author" occurs in the below.
Removing Behaviors Using a GMap Macro
I'm building some maps for a client using the GMap module and came across the following simple problem: using a GMap macro, how do you remove a default attribute?
The GMap module lets you set a number of default attributes for your maps: width, height, center position, initial zoom, and so on. If you want to change any of the default settings for a given map, you need to specify your overrides in a GMap macro, which looks something like this:
[gmap zoom=3| width=500px |height=350px]
It's all simple enough until you get to the behaviors.
Preventing empty CCK-based blocks from printing
Today I learned that if you have a Views-powered block that uses CCK fields; and the CCK fields are empty for all the returned rows; then Views doesn't realize you've got an empty set and it still prints all the scaffolding for your block – the admin links, the container divs, and so on. The content of the block is empty, but the HTML still takes up space and throws off your layout.
Fixing this is easy. To prevent an empty CCK field-based Views-powered block from being output, add a filter on the CCK field and set the filter to Is not empty (NOT NULL).
Setting JS Callbacks with Drupal, JSON and Some jQuery Sauce (for jCarousel controls)
Sometimes a few characters of code can make all the difference in the world. All coders know the scenario: the code looks fine, all the right values are in the right place, the code validates or compiles (or at least looks like it should), but when we run it we get...nada. Maybe the source of the problem is a comma following an array declaration (fine in PHP, but can be a killer in JS), an extra "$" in a variable name, or - as in the case of trying to define a callback for jQuery libraries like jCarousel using JSON - an extra pair of quotes around the callback function value.
Unfortunately, the difference of a few quotes is not always so easily overcome. In the case of extraneously quoted JSON function values, for instance, the issue originates from within the drupal_add_js function and PHP's own json_encode. Such situations can lead to desperate measures: grabbing the drupal_get_js output and munging it with regular expressions or other hackish means to make it do what we need. While use of such techniques is something of a religious dispute among Drupalists, I'm happy to announce that there's also another way to tackle JSON callbacks in Drupal, with nothing more out-of-line than drupal_add_js and a bit of simple jQuery magic.
Keep Drush Up to Date
We're big fans of Drush. It's installed on all of our servers and it's a great way to perform maintenance tasks, download core and modules, and much much more. I'm not a big fan of installing from zip files though, so let's use git to easily keep our Drush install up to date.
Set Sitewide Default Context in Drupal
In Context 3 they removed the sitewide default context, that is, the context that can be set to be enabled when no other context is active. The sitewide context, which is enabled at all times, is still available.
But, in its place, Dev Seed has added a very powerful feature, sneakily powerful, in fact.
You can set a context to be active using another context. Seeming kind of meh? Well, read on. You will soon see, young apprentice, just as I did just earlier today.
First, name all the contexts you DON'T want this context to coincide with in a regularized way, like
DrupalCon Chicago: A Roadmap for Drupal 8
While Dries' keynote touched on a number of topics -- from the scalability of Drupal to individual shout-outs to the top 30 core contributors -- the bulk of the talk focused on the roadmap for Drupal 8. Here's a quick rundown of what we can look forward to in the next release of Drupal:
- Drupal to any device, particularly mobile ones. If you look at the prevalence of net-connected computing devices in 2011, desktops are a minor, stagnant sector, whereas mobile devices -- anything from a smartphone, to a laptop, to a TV -- are more numerous, and their numbers are increasing.
Git Access on Drupal: How to Get and Use Git to Start Committing Modules and Patches
Want to contribute back to the Drupal community? Don't be afraid, Git is not as hard as you might think. There are plenty of resources available to those who search. Drupal recently switched over to Git from CVS. Much of the documentation on Drupal.org is out of date, so if you see documentation related to CVS, remember that Drupal now uses Git.
This post will serve as a way of helping you to get involved in the Drupal community and contributing modules back.
Get Git Access
Six Tricks for Developing a Web/Hybrid App on iOS
One of the compelling reasons to developing web-based or hybrid mobile applications is being able to use the tried and true HTML/CSS/JS stack, which allows your app to be used (with some minor restrictions) directly as a mobile website or to be compiled and distributed as a native application using a tool such as PhoneGap.
Dump each MySQL table to a file
Here's a one-liner to dump each table in a database to it's own .sql file. Crack open your shell of choice and follow along.
Fix pear permissions problem on Mac OS X
Delicious Feed Broken? Delicious Changed Its Domain Name
I ran into an issue recently where Delicous links weren't being updated as expected. It turned out sometime in August 2010 Delicious switched from using http://delicious.com/ to http://www.delicious.com/
I fixed my problem by changing the Views filter from http://delicious.com to http://www.delicious.com
Most changes should be as easy as that.










