Last active
April 20, 2017 21:46
-
-
Save jenlampton/51b9a5b4b973c009c3ea2c25b3145803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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