Projet

Général

Profil

Paste
Télécharger (2,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.tokens.inc @ 1f142f4f

1
<?php
2

    
3
/**
4
 * @file
5
 * Token module integration.
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function date_token_info() {
12
  // All date types can share the same date value type.
13
  $info['types']['date-field-value'] = array(
14
    'name' => t('Date field values'),
15
    'description' => t('Tokens related to date field values.'),
16
    'needs-data' => 'date-field-value',
17
    'field-value' => TRUE,
18
  );
19
  // Provide two tokens: 'date' (the date or start-date), and 'end-date'.
20
  $info['tokens']['date-field-value']['date'] = array(
21
    'name' => t('Date'),
22
    'description' => t('The date value.'),
23
    'type' => 'date',
24
  );
25
  $info['tokens']['date-field-value']['to-date'] = array(
26
    'name' => t('End Date'),
27
    'description' => t('The End date value.'),
28
    'type' => 'date',
29
  );
30

    
31
  return $info;
32
}
33

    
34
/**
35
 * Implements hook_tokens().
36
 */
37
function date_tokens($type, $tokens, array $data = array(), array $options = array()) {
38
  $replacements = array();
39
  $language_code = isset($options['language']) ? $options['language']->language : NULL;
40

    
41
  if (($type == 'date-field-value') && !empty($data['item'])) {
42
    $item = $data['item'];
43

    
44
    // Create tokens for the field "Date" or "Start date".
45
    if (($date_tokens = token_find_with_prefix($tokens, 'date')) && !empty($item['value'])) {
46
      // Load the Start date and convert to a unix timestamp.
47
      $date = new DateObject($item['value'], $item['timezone_db'], date_type_format($item['date_type']));
48
      if (!empty($date) && $item['timezone_db'] != $item['timezone']) {
49
        date_timezone_set($date, timezone_open($item['timezone']));
50
      }
51
      $timestamp = !empty($date) ? date_format_date($date, 'custom', 'U') : '';
52
      // Generate the token replacements, using the date token type provided
53
      // by system.module.
54
      $replacements += token_generate('date', $date_tokens, array('date' => $timestamp), $options);
55
    }
56

    
57
    // Create tokens for the field "End date".
58
    if (($date_tokens = token_find_with_prefix($tokens, 'end-date')) && !empty($item['value2'])) {
59
      // Load the to date and convert to a unix timestamp.
60
      $date = new DateObject($item['value2'], $item['timezone_db'], date_type_format($item['date_type']));
61
      if (!empty($date) && $item['timezone_db'] != $item['timezone']) {
62
        date_timezone_set($date, timezone_open($item['timezone']));
63
      }
64
      $timestamp = !empty($date) ? date_format_date($date, 'custom', 'U') : '';
65
      // Generate the token replacements, using the date token type provided
66
      // by system.module.
67
      $replacements += token_generate('date', $date_tokens, array('date' => $timestamp), $options);
68
    }
69
  }
70

    
71
  return $replacements;
72
}