Skip to content

Instantly share code, notes, and snippets.

@olafgrabienski
Last active April 22, 2017 15:20
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 olafgrabienski/cfc07b395e44daf76968145428b7b733 to your computer and use it in GitHub Desktop.
Save olafgrabienski/cfc07b395e44daf76968145428b7b733 to your computer and use it in GitHub Desktop.
Custom date token for Backdrop CMS event content type URL alias pattern
name = Custom Date Tokens
description = Adds custom tokens for date fields.
package = Custom
version = BACKDROP_VERSION
type = module
backdrop = 1.x
<?php
/**
* @file
* Provides custom tokens for building URL aliases using date fields.
*/
/**
* Implements hook_token_info().
*/
function custom_date_tokens_token_info() {
$info['tokens']['node']['field-date-custom'] = array(
'name' => t('Event Custom Date Format'),
'description' => t('Custom date field value for events, format: Y-M-D.'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function custom_date_tokens_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) {
case 'field-date-custom':
$time = strtotime($node->field_event_date['und'][0]['value']);
$newformat = date('Y-m-d',$time);
$replacements[$original] = $newformat;
break;
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment