「user:one-time-login-url」トークンがtoken_replaceに置き換えられない


7

hook_mail_alterフックを使用して、ユーザーに手動で電子メールを送信しようとしています。以下は、全体としての私の機能です:

function custom_module_mail_alter(&$message) {
    $email = '[user:name],

A site administrator at [site:name] has created an account for you. You may now log in by clicking this link or copying and pasting it to your browser:

[user:one-time-login-url]

This link can only be used once to log in and will lead you to a page where you can set your password.

After setting your password, you will be able to log in at [site:login-url] in the future using:

username: [user:name]
password: Your password

--  [site:name] team';

    $account = $message['params']['account'];
    $uid = $account->uid;

    $_user = user_load($uid);

    dpm(token_replace($email, array('user'=>$_user)));
}

出力は次のようになります。

Peter,

A site administrator at Website has created an account for you. You may now log in by clicking this link or copying and pasting it to your browser:

[user:one-time-login-url]

This link can only be used once to log in and will lead you to a page where you can set your password.

After setting your password, you will be able to log in at http://localhost/website/user in the future using:

username: Peter
password: Your password

--  Website team

ご覧のとおり、[user:name]、[site:name]、[site:login-url]などは適切に処理されています。処理されない唯一のトークンは[user:one-time-login-url]です。なぜこれが起こっているのですか?

編集:参考までに、システムによって自動的に送信されるウェルカム電子メールでトークン処理されるため、トークンモジュールはアクティブ機能しています... token_replaceを手動で呼び出しても処理されないようです()。


token.moduleを有効にしていますか?そのトークンは、user_token_info()ではなくtoken_token_info()で定義されているようです。
Berdir

はい、トークンモジュールが有効になっていて、最新のBeta7を使用しています...これはかなり奇妙です。
Peter

回答:


11

そのトークンを置き換えるには、token_replace()として呼び出す必要がありますtoken_replace($email, array('user' => $_user), array('callback' => 'user_mail_tokens', 'sanitize' => FALSE))

関数user_mail_tokens()は、ドキュメントでは次のように説明されています。

ユーザーのメールに安全でないトークンを追加するためのトークンコールバック。

この関数は、次のように使用されtoken_replace()の最後の呼び出し_user_mail_text()によって生成された電子メールメッセージに使用することができますいくつかの追加のトークンに設定するuser_mailを()

_user_mail_text()そのコールバックを呼び出すために使用するコードは次のコードです。

// 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($text, $variables, array('language' => $language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE));

トークンモジュールは、Drupal 7ではトークンを置き換える必要はありません。トークンを置き換えるコードは、Drupal 7コアコードの一部です。Drupal 7のTokenモジュールは、Drupalコアモジュールが定義しない追加のトークンを定義します。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.