0

I am using simple views custom field handler. And I would like to make list of custom tokens for this $data array I have. How could I do that?

class custom_views_handler_field_hostname extends views_handler_field {

  function render($value) {

     $location = get_some_data_from_outside;

     foreach ($location as $key => $value) {
      $data [] =  $key. ': ' . $value;
    }

    if (!is_array($value)) {
      return theme('item_list',
          array(
            'items' => $data,
            'title' => NULL,
            'type' => 'ul'
          ));
    }
  }
  • See hook_token_info() – Clive Feb 13 '15 at 11:29
  • Are you sure I need to use that? And not views token functions? like document_self_token and add_self_token? – Marko Blazekovic Feb 13 '15 at 11:32
0

The first hook, hook_token_info(), is used to declare any custom tokens.

    /**
 * Implements hook_token_info().
 */
function module_name_token_info() {
  $info = array();
  // Define a new token type.
  $info['types']['my_custom_tokens'] = array(
    'name' => t('My custom tokens'),
    'description' => t('A token type for my custom tokens.'),
  );
  // Define any new tokens.
  $info['tokens']['my_custom_tokens']['my_cool_token'] = array(
    'name' => t('My cool token'),
    'description' => t('A token I use to show that I am cool.'),
  );
  return $info;
}

We are creating our own custom token type here too but if you just want to put your token in an already existing type, you can get a list of the existing tokens and types by calling token_get_info().

Now that our new token is good and defined, we just need to give it some content with hook_tokens().

     /**
     * Implements hook_tokens().
     */
    function module_name_tokens($type, $tokens, array $data = array(), array $options = array()) {
      $replacements = array();

      if ($type == 'my_custom_tokens') {
        // Loop through the available tokens.
        foreach ($tokens as $name => $original) {
          // Find our custom tokens by name.
          switch ($name) {
            case 'my_cool_token':
              // Work out the value of our token.
              $value = 'Whatever we want the token to be';
              // Give our token it's value!
              $replacements[$original] = $value;
              break;
          }
        }
      }
   return $replacements;

}

That's it! Our token should now be available and appear in the list of tokens wherever tokens are available.

  • This doesnt seem useful at all? I am not talking about normal tokens but views tokens, also called replacement patterns in UI. – Marko Blazekovic Feb 13 '15 at 12:08
  • This solution works for anywhere you can use tokens, except views. The goal is to get them to show up under replacement patterns in the views_field_config_ui_form – iStryker Mar 13 '15 at 13:53

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.