Using Hooks & Avoiding Hacking

Using Hooks & Avoiding Hacking

Programmers quickly learn principles and best practices to apply when developing in a specific framework. Drupal development has its own set of unique programming standards. For modifications, Drupal is indeed changeable, and one of the most flexible platforms for building custom web applications.

In fact, almost everything in Drupal is open for customization, with one well-known caveat. Editing source code, or what is labelled "hacking", is absolutely frowned upon.

A newbie to Drupal might wonder how changes are made without altering any code. The short answer is through contributed modules or even your own custom-built modules. By utilizingHooks, programmers may customize the behavior and output of add-on modules. The term 'Hook' indicates an entry point for alterations. Various hook entry points exist within Drupal:

Modules: Modules are predominantly collections of hook implementations. They include PHP functions that define the hook’s output or behavior. Modules may also contain supporting code, CSS, JavaScript, and other files.

Themes: Themes contain collections of theme hook overrides (PHP functions or template files that define how output is presented). When your objective is to alter Drupal's look-and-feel, generally you should override theme hooks in a theme to change the presentation of data. (Modifying data output would require implementing a hook in a module).

Why Can't I Hack?

Again, there are programming communities where the term "hack" is a necessary, even celebrated evil. When it comes to Drupal, hacking is a cardinal sin. The word embodies any behavior that involves editing code downloaded from Drupal.org. "Never Hack Core", a cautionary statement concerning editing of Drupal core files, is as dirty as it sounds. Take a page out of Grandmaster Flash's book and 'don't do it'.

Here are a few more reasons why hacking is neither advisable, nor required to make changes to your site:

• Typically, hooks will exist to accomplish your goal without hacking.

• Once Drupal core is hacked, a proprietary CMS is created, closing the door to accessing new releases and security upgrades. Releases will often include bug fixes, security fixes, and nifty new features.

Using Hooks

Another benefit to programming with hooks, instead of hacking, is the ability to progressively watch your back. Specifically, you can turn off changes by disabling module(s) containing the hooks. If you hack code, on the other hand, you will need to reverse-engineer your changes to apply updates. Once you've implemented hooks provided by other modules, you preserve the integrity of your entire Drupal system and website. Hooks create a gateway to share data and/or extend with other modules. All modules utilize hooks to interact and function within Drupal core.

Invoking Hooks

If a hook is not available to resolve your issue, this may present a catch-22. Hacking might become necessary in order to fix a bug or add a new feature to a module. Be sure to backup your site, as you normally would prior to making big changes. In the event your hackathon yields positive results, you may have accomplished a goal for many besides yourself. Submitting your fix as a patch would benefit the wider Drupal community. Moreover, building a new Drupal module from scratch might also add a valuable new feature to the directory of available modules for the community.

Hook Examples

• hook_form_alter() - When creating a form, Drupal checks to see if any modules have hook_form_alter requests. For example, this hook may be used to make tweaks with the node edit form, including changing labels, adding a new checkbox, redirects, and more.

• hook_block() - Drupal looks for hook_block requests from modules upon viewing of either the blocks page or a page that contains block(s). If you would like your module to create it's own block, this is the hook you would use.

You may find a full list of calls to module invoke on Drupal 7 on Drupal.org.

In the rare instances where it's done, hacking is reserved for specific sites or individuals who are extremely familiar with the Drupal “code base, development practices and security model” – the Drupal elite. Drupal.org provides a rather concise guideline with regard to hacking, stating "If you have to ask, chances are you shouldn't"; choice words with a clear message.

Tags: