Monday, May 17, 2010

39. Drupal is flexible at handling events automatically and employing triggers. How do developers make use of these features?

There are really two answers here. At the code level, that's always what Drupal has been about: having your code run when a certain event happens. For example, the following code would send a tweet to my Twitter account every time someone logs in to the Drupal site (it requires the third-party Twitter Module to be installed to do the dirty work).

function mymodulename_user($op, &$edit, &$account) {

if ($op == 'login') {

// Bring twitter-related functions into scope.

module_load_include('inc', 'twitter');

// Use t() for proper localization.
$text = t('@username logged in', array('@username' => $account->name));

// Post to twitter using the twitter module.
twitter_set_status('clouseau', 'secret', $text);

}

}

That's fine if you are a programmer. But what if we took the whole idea of "Send a message to Twitter" and abstracted it? Then we could use a nice user interface to associate the action "Send a message to Twitter" with one of Drupal's common events, such as when a user logs in, or posts content, or creates a new account. That is what the new features in Drupal 6 provide: the user interface for doing such associations between actions and events. A trigger is an event that has been exposed in the user interface.

You can also create your own triggers. Perhaps you want to go the other way: you want actions to happen in Drupal when a new tweet is posted to your Twitter account! Chapter 3 of the book tells you how to make your own triggers.

source: http://ostatic.com/blog/interview-john-vandyk-author-of-pro-drupal-development

No comments:

Post a Comment