Projet

Général

Profil

Paste
Télécharger (8,89 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / addressfield / addressfield.tokens.inc @ f066bdb5

1
<?php
2

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

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function addressfield_token_info() {
12
  $type = array(
13
    'name' => t('Address field'),
14
    'description' => t('Tokens related to address field values and their components.'),
15
    'needs-data' => 'address-field',
16
    'field' => TRUE,
17
  );
18

    
19
  // Define tokens for the various components of addresses supported through the
20
  // user interface along with two helper tokens for country and administrative
21
  // area to distinguish between names and abbreviations.
22
  $info['country'] = array(
23
    'name' => t('Country name'),
24
    'description' => t('The full name of the country.'),
25
  );
26
  $info['country-code'] = array(
27
    'name' => t('Country code'),
28
    'description' => t('The two letter ISO country code.'),
29
  );
30
  $info['administrative-area'] = array(
31
    'name' => t('Administrative area (i.e. State/Province)'),
32
    'description' => t('The administrative area value, expanded to the full name if applicable.'),
33
  );
34
  $info['administrative-area-raw'] = array(
35
    'name' => t('Administrative area (raw value)'),
36
    'description' => t('The raw administrative area value.'),
37
  );
38
  $info['locality'] = array(
39
    'name' => t('Locality (i.e. City)'),
40
    'description' => t('The locality value.'),
41
  );
42
  $info['postal-code'] = array(
43
    'name' => t('Postal code'),
44
    'description' => t('The postal code value.'),
45
  );
46
  $info['thoroughfare'] = array(
47
    'name' => t('Thoroughfare (i.e. Street address)'),
48
    'description' => t('The thoroughfare value.'),
49
  );
50
  $info['premise'] = array(
51
    'name' => t('Premise (i.e. Street address)'),
52
    'description' => t('The premise value.'),
53
  );
54
  $info['sub_premise'] = array(
55
    'name' => t('Sub Premise (i.e. Suite, Apartment, Floor, Unknown.)'),
56
    'description' => t('The sub premise value.'),
57
  );
58
  $info['organisation'] = array(
59
    'name' => t('Organisation'),
60
    'description' => t('The organisation name value.'),
61
  );
62
  $info['name-line'] = array(
63
    'name' => t('Full name'),
64
    'description' => t('The name line value of the address.'),
65
  );
66
  $info['first-name'] = array(
67
    'name' => t('First name'),
68
    'description' => t('The first name value.'),
69
  );
70
  $info['last-name'] = array(
71
    'name' => t('Last name'),
72
    'description' => t('The last name value.'),
73
  );
74

    
75
  // Add a helper token to format addresses as expected by MailChimp.
76
  $info['format-mailchimp'] = array(
77
    'name' => t('Address formatted for MailChimp'),
78
    'description' => t('The full address formatted for import into MailChimp.'),
79
  );
80

    
81
  return array(
82
    'types' => array('address-field' => $type),
83
    'tokens' => array('address-field' => $info),
84
  );
85
}
86

    
87
/**
88
 * Implements hook_token_info_alter().
89
 */
90
function addressfield_token_info_alter(&$data) {
91
  // Loop over every address field on the site.
92
  foreach (array_filter(field_info_field_map(), 'addressfield_field_map_filter') as $field_name => $field) {
93
    foreach ($data['tokens'] as $group => $token){
94
      if (isset($data['tokens'][$group][$field_name]) && is_array($data['tokens'][$group][$field_name])) {
95
        // Set the token type for the field to use the addressfield child tokens.
96
        $data['tokens'][$group][$field_name]['type'] = 'address-field';
97
      }
98
    }
99
  }
100
}
101

    
102
/**
103
 * Implements hook_tokens().
104
 */
105
function addressfield_tokens($type, $tokens, array $data = array(), array $options = array()) {
106
  if (isset($options['language'])) {
107
    $language_code = $options['language']->language;
108
  }
109
  else {
110
    $language_code = LANGUAGE_NONE;
111
  }
112

    
113
  $sanitize = !empty($options['sanitize']);
114

    
115
  $replacements = array();
116

    
117
  // If we're generating tokens for an address field, extract the address data
118
  // from the field value array and generate the necessary replacements.
119
  if ($type == 'address-field' && !empty($data['address-field'][$language_code]) && is_array($data['address-field'][$language_code])) {
120
    $address = reset($data['address-field'][$language_code]);
121

    
122
    foreach ($tokens as $name => $original) {
123
      switch ($name) {
124
        case 'country':
125
          $countries = _addressfield_country_options_list();
126
          $replacements[$original] = $sanitize ? check_plain($countries[$address['country']]) : $countries[$address['country']];
127
          break;
128

    
129
        case 'country-code':
130
          $replacements[$original] = $sanitize ? check_plain($address['country']) : $address['country'];
131
          break;
132

    
133
        case 'administrative-area':
134
          // If we received format handlers in the data array, generate the form
135
          // for the address field to see if the administrative area should be
136
          // expanded from an abbreviation to a related name.
137
          $administrative_area = $address['administrative_area'];
138

    
139
          if (!empty($data['format_handlers'])) {
140
            $form = addressfield_generate($address, $data['format_handlers'], array('mode' => 'form'));
141

    
142
            if (!empty($form['locality_block']['administrative_area']['#options'][$administrative_area])) {
143
              $administrative_area = $form['locality_block']['administrative_area']['#options'][$administrative_area];
144
            }
145
          }
146

    
147
          $replacements[$original] = $sanitize ? check_plain($administrative_area) : $administrative_area;
148
          break;
149

    
150
        case 'administrative-area-raw':
151
          $replacements[$original] = $sanitize ? check_plain($address['administrative_area']) : $address['administrative_area'];
152
          break;
153

    
154
        case 'locality':
155
          $replacements[$original] = $sanitize ? check_plain($address['locality']) : $address['locality'];
156
          break;
157

    
158
        case 'postal-code':
159
          $replacements[$original] = $sanitize ? check_plain($address['postal_code']) : $address['postal_code'];
160
          break;
161

    
162
        case 'thoroughfare':
163
          $replacements[$original] = $sanitize ? check_plain($address['thoroughfare']) : $address['thoroughfare'];
164
          break;
165

    
166
        case 'premise':
167
          $replacements[$original] = $sanitize ? check_plain($address['premise']) : $address['premise'];
168
          break;
169

    
170
        case 'sub_premise':
171
          $replacements[$original] = $sanitize ? check_plain($address['sub_premise']) : $address['sub_premise'];
172
          break;
173

    
174
        case 'organisation':
175
          $replacements[$original] = $sanitize ? check_plain($address['organisation_name']) : $address['organisation_name'];
176
          break;
177

    
178
        case 'name-line':
179
          $replacements[$original] = $sanitize ? check_plain($address['name_line']) : $address['name_line'];
180
          break;
181

    
182
        case 'first-name':
183
          $replacements[$original] = $sanitize ? check_plain($address['first_name']) : $address['first_name'];
184
          break;
185

    
186
        case 'last-name':
187
          $replacements[$original] = $sanitize ? check_plain($address['last_name']) : $address['last_name'];
188
          break;
189

    
190
        // See: http://kb.mailchimp.com/article/how-do-i-format-my-list-fields-to-import-them
191
        case 'format-mailchimp':
192
          $components = array();
193

    
194
          foreach (array('thoroughfare', 'premise', 'locality', 'administrative_area', 'postal_code', 'country') as $component) {
195
            if (!empty($address[$component])) {
196
              $components[] = $address[$component];
197
            }
198
          }
199

    
200
          $format_mailchimp = implode('  ', $components);
201
          $replacements[$original] = $sanitize ? check_plain($format_mailchimp) : $format_mailchimp;
202
          break;
203
      }
204
    }
205
  }
206

    
207
  // The Token module extends direct token generation by using a generic entity
208
  // token generation process. Since we intend to overwrite the default Token
209
  // module implementation of address field tokens, we use this generic token
210
  // generation process to find and replace address field tokens on relevant
211
  // entities. This ensures our tokens aren't overwritten by the Token module
212
  // and helps us avoid having to do the entity detection ourselves.
213
  if ($type == 'entity') {
214
    // Loop over the address fields defined on the site.
215
    foreach (array_filter(field_info_field_map(), 'addressfield_field_map_filter') as $field_name => $field) {
216
      // If there are any address field tokens in the token list...
217
      if ($addressfield_tokens = token_find_with_prefix($tokens, $field_name)) {
218
        // If the current field is on the matching entity type...
219
        if (!empty($field['bundles'][$data['entity_type']])) {
220
          // Extract the format handlers selected in a representative instance
221
          // settings form for use in formatting tokens.
222
          $instance = field_info_instance($data['entity_type'], $field_name, reset($field['bundles'][$data['entity_type']]));
223
          $format_handlers = $instance['widget']['settings']['format_handlers'];
224
        }
225

    
226
        // Generate the necessary address field tokens for the entity.
227
        $replacements += token_generate('address-field', $addressfield_tokens, array('address-field' => $data['entity']->$field_name, 'format_handlers' => $format_handlers), $options);
228
      }
229
    }
230
  }
231

    
232
  return $replacements;
233
}