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

CHAPTER 3 HOOKS, ACTIONS, AND TRIGGERS

db_update('users') ->fields(array('status' => 0)) ->condition('uid', $uid) ->execute();

drupal_session_destroy_uid($uid);

watchdog('action', 'Blocked user %name.', array('%name' => $user->name));

}

Actions must be somewhat intelligent, because they do not know much about what is happening when they are called. That is why the best candidates for actions are straightforward, even atomic. The trigger module always passes the current hook and operation along in the context. These values are stored in $context['hook'] and $context['op']. This approach offers a standardized way to provide information to an action.

How Actions Are Stored

Actions are functions that run at a given time. Simple actions do not have configurable parameters. For example, the “Beep” action we created simply beeped. It did not need any other information (though, of course, $object and $context are available if needed). Contrast this action with the advanced action we created. The “Beep multiple times” action needed to know how many times to beep. Other advanced actions, such as the “Send e-mail” action, may need even more information: whom to send the e-mail to, what the subject of the e-mail should be, and so on. These parameters must be stored in the database.

The actions Table

When an instance of an advanced action is created by the administrator, the information that is entered in the configuration form is serialized and saved into the parameters field of the actions table. A record for the simple “Beep” action would look like this:

aid: 2

type: 'system'

callback: 'beep_beep_action'

parameters: (serialized array containing the beeps parameter with its value, i.e., the number of times to beep)

label: Beep three times

Just before an advanced action is executed, the contents of the parameters field are unserialized and included in the $context parameter that is passed to the action. So the number of beeps in our “Beep multiple times” action instance will be available to beep_multiple_.beep_.action() as $context['beeps'].

Action IDs

Notice the difference in the action IDs of the two table records in the previous section. The action ID of the simple action is the actual function name. But obviously we cannot use the function name as an identifier for advanced actions, since multiple instances of the same action are stored. So a numeric action ID (tracked in the actions_aid database table) is used instead.

49

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