10

Is it possible to send parameters along with tokens?

I have a module that defines the mod1_info and mod1_tokens. If I enter %token:mod1:test on a page, it is replaced with <a href='test.html'></a>.

What I actually want is to enter %token:mod1:test[variable] and get <a href='test.html'>variable</a>.

Is this possible?

Behind the scenes, the test.html is being built with a few dynamic parameters so it actually looks similar to test.html?param1=foo&param2=bar. What I need is a way for someone editing the site to insert a token on any page, and be able to specify what appears as the link (an image, some text, or both).

1
  • 1
    Unfortunately, I don't believe this is possible. On the other hand, I don't quite understand the use case, as I have never myself needed this. Perhaps if you expand on what you want, a different suggestion can be suggested. – Letharion Apr 23 '12 at 15:01
8

Using Drupal 7, modules can easily handle tokens similar to [node:author:name], where the part of the token after node:author is referring to a user token. Differently from the Token module for Drupal 6, the code that in Drupal 7 handles the tokens allows the creation of dynamic tokens. This is because on Drupal 7, modules implementing tokens can replace any token, not just the tokens they defined. The normal loop used in hook_tokens() is the following one.

foreach ($tokens as $name => $original) {
  // Check the value of $name, and generate the replacement that is assigned to
  // $replacements[$original].
}

Using the following code, you could get what you are trying to achieve. Tokens have a specific format, which is different from the one you are describing, though. The code has not been tested.

function mymodule_token_info() {
  $type = array(
    'name' => t('Anchors'), 
    'description' => t('Your description'), 
  );

  return array(
    'types' => array('anchor' => $type), 
  );
}

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

  if ($type == 'anchor') {
    foreach ($tokens as $name => $original) {
      list($file, $title) = explode(':', $name);
      if (!empty($title) && !empty($_GET[$title])) {
        $title = $_GET[$title];
      }
      else {
        $title = '';
      }

      $replacements[$original] = "<a href='$file.html'>$title</a>";
    }
  }

  return $replacements;
}

The replacement for the tokens is so specific that I don't think you will find a module that does that. Your only possibility is a custom module you write.

0

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.