Skip to content

Instantly share code, notes, and snippets.

@kevinquillen
Created January 5, 2019 15:25
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 kevinquillen/f2522d8f3dfe37b6a6c95b6f0c99c743 to your computer and use it in GitHub Desktop.
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`).
<?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