date.tokens.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Token module integration.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function date_token_info() {
  10. // All date types can share the same date value type.
  11. $info['types']['date-field-value'] = array(
  12. 'name' => t('Date field values'),
  13. 'description' => t('Tokens related to date field values.'),
  14. 'needs-data' => 'date-field-value',
  15. 'field-value' => TRUE,
  16. );
  17. // Provide two tokens: 'date' (the date or start-date), and 'end-date'.
  18. $info['tokens']['date-field-value']['date'] = array(
  19. 'name' => t('Date'),
  20. 'description' => t('The date value.'),
  21. 'type' => 'date',
  22. );
  23. $info['tokens']['date-field-value']['to-date'] = array(
  24. 'name' => t('End Date'),
  25. 'description' => t('The End date value.'),
  26. 'type' => 'date',
  27. );
  28. return $info;
  29. }
  30. /**
  31. * Implements hook_tokens().
  32. */
  33. function date_tokens($type, $tokens, array $data = array(), array $options = array()) {
  34. $replacements = array();
  35. $language_code = isset($options['language']) ? $options['language']->language : NULL;
  36. if (($type == 'date-field-value') && !empty($data['item'])) {
  37. $item = $data['item'];
  38. // Create tokens for the field "Date" or "Start date".
  39. if (($date_tokens = token_find_with_prefix($tokens, 'date')) && !empty($item['value'])) {
  40. // Load the Start date and convert to a unix timestamp.
  41. $date = new DateObject($item['value'], $item['timezone_db'], date_type_format($item['date_type']));
  42. if (!empty($date) && $item['timezone_db'] != $item['timezone']) {
  43. date_timezone_set($date, timezone_open($item['timezone']));
  44. }
  45. $timestamp = !empty($date) ? date_format_date($date, 'custom', 'U') : '';
  46. // Generate the token replacements, using the date token type provided
  47. // by system.module.
  48. $replacements += token_generate('date', $date_tokens, array('date' => $timestamp), $options);
  49. }
  50. // Create tokens for the field "End date".
  51. if (($date_tokens = token_find_with_prefix($tokens, 'end-date')) && !empty($item['value2'])) {
  52. // Load the to date and convert to a unix timestamp.
  53. $date = new DateObject($item['value2'], $item['timezone_db'], date_type_format($item['date_type']));
  54. if (!empty($date) && $item['timezone_db'] != $item['timezone']) {
  55. date_timezone_set($date, timezone_open($item['timezone']));
  56. }
  57. $timestamp = !empty($date) ? date_format_date($date, 'custom', 'U') : '';
  58. // Generate the token replacements, using the date token type provided
  59. // by system.module.
  60. $replacements += token_generate('date', $date_tokens, array('date' => $timestamp), $options);
  61. }
  62. }
  63. return $replacements;
  64. }