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

CHAPTER 6 WORKING WITH USERS

Continued

Component

Description

 

Provided by the user_roles Table

roles

The roles currently assigned to this user.

Provided by the sessions Table

 

sid

The session ID assigned to this user session by PHP.

Ssid

A secure session ID assigned to this user session by PHP.

hostname

The IP address from which the user is viewing the current page.

timestamp

A Unix timestamp representing time at which the user’s browser last

 

received a completed page.

cache

A timestamp used for per-user caching (see includes/cache.inc).

session

Arbitrary, transitory data stored for the duration of the user’s session

 

can be stored here by modules.

 

 

Testing If a User Is Logged In

During a request, the standard way of testing if a user is logged in is to test whether $user->uid is 0. Drupal has a convenience function called user_is_logged_in() for this purpose (there is a corresponding user_is_anonymous() function):

if (user_is_logged_in()) {

$output = t('User is logged in.'); else {

$output = t('User is an anonymous user.');

}

Introduction to user hooks

Implementing user hooks gives your modules a chance to react to the different operations performed on a user account and to modify the $user object. There are several variants of hook_user, each variant performing a specific action (see Table 6-2).

118

CHAPTER 6 WORKING WITH USERS

Table 6-2. hook user Functions

Hook function

Purpose

 

 

hook_username_alter(&$name, $account) hook_user_cancel($edit, $account, $method) hook_user_cancel_methods_alter(&$methods) hook_user_categories()

hook_user_delete($account) hook_user_insert(&$edit, $account, $category) hook_user_load($users)

hook_user_login(&$edit, $account) hook_user_logout($account) hook_user_operations() hook_user_presave(&$edit, $account, $category) hook_user_role_delete($role)

hook_user_role_insert($role)

hook_user_role_update($role)

hook_user_update(&$edit, $account, $category) hook_user_view($account, $viewmode) hook_user_view_alter(&$build)

Alter the username that is displayed for the user. Act on user account cancellations.

Modify an account cancellation method.

Retrieve a list of user setting or profile information changes.

Respond to user deletion.

A user account was created.

Act on user objects when loaded from the database.

The user just logged in.

The user just logged out.

Add mass user operations.

A user account is about to be created or updated.

Inform other modules that a user role has been deleted.

Inform other modules that a user role has been added.

Inform other modules that a user role has been updated.

A user account was updated.

The user’s account information is being displayed.

The user was built; the module may modify the structured content.

119

CHAPTER 6 WORKING WITH USERS

Caution Don’t confuse the $account parameter within many of the user hook functions with the global $user object. The $account parameter is the user object for the account currently being manipulated. The global $user object is the user currently logged in. Often, but not always, they are the same.

Understanding hook_user_view($account, $view_mode)

hook_user_view() is used by modules to add information to user profile pages (e.g., what you see at http://example.com/?q=user/1; see Figure 6-1).

Download from Wow! eBook <www.wowebook.com>

Figure 6-1. The user profile page, with the blog module and the user module implementing hook_user_view() to add additional information

Let’s examine how the blog module added its information to this page using the hook_user_view function:

/**

* Implements hook_user_view(). */

function blog_user_view($account) {

if (user_access('create blog content', $account)) { $account->content['summary']['blog'] = array(

'#type' => 'user_profile_item', '#title' => t('Blog'),

'#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),

120

CHAPTER 6 WORKING WITH USERS

'#attributes' => array('class' => array('blog')),

);

}

}

The view function stashes some information into $user->content. User profile information is organized into categories, with each category representing a page of information about a user. In Figure 6-1, there is just one category, called History. The outer array should be keyed by category name. In the preceding example, the name of the key is summary, which corresponds to the History category (admittedly, it would make more sense to name the key and the category the same thing). The interior array(s) should have a unique textual key (blog in this case) and have #type, #title, #markup, and

#attributes elements. The type user_profile_item points Drupal’s theming layer to modules/user/user- profile-item.tpl.php. By comparing the code snippet with Figure 6-1, you can see how these elements are rendered. Listing 6-1 shows the contents of the $user->content array, which became the page shown in Figure 6-1.

Listing 6-1. The Structure of $user->content

Array

(

[#pre_render] => Array

(

[0] => _field_extra_fields_pre_render

)

[#entity_type] => user [#bundle] => user [#attached] => Array

(

[css] => Array

(

[0] => modules/field/theme/field.css

)

)

[summary] => Array

(

[blog] => Array

(

[#type] => user_profile_item [#title] => Blog

[#markup] => View recent blog entries [#attributes] => Array

(

[class] => Array

(

[0] => blog

)

)

)

121

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