Skip to content

Instantly share code, notes, and snippets.

@jenlampton
Last active April 20, 2017 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenlampton/51b9a5b4b973c009c3ea2c25b3145803 to your computer and use it in GitHub Desktop.
Save jenlampton/51b9a5b4b973c009c3ea2c25b3145803 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Provides a token for building URL aliases for NGO articles.
*/
/**
* Implements hook_token_info().
*/
function example_token_token_info() {
$info['tokens']['node']['example-category'] = array(
'name' => t('New Date Format'),
'description' => t('Date field value, formatted nicely'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function example_token_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) {
// Simple key values on the node.
case 'example-category':
$lang = $node->language;
$replace = array();
if (!empty($node->field_date[$lang])) {
$date = $node->field_date[$lang][0]['value'];
$replacements[$original] = format_date($date, 'custom', 'y-m-d');
}
break;
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment