0

I want to create a new token form the selected item in the list box of currently submited node and set it as url alias of the node.

unfortunately, I need keys of select box as token but default tokens do not have this item(it just has labels of listboxes). so I decided to create it's token programmatically.

this is the code but I do not know how to set selected item of list box in $value

function cool_token_token_info() {

  $info['tokens']['node']['key1'] = array(
    'name' => t('My cool token'),
    'description' => t('A token I use to show that I am cool.'),
  );
  return $info;
}



function cool_token_tokens($type, $tokens, array $data = array(), array $options = array(),$node=array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'node') {
    // Loop through the available tokens.
    foreach ($tokens as $name => $original) {
      // Find our custom tokens by name.
      switch ($name) {
        case 'key1':
          // Work out the value of our token.
          if (isset($node)){
                $value= "here I want to have key of the selected item in the listbox of current node";
          }else{
              $value="not working";
          }
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
      }
    }
  }

  return $replacements;
}

I tried

field_get_items('node', $node, 'field_parent')

and many many other ways but it does not print anything

2 Answers 2

2

Check this example code:

/**
* Implements hook_token_info().
*/
function yourmodulename_token_info() {  
  $info['tokens']['node']['city'] = array(
    'name' => t('Venue City'),
    'description' => t('Returns the Venue city (Location)'),
  );
  return

$info;
} 


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

  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    if (isset(

$tokens['city'])) {
      $replacements[$tokens['city']] = pathauto_cleanstring($node->field_location['und'][0]['city']);//here I retrieve the value of the city in the location field.
    }
  }

  return $replacements;
}
1

Try using the Computed field or Token Field to achieve this.

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.