chacadwa.com

Technical blog and writings by Micah Webner.

Simple hook_form_alter Module for Drupal 5

Note: I've updated this tutorial for Drupal 6 in this post on the Geeks and God website.

This example "mysite" module demonstrates hook_form_alter. There is a lot of good info out there about hook_form_alter, but I found very few examples that put all of the pieces of custom module creation together in such a simple way as this.

The also function adds an example "form_array" element that displays everything in a form using print_r. Installing the Devel Module is probably a better way to get this same information. You wouldn't want to leave this hunk of code on a production site, but it is pretty nice for seeing what form elements are available to manipulate.

Uncomment print $form_id; to show the name of every form on your site as you navigate.

mysite.info

name = mysite description = Customizations and tweaks for my site. package = Other

mysite.module

<?php // $Id: mysite.module,v 0.1 2008/06/07 18:16:00 micah Exp $ /** * See http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/drupalorg/ * for additional examples. */ /** * Implementation of hook_form_alter(). */ function mysite_form_alter($form_id, &$form) { // print $form_id; switch ($form_id) { case 'story_node_form': // Place your form altering code here. unset($form['menu']); unset($form['author']); unset($form['log']); $form['form_array'] = array( '#value' => '<pre style="color: #000; background: #cba; border: 1px solid brown;" >'. print_r($form, 1) .'</pre>', '#weight' => '5', ); break; case 'story_node_form': unset($form['log']); break; case 'user_login': $form['name']['#description'] = t('Enter your username, if you can remember it.'); $form['pass']['#description'] = t('You probably forgot this. Just use password reset again.'); break; /* might want to do similar thing for user_login_block */ } }
Topics: