Commit 4efc71cf authored by webchick's avatar webchick

Issue #1754208 by PrabhuG, webflo, savithac, Gábor Hojtsy: Convert hook_mail()...

Issue #1754208 by PrabhuG, webflo, savithac, Gábor Hojtsy: Convert hook_mail() and hook_mail_alter() to langcode.
parent 5254501a
......@@ -26,13 +26,13 @@
*
* Finding out what language to send the e-mail with needs some consideration.
* If you send e-mail to a user, her preferred language should be fine, so
* use user_preferred_language(). If you send email based on form values
* use user_preferred_langcode(). If you send email based on form values
* filled on the page, there are two additional choices if you are not
* sending the e-mail to a user on the site. You can either use the language
* used to generate the page ($language global variable) or the site default
* language. See language_default(). The former is good if sending e-mail to
* the person filling the form, the later is good if you send e-mail to an
* address previously set up (like contact addresses in a contact form).
* used to generate the page or the site default language. See
* language_default(). The former is good if sending e-mail to the person
* filling the form, the later is good if you send e-mail to an address
* previously set up (like contact addresses in a contact form).
*
* Taking care of always using the proper language is even more important
* when sending e-mails in a row to multiple users. Hook_mail() abstracts
......@@ -47,13 +47,13 @@
* foreach ($accounts as $account) {
* $params['account'] = $account;
* // example_mail() will be called based on the first drupal_mail() parameter.
* drupal_mail('example', 'notice', $account->mail, user_preferred_language($account), $params);
* drupal_mail('example', 'notice', $account->mail, user_preferred_langcode($account), $params);
* }
* }
*
* function example_mail($key, &$message, $params) {
* $data['user'] = $params['account'];
* $options['langcode'] = $message['language'];
* $options['langcode'] = $message['langcode'];
* user_mail_tokens($variables, $data, $options);
* switch($key) {
* case 'notice':
......@@ -63,9 +63,8 @@
* $message['send'] = FALSE;
* break;
* }
* $langcode = $message['language']->langcode;
* $message['subject'] = t('Notification from !site', $variables, array('langcode' => $langcode));
* $message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, array('langcode' => $langcode));
* $message['subject'] = t('Notification from !site', $variables, $options);
* $message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, $options);
* break;
* }
* }
......@@ -77,7 +76,7 @@
* @code
* $params = array('current_conditions' => $data);
* $to = 'user@example.com';
* $message = drupal_mail('example', 'notice', $to, $language, $params, FALSE);
* $message = drupal_mail('example', 'notice', $to, $langcode, $params, FALSE);
* // Only add to the spool if sending was not canceled.
* if ($message['send']) {
* example_spool_message($message);
......@@ -98,10 +97,10 @@
* - user@example.com, anotheruser@example.com
* - User <user@example.com>
* - User <user@example.com>, Another User <anotheruser@example.com>
* @param $language
* Language object to use to compose the e-mail.
* @param $langcode
* Language code to use to compose the e-mail.
* @param $params
* Optional parameters to build the e-mail.
* (optional) parameters to build the e-mail.
* @param $from
* Sets From to this value, if given.
* @param $send
......@@ -117,7 +116,7 @@
* written to the watchdog. (Success means nothing more than the message being
* accepted at php-level, which still doesn't guarantee it to be delivered.)
*/
function drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
function drupal_mail($module, $key, $to, $langcode, $params = array(), $from = NULL, $send = TRUE) {
$site_mail = config('system.site')->get('mail');
if (empty($site_mail)) {
$site_mail = ini_get('sendmail_from');
......@@ -131,7 +130,7 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
'key' => $key,
'to' => $to,
'from' => isset($from) ? $from : $default_from,
'language' => $language,
'langcode' => $langcode,
'params' => $params,
'send' => TRUE,
'subject' => '',
......
......@@ -168,7 +168,7 @@ function contact_load($cid) {
* Implements hook_mail().
*/
function contact_mail($key, &$message, $params) {
$language = $message['language'];
$language = language_load($message['langcode']);
$variables = array(
'!site-name' => config('system.site')->get('name'),
'!subject' => $params['subject'],
......
......@@ -168,16 +168,16 @@ function contact_site_form_submit($form, &$form_state) {
$from = $values['sender']->mail;
// Send the e-mail to the recipients using the site default language.
drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from);
drupal_mail('contact', 'page_mail', $to, language_default()->langcode, $values, $from);
// If the user requests it, send a copy using the current language.
if ($values['copy']) {
drupal_mail('contact', 'page_copy', $from, $language_interface, $values, $from);
drupal_mail('contact', 'page_copy', $from, $language_interface->langcode, $values, $from);
}
// Send an auto-reply if necessary using the current language.
if ($values['category']['reply']) {
drupal_mail('contact', 'page_autoreply', $from, $language_interface, $values, $to);
drupal_mail('contact', 'page_autoreply', $from, $language_interface->langcode, $values, $to);
}
flood_register_event('contact', config('contact.settings')->get('flood.interval'));
......@@ -308,11 +308,11 @@ function contact_personal_form_submit($form, &$form_state) {
$from = $values['sender']->mail;
// Send the e-mail in the requested user language.
drupal_mail('contact', 'user_mail', $to, user_preferred_language($values['recipient']), $values, $from);
drupal_mail('contact', 'user_mail', $to, user_preferred_langcode($values['recipient']), $values, $from);
// Send a copy if requested, using current page language.
if ($values['copy']) {
drupal_mail('contact', 'user_copy', $from, $language_interface, $values, $from);
drupal_mail('contact', 'user_copy', $from, $language_interface->langcode, $values, $from);
}
flood_register_event('contact', config('contact.settings')->get('flood.interval'));
......
......@@ -52,7 +52,7 @@ public function testPluggableFramework() {
$language_interface = language(LANGUAGE_TYPE_INTERFACE);
// Use MailTestCase for sending a message.
$message = drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language_interface);
$message = drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language_interface->langcode);
// Assert whether the message was sent through the send function.
$this->assertEqual(self::$sent_message['to'], 'testing@example.com', t('Pluggable mail system is extendable.'));
......@@ -64,13 +64,13 @@ public function testPluggableFramework() {
* @see simpletest_mail_alter()
*/
public function testCancelMessage() {
global $language;
$language_interface = language(LANGUAGE_TYPE_INTERFACE);
// Reset the class variable holding a copy of the last sent message.
self::$sent_message = NULL;
// Send a test message that simpletest_mail_alter should cancel.
$message = drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language);
$message = drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->langcode);
// Assert that the message was not actually sent.
$this->assertNull(self::$sent_message, 'Message was canceled.');
......
......@@ -1945,7 +1945,7 @@ function hook_watchdog(array $log_entry) {
'@message' => strip_tags($log_entry['message']),
));
drupal_mail('emaillog', 'entry', $to, $language_interface, $params);
drupal_mail('emaillog', 'entry', $to, $language_interface->langcode, $params);
}
/**
......
......@@ -3661,14 +3661,14 @@ function system_send_email_action($entity, $context) {
// language.
$recipient_account = user_load_by_mail($recipient);
if ($recipient_account) {
$language = user_preferred_language($recipient_account);
$langcode = user_preferred_langcode($recipient_account);
}
else {
$language = language_default();
$langcode = language_default()->langcode;
}
$params = array('context' => $context);
if (drupal_mail('system', 'action_send_email', $recipient, $language, $params)) {
if (drupal_mail('system', 'action_send_email', $recipient, $langcode, $params)) {
watchdog('action', 'Sent email to %recipient', array('%recipient' => $recipient));
}
else {
......
......@@ -366,15 +366,15 @@ function _update_cron_notify() {
if (!empty($params)) {
$notify_list = $update_config->get('notification.emails');
if (!empty($notify_list)) {
$default_language = language_default();
$default_langcode = language_default()->langcode;
foreach ($notify_list as $target) {
if ($target_user = user_load_by_mail($target)) {
$target_language = user_preferred_language($target_user);
$target_langcode = user_preferred_langcode($target_user);
}
else {
$target_language = $default_language;
$target_langcode = $default_langcode;
}
$message = drupal_mail('update', 'status_notify', $target, $target_language, $params);
$message = drupal_mail('update', 'status_notify', $target, $target_langcode, $params);
// Track when the last mail was successfully sent to avoid sending
// too many e-mails.
if ($message['result']) {
......
......@@ -510,11 +510,10 @@ function _update_get_cached_available_releases() {
* @see _update_message_text()
*/
function update_mail($key, &$message, $params) {
$language = $message['language'];
$langcode = $language->langcode;
$langcode = $message['langcode'];
$message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => config('system.site')->get('name')), array('langcode' => $langcode));
foreach ($params as $msg_type => $msg_reason) {
$message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $language);
$message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $langcode);
}
$message['body'][] = t('See the available updates page for more information:', array(), array('langcode' => $langcode)) . "\n" . url('admin/reports/updates', array('absolute' => TRUE, 'language' => $language));
if (update_manager_access()) {
......@@ -544,14 +543,13 @@ function update_mail($key, &$message, $params) {
* @param $report_link
* (optional) Boolean that controls if a link to the updates report should be
* added. Defaults to FALSE.
* @param $language
* (optional) A language object to use. Defaults to NULL.
* @param $langcode
* (optional) A language code to use. Defaults to NULL.
*
* @return
* The properly translated error message for the given key.
*/
function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $language = NULL) {
$langcode = isset($language) ? $language->langcode : NULL;
function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $langcode = NULL) {
$text = '';
switch ($msg_reason) {
case UPDATE_NOT_SECURE:
......@@ -602,7 +600,12 @@ function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $lan
}
break;
}
if (!empty($langcode)) {
$language = language_load(langcode);
}
else {
$language = NULL;
}
if ($report_link) {
if (update_manager_access()) {
$text .= ' ' . t('See the <a href="@available_updates">available updates</a> page for more information and to install your missing updates.', array('@available_updates' => url('admin/reports/updates/update', array('language' => $language))), array('langcode' => $langcode));
......
......@@ -204,9 +204,9 @@ public function form(array $form, array &$form_state, EntityInterface $account)
$form['#validate'][] = 'user_validate_picture';
$user_preferred_language = $register ? $language_interface : user_preferred_language($account);
$user_preferred_langcode = $register ? $language_interface->langcode : user_preferred_langcode($account);
$user_preferred_admin_language = $register ? $language_interface : user_preferred_language($account, 'admin');
$user_preferred_admin_langcode = $register ? $language_interface->langcode : user_preferred_langcode($account, 'admin');
// Is default the interface language?
include_once DRUPAL_ROOT . '/core/includes/language.inc';
......@@ -223,7 +223,7 @@ public function form(array $form, array &$form_state, EntityInterface $account)
'#type' => 'language_select',
'#title' => t('Site language'),
'#languages' => LANGUAGE_CONFIGURABLE,
'#default_value' => $user_preferred_language->langcode,
'#default_value' => $user_preferred_langcode,
'#description' => $interface_language_is_default ? t("This account's preferred language for e-mails and site presentation.") : t("This account's preferred language for e-mails."),
);
......@@ -231,7 +231,7 @@ public function form(array $form, array &$form_state, EntityInterface $account)
'#type' => 'language_select',
'#title' => t('Administration pages language'),
'#languages' => LANGUAGE_CONFIGURABLE,
'#default_value' => $user_preferred_admin_language->langcode,
'#default_value' => $user_preferred_admin_langcode,
'#access' => user_access('access administration pages', $account),
);
......
......@@ -1812,13 +1812,8 @@ function user_external_login_register($name, $module) {
*/
function user_pass_reset_url($account, $options = array()) {
$timestamp = REQUEST_TIME;
$url_options = array('absolute' => TRUE);
if (isset($options['langcode'])) {
$url_options['language'] = language_load($options['langcode']);
}
else {
$url_options['language'] = user_preferred_language($account);
}
$langcode = isset($options['langcode']) ? $options['langcode'] : user_preferred_langcode($account);
$url_options = array('absolute' => TRUE, 'language' => language_load($langcode));
return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), $url_options);
}
......@@ -1845,13 +1840,8 @@ function user_pass_reset_url($account, $options = array()) {
*/
function user_cancel_url($account, $options = array()) {
$timestamp = REQUEST_TIME;
$url_options = array('absolute' => TRUE);
if (isset($options['langcode'])) {
$url_options['language'] = language_load($options['langcode']);
}
else {
$url_options['language'] = user_preferred_language($account);
}
$langcode = isset($options['langcode']) ? $options['langcode'] : user_preferred_langcode($account);
$url_options = array('absolute' => TRUE, 'language' => language_load($langcode));
return url("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), $url_options);
}
......@@ -2105,10 +2095,10 @@ function user_build_content($account, $view_mode = 'full', $langcode = NULL) {
* Implements hook_mail().
*/
function user_mail($key, &$message, $params) {
$language = $message['language'];
$langcode = $message['langcode'];
$variables = array('user' => $params['account']);
$message['subject'] .= _user_mail_text($key . '.subject', $language, $variables);
$message['body'][] = _user_mail_text($key . '.body', $language, $variables);
$message['subject'] .= _user_mail_text($key . '.subject', $langcode, $variables);
$message['body'][] = _user_mail_text($key . '.body', $langcode, $variables);
}
/**
......@@ -2116,16 +2106,15 @@ function user_mail($key, &$message, $params) {
*
* @param string $key
* The config key that provides the mail text.
* @param object $language
* A language object.
* @param string $langcode
* (optional) A language code to use to generate the e-mail text.
* @param array $variables
* An array of token keys and values.
* (optional) An array of token keys and values.
*
* @return
* A string value containing the text for the user.mail config key.
*/
function _user_mail_text($key, $language = NULL, $variables = array()) {
$langcode = isset($language) ? $language->langcode : NULL;
function _user_mail_text($key, $langcode = NULL, $variables = array()) {
// We do not sanitize the token replacement, since the output of this
// replacement is intended for an e-mail message, not a web browser.
return token_replace(config('user.mail')->get($key), $variables, array('langcode' => $langcode, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
......@@ -2795,10 +2784,10 @@ function theme_user_signature($variables) {
* Default to 'preferred_langcode' property.
* If set 'preferred_$type_langcode' is used.
* @param $default
* Optional default language object to return if the account
* Optional default language code to return if the account
* has no valid language.
*/
function user_preferred_language($account, $type = NULL, $default = NULL) {
function user_preferred_langcode($account, $type = NULL, $default = NULL) {
$language_list = language_list();
if (isset($type)) {
$preferred_langcode = $account->{'preferred_' . $type . '_langcode'};
......@@ -2807,10 +2796,10 @@ function user_preferred_language($account, $type = NULL, $default = NULL) {
$preferred_langcode = $account->preferred_langcode;
}
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode];
return $language_list[$preferred_langcode]->langcode;
}
else {
return $default ? $default : language_default();
return $default ? $default : language_default()->langcode;
}
}
......@@ -2837,20 +2826,21 @@ function user_preferred_language($account, $type = NULL, $default = NULL) {
* @param $account
* The user object of the account being notified. Must contain at
* least the fields 'uid', 'name', and 'mail'.
* @param $language
* Optional language to use for the notification, overriding account language.
* @param $langcode
* Optional language code to use for the notification, overriding account
* language.
*
* @return
* The return value from drupal_mail_system()->mail(), if ends up being
* called.
*/
function _user_mail_notify($op, $account, $language = NULL) {
function _user_mail_notify($op, $account, $langcode = NULL) {
// By default, we always notify except for canceled and blocked.
$notify = config('user.settings')->get('notify.' . $op);
if ($notify || ($op != 'status_canceled' && $op != 'status_blocked')) {
$params['account'] = $account;
$language = $language ? $language : user_preferred_language($account);
$mail = drupal_mail('user', $op, $account->mail, $language, $params);
$langcode = $langcode ? $langcode : user_preferred_langcode($account);
$mail = drupal_mail('user', $op, $account->mail, $langcode, $params);
if ($op == 'register_pending_approval') {
// If a user registered requiring admin approval, notify the admin, too.
// We use the site default language for this.
......@@ -2858,7 +2848,7 @@ function _user_mail_notify($op, $account, $language = NULL) {
if (empty($site_mail)) {
$site_mail = ini_get('sendmail_from');
}
drupal_mail('user', 'register_pending_approval_admin', $site_mail, language_default(), $params);
drupal_mail('user', 'register_pending_approval_admin', $site_mail, language_default()->langcode, $params);
}
}
return empty($mail) ? NULL : $mail['result'];
......
......@@ -81,7 +81,7 @@ function user_pass_submit($form, &$form_state) {
$account = $form_state['values']['account'];
// Mail one time login URL and instructions using current language.
$mail = _user_mail_notify('password_reset', $account, $language_interface);
$mail = _user_mail_notify('password_reset', $account, $language_interface->langcode);
if (!empty($mail)) {
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment