Quicktips

DrupalEasy Quicktips are short, focused posts intended to accomplish a very specific goal. Quicktips are exclusively available first via our @drupaleasy Twitter account.

Module Development: Adding Form Fields via AJAX/AHAH

Adding form fields to a Drupal form dynamically is not the most straight-forward task. You'll see this occasionally on a Drupal form (poll module jumps to mind) when there is an "add another" button that (seemingly) simply adds another set of input elements to the form. The big sticking point in setting this functionality up is doing it in a way that keeps FormAPI's security intact. Fortunately, there's some tips to get you mentally started.

Module Development: Understanding Wildcard Loader Arguments

When writing a module from scratch (or trying to deconstruct one to figure out how it works), it might save you some time to be aware of "wildcard loader arguments" when using hook_menu(). These wildcard arguments are automatically linked with "_load()" functions that allow the module developer to load data into the argument anytime the menu item is called.

Here's a quick example of what I'm talking about. Look at this snippet from the Quicktabs module's quicktabs_menu() function:


Simple and exact image cropping

We all know imagecache is a great module to grab images of any size that are in your site's files/ directory, but sometimes it's hard to get every image to be the right size without black borders on the sides or top. The trick is to know the proper "order of operations".

Here's a fast recipe for square thumbnails that will work with landscape or portrait picture orientations:
Scale - width: 85, height: , upscale: No
Crop - width: 85, height: 85, xoffset: , yoffset: top
Scale And Crop - width: 25, height: 25

Why filter_xss() is a Friend to Module Developers

When writing custom modules, few functions are as useful as filter_xss(). In a nutshell, this function protects your web site from both malicious HTML and unwanted tags.

The function takes some text and, in effect, "cleans" it based on some very simple rules. First and foremost, it makes sure any HTML elements, tags, and attributes are well-formed. It also removes any non-standard characters and constructs that can trick browsers. Finally, it can remove any HTML tags depending on an argument passed to the function.

Print, Email, PDF That Page!

So often clients ask me for the ability to add an "email this page" or "print this page" option to their web site. Luckily, the print module does both these things - and more.

The module is a snap to set up and even offers the capability to print any web page as a PDF (using an external, free, open-source library). It has loads of options that allow you to tweak the position and language of the links and provides several theme functions that can be overridden so you can match the links to your site's theme.

Hide That Field!

So you have a content type that you created with CCK that has more than a few fields. In some cases, you don't want all of the fields to show up when viewing the full node - luckily, there's an easy way to hide individual fields without resorting to theming tricks or PHP code.

Simply go to the content type's "edit" page (assuming you're logged in as an appropriate administrative user) and click on the "Display fields" tab. From here you can hide or exclude individual fields from view in both teasers and full nodes.

Easy DB Backups

When developing a new site or adding features to your old site, it's always a good idea to do frequent database "development" backups (as opposed to normal "everyday" backups that you're already doing, right?) While there are a bunch of different methods for performing a backup (via the command line, phpMyAdmin), one of the easiest is with the Backup and Migrate module.

New Module? Check Permissions!

One thing that consistently trips up both new and experienced developers when enabling a new module are "permissions". I can't tell you how many times I've enabled a new module then cursed my computer for a few minutes thinking that the module wasn't working properly - only to realize that I forgot to set some arcane permission on the admin/user/permissions page.

So, next time you download and enable that shiny new module, be sure to not only configure it using its settings page, but to check its permissions!