Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

CHAPTER 6 WORKING WITH USERS

[#type] => user_profile_category [#attributes] => Array

(

[class] => Array

(

[0] => user-member

)

)

[#weight] => 5 [#title] => History [member_for] => Array

(

[#type] => user_profile_item [#title] => Member for [#markup] => 3 days 11 hours

)

)

[user_picture] => Array

(

[#markup] => [#weight] => -10

)

)

Your module may also implement hook_user_view() to manipulate the profile items in the $user- >content array before they are themed. The following is an example of simply removing the blog profile item from the user profile page. The function is named as if it were in the hypothetical hide.module:

/**

* Implements hook_user_view(). */

function hide_user_view($account, $view_mode = ‘full’) { unset($account->content['summary']['blog']);

}

The User Registration Process

By default, user registration on a Drupal site requires nothing more than a username and a valid e-mail address. Modules can add their own fields to the user registration form by implementing a few user hooks. Let’s write a module called legalagree.module that provides a quick way to make your site play well in today’s litigious society.

First, create a folder at sites/all/modules/custom/legalagree, and add the following files (see Listings 6-2 and 6-3) to the legalagree directory. Then, enable the module via Administer -> Site building -> Modules.

122

CHAPTER 6 WORKING WITH USERS

Listing 6-2. legalagree.info

name = Legal Agreement

description = Displays a dubious legal agreement during user registration. package = Pro Drupal Development

core = 7.x

files[] = legalagree.module

Listing 6-3. legalagree.module

<?php

/**

*@file

*Support for dubious legal agreement during user registration.

*/

/**

* Implements hook_form_alter(). */

function legalagree_form_alter(&$form, &$form_state, $form_id) {

//check to see if the form is the user registration or user profile form

//if not then return and don’t do anything

if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) { return;

}

//add a new validate function to the user form to handle the legal agreement $form['#validate'][] = 'legalagree_user_form_validate';

//add a field set to wrap the legal agreement

$form['account']['legal_agreement'] = array( '#type' => 'fieldset',

'#title' => t('Legal agreement')

);

// add the legal agreement radio buttons $form['account']['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'))

);

}

123

CHAPTER 6 WORKING WITH USERS

/**

*Form validation handler for the current password on the user_account_form().

*@see user_account_form()

*/

function legalagree_user_form_validate($form, &$form_state) {

global $user;

// Did user agree?

if ($form_state['input']['decision'] <> 1) {

form_set_error('decision', t('You must agree to the Legal Agreement before registration can be completed.'));

} else {

watchdog('user', t('User %user agreed to legal terms', array('%user' => $user->name)));

}

}

The user hook gets called during the creation of the registration form, during the validation of that form, and after the user record has been inserted into the database. Our brief module will result in a registration form similar to the one shown in Figure 6-2.

Figure 6-2. A modified user registration form

124

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]