Created
January 5, 2019 15:25
-
-
Save kevinquillen/f2522d8f3dfe37b6a6c95b6f0c99c743 to your computer and use it in GitHub Desktop.
Custom module for Drupal 7 defining a token for a custom field that stores its data in a `year` column (instead of `value`).
This file contains hidden or 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 | |
declare(strict_types = 1); | |
/** | |
* Implements hook_token_info(). | |
*/ | |
function mymodule_token_info() : array { | |
$info['tokens']['node']['custom_year'] = array( | |
'name' => t('Year Field Value'), | |
'description' => t('Value from the year field.') | |
); | |
return $info; | |
} | |
/** | |
* Implements hook_tokens(). | |
*/ | |
function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) : array { | |
$replacements = []; | |
if ($type == 'node' && !empty($data['node']) && array_key_exists('custom_year', $tokens)) { | |
$replacements['[node:custom_year]'] = $data['node']->field_custom_year[LANGUAGE_NONE][0]['year']; | |
} | |
return $replacements; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment