Introduction:
Drupal is a great open-source project that combines being a unified web-framework, a serious API and is highly modular due to its architecture. It is also very community driven, powerful and above all, caters to the developer and the themer. Off the bat, Drupal can be a little big confusing and intimidating for the beginner. In this mini-series, I will show you various ways of adding fields to the registration and user process in Drupal.
The method I am going to outline here is using a module to create a legal agreement option during user registration. That option will feature a yes/no dialog and will be validated upon user completion. If the user says no, the user will be warned.
Requirements:
- Working Drupal development site
- Drupal development environment (Howto on configuring Aptana)
- File access to the development site
It is also good to have a basic understanding of the Drupal API.
Getting Started:
After getting your environment fired up, connect to your Drupal development and navigate to:
/drupal home/sites/all/modules
Note: If the modules directory does not exist then create it.
Next, create a directory called custom, and then create a directory called loginagreement. The path should look like:
/drupal home/sites/all/modules/custom/loginagreement
Creating the module:
Create these three files inside of the loginagreement directory. This is the basic procedure to create a module and I will not go much further into that.
- loginagreement.info
- loginagreement.module
- README.TXT
Generating the .TXT file:
Inside of README.TXT add a bit of text explaining what the module is about. Something like this:
Its always a good idea to include readmes in your code even if you wrote it. You or someone else may come back to it later and want to know what the code is about quickly and efficiently.
=======================
Author: Ron Brash
Date: Feb 4th, 2025Purpose:
The purpose of this module is to help learn the modularity of the Drupal system and alter the user_registration form.
Generating the .info file:
Inside loginagreement.info file add something similar to the following:
; $Id$
name = Login Agreement
description = Displays a legality notice agreement to the user upon registration
package = Orangespike
core = 6.x
Note: The $Id$ tag is for subversion and is a good habit to introduce into your code.
Generating the .module file:
Inside loginagreement.module add the following:
<?php
// $Id$/**
* @file
* Support for the legal agreement during user registration
*/
Note: The $Id$ tag is a little bit different and we use Oxygen styled comments to clarify the code and the modules.
Next, we create an implementation of hook_user and we modify it or more correctly, override it with our new module.
/**
* Implementation of hook_user().
*/
function legalagreement_user($op, &$edit, &$user, $category = NULL) {
switch($op) {
case 'register':
$fields['legal_agreement'] = array(
'#type' => 'fieldset',
'#title' => t('Legal And Privacy Agreement')
);
$fields['legal_agreement']['decision'] = array(
'#type' => 'radios',
'#description' => t('By registering at %site-name, you agree to our legal and privacy notices.</a>',
array('%site-name' => variable_get('site_name', 'drupal'))),
'#default_value' => 0,
'#options' => array(t('I disagree'), t('I agree'))
);
return $fields;
case 'validate':
if (isset($edit['decision']) && $edit['decision'] != '1') {
form_set_error('decision', t('You must agree to the Legal Agreement before registration can be completed.'));
}
break;
case 'insert':
watchdog('user', t('User %user agreed to legal terms',
array('%user' => $user->name)));
break;
}
}
Let me explain this code. legalagreement_user() modifies the hook_user() method and requires the $op and &$edit parameters. Basically, we are calling to alter the registration in the case 'register', we are going to validate this change and on completion enter a message into the Drupal watchdog.
The validate case is looking for the boolean value to not equal the default value of 0, which is "I disagree" in the #options array for the radial buttons. If the value is not equal or != 1 then the user is shown an error.
Finally, upload all of your changes to your Drupal development site, enable the module and clear the website's cache. Signout of Drupal, navigate to the registration page and do a hard refresh on the page to clear your browser. This is down with a CTRL+F5 in IE or FF.
Conclusion:
Theming and modifying Drupal has two approaches. One for themers and one for developers. This article was targeted towards developers, but in the following days, I will write up another article on modifying the user registration page through one themer's method.