Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom module for Drupal 7 defining a token for a custom field that stores its data in a `year` column (instead of `value`).
<?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