Created
February 18, 2016 09:35
-
-
Save delta-9/816ee7e7e3e6f3d2b495 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 | |
/** | |
* Implements hook_tokens(). | |
*/ | |
function cake_tokens($type, $tokens, array $data = array(), array $options = array()) { | |
$sanitize = !empty($options ['sanitize']); | |
$replacements = array(); | |
if ($type == 'cake' && !empty($data['cake'])) { | |
$cake = $data['cake']; | |
foreach ($tokens as $name => $original) { | |
switch ($name) { | |
// Default values for the chained tokens handled below. | |
case 'author': | |
if ($cake->uid) { | |
$account = user_load($cake->uid); | |
$replacements[$original] = $sanitize ? check_plain(format_username($account)) : format_username($account); | |
} | |
break; | |
case 'recipe': | |
if ($cake->recipe_nid) { | |
$node = node_load($cake->recipe_nid); | |
$replacements[$original] = $sanitize ? check_plain($node->title) : $node->title; | |
} | |
break; | |
} | |
} | |
// Replace the user tokens chained below the cake author token. | |
if ($user_tokens = token_find_with_prefix($tokens, 'author')) { | |
$account = user_load($cake->uid); | |
$replacements += token_generate('user', $user_tokens, array('user' => $account), $options); | |
} | |
// Replace the node tokens chained below the cake recipe token. | |
if ($node_tokens = token_find_with_prefix($tokens, 'recipe')) { | |
$node = node_load($cake->recipe_nid); | |
$replacements += token_generate('node', $node_tokens, array('node' => $node), $options); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment