0

I am using simplenews to send out a newsletter. I have a custom module. It has a table that relates a user code to the user's email.

email_address                  user_code
bob.loblaw@lawblogs.com        37XylT12

In the simplenews email I will send, the idea is that each user will receive a link with their usercode in it (i.e., http://www.domain.com/?q=go&c=37XylT12).

I've been attempting to create a custom token to place in the text of the newsletter and replaced with my custom link prior to send. However, I have two issues: 1) I cannot find an example of a token that includes a db_select query to return data and 2) I cannot find a way to access the simplenews email token ([simplenews-subscriber:mail]) in my custom module -- I need this value to lookup the code.

If someone could point me in the right direction, I would appreciate it. KMT

1

Accessing the database to set a token is not different from accessing the database in other cases.

function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);

  $replacements = array();

  if ($type == 'YOUR TOKEN TYPE') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'YOUR TOKEN':
          $replacements[$original] = /* CODE TO QUERY THE DATABASE */;
          break;
      }
    }
  }

  return $replacements;
}

As for accessing the value of a token defined from a module, when you know the token you are looking for, you can call token_replace() passing the token for which you want to know the value as first parameter. Keep in mind that your implementation of hook_tokens() is going to be called again; be careful of not causing a loop.

|improve this answer|||||
  • I've completed the database component and it is working with string values I type in, however, I'm struggling with the token_replace() function. The token I'm trying to replace is below -- $text = token_replace('[simplenews-subscriber:mail]'); If I return $text to the page, it is correct. However, if i try to use the value in php, it is [simplenews-subscriber:mail]. Please advise. – KMT333 Mar 19 '13 at 16:57
  • I am not sure I understand what you mean. In your code, you need to use $text as value of the token. – kiamlaluno Mar 21 '13 at 12:57
  • It was an issue I ran into calling a token from another module. In the module, the variable $subscriber was set within the module. The token_replace() call needed me to submit the variable of $subscriber, but I could not. I ended up adding a custom token to the module in a separate, custom tokens.inc file. It's not perfect, but it works. Thanks for your help! – KMT333 Mar 21 '13 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.