0

I have defined few tokens that are grouped in few separate groups similar to [node:content-type:name] scheme. For example I'd like to have a token like [master:sub_group_1:tokan_A]. So This is how hook_token_info() looks like:

function MY_MODULE_token_info() {
  $tokens = array();
  $types = array();

  $types['master'] = array(
    'name' => 'Master group',
    'description' => 'This is the master group',
  );

  $types['sub_group_1'] = array(
    'name' => 'Sub group 1',
    'description' => 'This is the first sub group',
    // this tells this group that it's parent is 'master' group.
    'needs-data' => 'master',
  );

  $tokens['sub_group_1']['token_A'] = array(
    'name' => 'Token A',
    'description' => 'This token prints letter A',
  );

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

I'll skip MY_MODULE_tokens() as it's irrelevant here, but token_replace() works perfectly. It replaces [master:sub_group_1:token_A] correctly. But the problem is that token module does not list my custom tokens in available tokens section and when I try to use [master:sub_group_1:token_A] in forms that get validated by token module, I get The %element-title is using the following invalid tokens error message.

What is the correct method to define custom tokens and group them to avoid conflicts with tokens defined by other modules?

1 Answer 1

0

It seems that in order to nest those tokens a "dummy" token is needed that matches group name:

function MY_MODULE_token_info() {
  $tokens = array();
  $types = array();

  $types['master'] = array(
    'name' => 'Master group',
    'description' => 'This is the master group',
  );

  // This is needed in order to nest tokens:
  // a dummy token that is the same as nested group/type
  $tokens['master']['sub_group_1'] = array(
    'name' => 'Sub group 1',
    'description' => '1st tokens group',
    'type' => 'sub_group_1',
  );

  $types['sub_group_1'] = array(
    'name' => 'Sub group 1',
    'description' => 'This is the first sub group',
    // this tells this group that it's parent is 'master' group.
    'needs-data' => 'master',
  );

  $tokens['sub_group_1']['token_A'] = array(
    'name' => 'Token A',
    'description' => 'This token prints letter A',
  );

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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