Projet

Général

Profil

Paste
Télécharger (4,27 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / line_item / commerce_line_item.tokens.inc @ 9d13637e

1
<?php
2

    
3
/**
4
 * @file
5
 * Builds placeholder replacement tokens for line item related data.
6
 */
7

    
8

    
9
/**
10
 * Implements hook_token_info().
11
 */
12
function commerce_line_item_token_info() {
13
  $type = array(
14
    'name' => t('Line items'),
15
    'description' => t('Tokens related to individual line items.'),
16
    'needs-data' => 'commerce-line-item',
17
  );
18

    
19
  // Tokens for line items.
20
  $line_item = array();
21

    
22
  $line_item['line-item-id'] = array(
23
    'name' => t('Line item ID'),
24
    'description' => t('The unique numeric ID of the line item.'),
25
  );
26
  $line_item['type'] = array(
27
    'name' => t('Line item type'),
28
    'description' => t('The type of the line item.'),
29
  );
30
  $line_item['type-name'] = array(
31
    'name' => t('Line item type name'),
32
    'description' => t('The human-readable name of the line item type.'),
33
  );
34
  $line_item['line-item-label'] = array(
35
    'name' => t('Line item label'),
36
    'description' => t('The label displayed with the line item.'),
37
  );
38
  $line_item['quantity'] = array(
39
    'name' => t('Quantity'),
40
    'description' => t('The quantity of the line item.'),
41
  );
42

    
43
  // Chained tokens for products.
44
  $line_item['order'] = array(
45
    'name' => t('Order'),
46
    'description' => t('Order associated with the line item'),
47
    'type' => 'commerce-order',
48
  );
49
  $line_item['created'] = array(
50
    'name' => t('Date created'),
51
    'description' => t('The date the line item was created.'),
52
    'type' => 'date',
53
  );
54
  $line_item['changed'] = array(
55
    'name' => t('Date updated'),
56
    'description' => t('The date the line item was last updated.'),
57
    'type' => 'date',
58
  );
59

    
60
  return array(
61
    'types' => array('commerce-line-item' => $type),
62
    'tokens' => array('commerce-line-item' => $line_item),
63
  );
64
}
65

    
66
/**
67
 * Implements hook_tokens().
68
 */
69
function commerce_line_item_tokens($type, $tokens, array $data = array(), array $options = array()) {
70
  $url_options = array('absolute' => TRUE);
71

    
72
  if (isset($options['language'])) {
73
    $url_options['language'] = $options['language'];
74
    $language_code = $options['language']->language;
75
  }
76
  else {
77
    $language_code = NULL;
78
  }
79

    
80
  $sanitize = !empty($options['sanitize']);
81

    
82
  $replacements = array();
83

    
84
  if ($type == 'commerce-line-item' && !empty($data['commerce-line-item'])) {
85
    $line_item = $data['commerce-line-item'];
86

    
87
    foreach ($tokens as $name => $original) {
88
      switch ($name) {
89
        // Simple key values on the line item.
90
        case 'line-item-id':
91
          $replacements[$original] = $line_item->line_item_id;
92
          break;
93

    
94
        case 'type':
95
          $replacements[$original] = $sanitize ? check_plain($line_item->type) : $line_item->type;
96
          break;
97

    
98
        case 'type-name':
99
          $replacements[$original] = $sanitize ? check_plain(commerce_line_item_type_get_name($line_item->type)) : commerce_line_item_type_get_name($line_item->type);
100
          break;
101

    
102
        case 'line-item-label':
103
          $replacements[$original] = $sanitize ? check_plain($line_item->line_item_label) : $line_item->line_item_label;
104
          break;
105

    
106
        case 'quantity':
107
          $replacements[$original] = $sanitize ? check_plain($line_item->quantity) : $line_item->quantity;
108
          break;
109

    
110
        // Default values for the chained tokens handled below.
111
        case 'order':
112
          if ($line_item->order_id) {
113
            $order = commerce_order_load($line_item->order_id);
114
            $replacements[$original] = $sanitize ? check_plain($order->order_number) : $order->order_number;
115
          }
116
          break;
117

    
118
        case 'created':
119
          $replacements[$original] = format_date($line_item->created, 'medium', '', NULL, $language_code);
120
          break;
121

    
122
        case 'changed':
123
          $replacements[$original] = format_date($line_item->changed, 'medium', '', NULL, $language_code);
124
          break;
125
      }
126
    }
127

    
128
    if ($order_tokens = token_find_with_prefix($tokens, 'order')) {
129
      $order = commerce_order_load($line_item->order_id);
130
      $replacements += token_generate('commerce-order', $order_tokens, array('commerce-order' => $order), $options);
131
    }
132

    
133
    foreach (array('created', 'changed') as $date) {
134
      if ($created_tokens = token_find_with_prefix($tokens, $date)) {
135
        $replacements += token_generate('date', $created_tokens, array('date' => $order->{$date}), $options);
136
      }
137
    }
138
  }
139

    
140
  return $replacements;
141
}