列表 6-2. legalagree.module
<?php
// $Id$
/**
* @file
* Support for dubious legal agreement during user registration.
*/
/**
* Implementation of hook_user().
*/
function legalagree_user($op, &$edit, &$user, $category = NULL) {
switch($op) {
// User is registering.
case 'register':
// Add a fieldset containing radio buttons to the
// user registration form.
$fields['legal_agreement'] = array(
'#type' => 'fieldset',
'#title' => t('Legal Agreement')
);
$fields['legal_agreement']['decision'] = array(
'#type' => 'radios',
'#description' => t('By registering at %site-name, you agree that at any time, we (or our surly, brutish henchmen) may enter your place of residence and smash your belongings with a ball-peen hammer.',array('%site-name' => variable_get('site_name', 'drupal'))),
'#default_value' => 0,
'#options' => array(t('I disagree'), t('I agree'))
);
return $fields;
// Field values for registration are being checked.
case 'validate':
// Make sure the user selected radio button 1 ('I agree').
// The validate op is reused when a user updates information on
// the 'My account' page, so we use isset() to test whether we are
// on the registration page where the decision field is present.
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;
// New user has just been inserted into the database.
case 'insert':
// Record information for future lawsuit.
watchdog('user', t('User %user agreed to legal terms', array('%user' => $user->name)));
break;
}
}
在注册表单创建期间,在表单验证期间,还有在用户记录被插入到数据库中以后,都要调用user钩子。我们这个简单的模块将生成类似于图6-2所示的注册表单。
图6-2 一个修改了的用户注册表单
老葛的Drupal培训班 Think in Drupal
评论
勘误1:
我们这个简单的模块将生成类似于图6-2所示的注册表单。
应为
我们这个简单的模块将生成类似于图6-3所示的注册表单。
官方勘误:
官方勘误:http://www.drupalbook.com/errata2?page=2 Page 123
case 'insert':
// Record information for future lawsuit.
watchdog('user', t('User %user agreed to legal terms',
array('%user' => $user->name)));
break;
case 'insert':
// Record information for future lawsuit.
$name = $edit['name'];
watchdog('user', t('User %user agreed to legal terms',
array('%user' => $name)));
break;
when operation insert happen, the user object is still loaded as anonymous, so $user->name get any information.
To get the new user's name you have to use the $edit variable who content the value submit by the new user in a array. So to get the name you have to use $edit['name'].