18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Building an Admin Interface<br />

This text contains three tokens:<br />

• [site:name]: the site's name as described earlier<br />

• [site:mail]: the administrative e-mail address (this is the same e-mail<br />

address returned by variable_get('site-mail')<br />

• [user:name]: the login name of a specified user<br />

In order to make this work, we have to implement token_replace() in our<br />

hook_mail() implementation as highlighted below:<br />

/**<br />

* Implement hook_mail().<br />

*/<br />

function user_warn_mail($key, &$message, $params) {<br />

switch ($key) {<br />

case 'warn':<br />

$account = $params['account'];<br />

$subject = variable_get('user_warn_e-mail_<br />

subject','Administrative Warning');<br />

$body = variable_get('user_warn_e-mail_text',<br />

USER_WARN_MAIL_TEXT);<br />

if (variable_get('user_warn_bcc', FALSE)) {<br />

$admin_mail = variable_get('site_mail', NULL);<br />

$message['headers']['bcc'] = $admin_mail;<br />

}<br />

}<br />

$message['to'] = $account->mail;<br />

$message['subject'] = $subject;<br />

$message['body'][] = token_replace($body,<br />

array('user' => $account));<br />

break;<br />

}<br />

As you can see, we're now setting the e-mail body to the return value from<br />

token_replace(). This function is pretty simple, it only takes two arguments:<br />

• The text with tokens in place.<br />

• An array of keyed objects to be used in the token replacement process. In this<br />

case, the user object for the recipient of this e-mail as passed in the $params<br />

argument from drupal_mail(). If you need other replacements (like for a<br />

node) you would add additional objects into this array.<br />

[ 148 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!