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

C H A P T E R 3

■ ■ ■

Hooks, Actions, and Triggers

A common goal when working with Drupal is for something to happen when a certain event takes place. For example, a site administrator may want to receive an e-mail message when a message is posted. Or a user should be blocked if certain words appear in a comment. This chapter describes how to hook into Drupal’s events to have your own code run when those events take place.

Understanding Events and Triggers

Drupal proceeds through a series of events as it goes about its business. These internal events are times when modules are allowed to interact with Drupal’s processing. Table 3-1 shows some of Drupal’s events.

Table 3-1. Examples of Drupal Events

Event

Type

 

 

Creation of a node

Node

Deletion of a node

Node

Viewing of a node

Node

Creation of a user account

User

Updating of a user profile

User

Login

User

Logout

User

 

 

Drupal developers refer to these internal events as hooks because when one of the events occurs, Drupal allows modules to hook into the path of execution at that point. You’ve already met some hooks in previous chapters. Typical module development involves deciding which Drupal event you want to react to, that is, which hooks you want to implement in your module.

33

CHAPTER 3 HOOKS, ACTIONS, AND TRIGGERS

Suppose you have a web site that is just starting out, and you are serving the site from the computer in your basement. Once the site gets popular, you plan to sell it to a huge corporation and get filthy rich. In the meantime, you’d like to be notified each time a user logs in. You decide that when a user logs in, you want the computer to beep. Because your cat is sleeping and would find the beeps annoying, you decide to simulate the beep for the time being with a simple log entry. You quickly write an .info file and place it at sites/all/modules/custom/beep/beep.info:

name = Beep

description = Simulates a system beep. package = Pro Drupal Development

core = 7.x

files[] = beep.module

Then it’s time to write sites/all/modules/custom/beep/beep.module:

<?php

/**

*@file

*Provide a simulated beep.

*/

function beep_beep() { watchdog('beep', 'Beep!');

}

This writes the message “Beep!” to Drupal’s log—good enough for now. Next, it’s time to tell Drupal to beep when a user logs in. We can do that easily by implementing hook_user_login() in our module:

/**

* Implementation of hook_user_login(). */

function beep_user(&$edit, $account) { beep_beep();

}

There—that was easy. How about beeping when new content is added, too? We can do that by implementing hook_node_insert() in our module and catching the insert operation:

/**

* Implementation of hook_node_insert(). */

function beep_node_insert($node) { beep_beep();

}

34

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