drupal 7: how to create custom node token

Posted by admin on Sat, 11/11/2017 - 07:54

Here is the simple example how to create custom node token in Drupal 7.

Lets say you want to crete a custom node token that will return a first letter of the node title.

So at the first step you need to declare your node token using hook_token_info:

function MYMODULE_token_info() {
  $info['tokens']['node']['title_first_letter'] = array(
    'name' => t('Title first letter'),
    'description' => t('Generates first letter of node title')
  );
  return $info;
}

Then you need to create a processing of that token:

function slideaway_currency_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'title_first_letter':
          $replacements[$original] = substr($node->title, 0, 1); // get first letter of the title here 
          break;
      }
    }
  }
  return $replacements;
}

Don't forget to flush the cache to update token register.
That's it: go to the token list and it should be there!

Tags: 
×

Hey! Don't miss a new cool stuff!

Comments

asd (not verified)

Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks. karamba I’ll probably keep it short and sweet and link to this instead if thats cool. Thanks.

Jasper (not verified)

One edge to online gaming is that you are able to exercise at
no cost, therefore there is no-risk.

my blog ... Octavio

aftab ali (not verified)

Os ensure you have these focuses as a top priority while you are picking a gambling club for you. hopa

Okay I have a f... (not verified)

Okay I have a front page which you need to log in to get in to the web page but when I use the user menu log out button it just takes me to the page I created for front page but with in the site.. help with assignment , I mean it says its going to sign in page but I don't see the log in block and also does anyone knows how to update the triggers html code

asad (not verified)

Regular visits listed here are the easiest method to appreciate your energy, which is why why I am going to the website everyday, searching for new, interesting info. Many, thank you  iq option

emily seo (not verified)

It ought to have a staff that is fit for dealing with requests of an extensive variety of businesses, for example, back, providing food, correspondence, transport, retail, tourism, and website composition. link building company

aftab ali (not verified)

There are numerous responses to that inquiry and one of them is arrive based clubhouse don't give you rewards, online gambling club give you liberal rewards! storspelare

Anonymous (not verified)

Example of the custom toke this is simple and my token has been generated on first try on mine by putting the codes. That token I need to have some purchasing from rushmyessay.com website like notes and some stuff of the essays to concept clearances.

web design and ... (not verified)

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.. web design and development dubai

aftab ali (not verified)

You set the tone and the climate that will encompass you, so kick back, unwind and appreciate the more stunning side of internet gaming. Cherry casino

Add new comment