3 // Copyright 2008 Thierry GUEGAN http://www.arvoriad.com
7 * Defines zipcodes fields for CCK.
8 * Provide some verifications on the zipcodes
12 * Implements hook_field_info().
14 * Here we indicate that the content module will use its default
15 * handling for the view of this field.
17 * Callbacks can be omitted if default handing is used.
18 * They're included here just so this module can be used
19 * as an example for custom modules that might do things
22 * If your module will provide its own Views tables or arguments,
23 * change CONTENT_CALLBACK_DEFAULT to CONTENT_CALLBACK_CUSTOM.
25 * IMPORTANT! - field and widget names will be truncated to 32 characters in
26 * the database and in internal arrays, like content_fields().
28 function zipcode_field_info() {
30 'fr_zipcode' => array('label' => t('Zipcode - France')),
31 'ca_zipcode' => array('label' => t('Zipcode - Canada')),
32 'us_zipcode' => array('label' => t('Zipcode - US')),
33 'gp_zipcode' => array('label' => t('Zipcode - Guadeloupe')),
34 'cl_zipcode' => array('label' => t('Zipcode - Chile')),
35 'ar_zipcode' => array('label' => t('Zipcode - Argentina')),
36 'br_zipcode' => array('label' => t('Zipcode - Brazil')),
37 'uk_zipcode' => array('label' => t('Zipcode - United Kingdom')),
38 'be_zipcode' => array('label' => t('Zipcode - Belgium')),
39 'nl_zipcode' => array('label' => t('Zipcode - Netherlanc')),
40 'de_zipcode' => array('label' => t('Zipcode - Germany')),
41 'ch_zipcode' => array('label' => t('Zipcode - Switzerland')),
42 'it_zipcode' => array('label' => t('Zipcode - Italy')),
43 'ad_zipcode' => array('label' => t('Zipcode - Andorra')),
44 'hu_zipcode' => array('label' => t('Zipcode - Hungary')),
45 'ee_zipcode' => array('label' => t('Zipcode - Estonia')),
46 'hr_zipcode' => array('label' => t('Zipcode - Croatia')),
47 'by_zipcode' => array('label' => t('Zipcode - Belarussia')),
48 'ru_zipcode' => array('label' => t('Zipcode - Russia')),
49 'cn_zipcode' => array('label' => t('Zipcode - China')),
50 'jp_zipcode' => array('label' => t('Zipcode - Japan')),
51 'au_zipcode' => array('label' => t('Zipcode - Australia')),
52 'nz_zipcode' => array('label' => t('Zipcode - New Zealand')),
53 'dk_zipcode' => array('label' => t('Zipcode - Daenmark')),
54 'se_zipcode' => array('label' => t('Zipcode - Sweden')),
55 'pt_zipcode' => array('label' => t('Zipcode - Portugal')),
56 'cy_zipcode' => array('label' => t('Zipcode - Cyprus')),
57 'dz_zipcode' => array('label' => t('Zipcode - Algeria')),
58 'gw_zipcode' => array('label' => t('Zipcode - Guinea Bissau')),
59 'eg_zipcode' => array('label' => t('Zipcode - Egypt')),
60 'bn_zipcode' => array('label' => t('Zipcode - Brunei')),
61 'bd_zipcode' => array('label' => t('Zipcode - Bangladesh')),
62 'in_zipcode' => array('label' => t('Zipcode - India')),
67 * Implements hook_theme().
69 function zipcode_theme() {
71 'zipcode_textfield' => array(
72 'arguments' => array('element' => NULL
),
74 'zipcode_formatter_default' => array(
75 'arguments' => array('element' => NULL
),
81 * Implements hook_field_settings().
83 * Handle the settings for a field.
86 * The operation to be performed. Possible values:
87 * - "form": Display the field settings form.
88 * - "validate": Check the field settings form for errors.
89 * - "save": Declare which fields to save back to the database.
90 * - "database columns": Declare the columns that content.module should create
91 * and manage on behalf of the field. If the field module wishes to handle
92 * its own database storage, this should be omitted.
93 * - "filters": Declare the Views filters available for the field.
94 * (this is used in CCK's default Views tables definition)
95 * They always apply to the first column listed in the "database columns"
98 * The field on which the operation is to be performed.
100 * This varies depending on the operation.
101 * - "form": an array of form elements to add to
103 * - "validate": no return value. Use form_set_error().
104 * - "save": an array of names of form elements to
105 * be saved in the database.
106 * - "database columns": an array keyed by column name, with arrays of column
107 * information as values. This column information must include "type", the
108 * MySQL data type of the column, and may also include a "sortable" parameter
109 * to indicate to views.module that the column contains ordered information.
110 * TODO: Details of other information that can be passed to the database layer can
111 * be found in the API for the Schema API.
112 * - "filters": an array of 'filters' definitions as expected by views.module
113 * (see Views Documentation).
114 * When providing several filters, it is recommended to use the 'name'
115 * attribute in order to let the user distinguish between them. If no 'name'
116 * is specified for a filter, the key of the filter will be used instead.
118 function zipcode_field_settings($op, $field) {
120 case
'database columns':
121 if ($field['type'] == 'fr_zipcode') {
123 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
126 if ($field['type'] == 'ca_zipcode') {
128 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
131 if ($field['type'] == 'us_zipcode') {
133 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
136 if ($field['type'] == 'gp_zipcode') {
138 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
141 if ($field['type'] == 'cl_zipcode') {
143 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
146 if ($field['type'] == 'ar_zipcode') {
148 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
151 if ($field['type'] == 'br_zipcode') {
153 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
156 if ($field['type'] == 'uk_zipcode') {
158 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
161 if ($field['type'] == 'be_zipcode') {
163 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
166 if ($field['type'] == 'nl_zipcode') {
168 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
171 if ($field['type'] == 'de_zipcode') {
173 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
176 if ($field['type'] == 'ch_zipcode') {
178 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
181 if ($field['type'] == 'it_zipcode') {
183 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
186 if ($field['type'] == 'ad_zipcode') {
188 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
191 if ($field['type'] == 'hu_zipcode') {
193 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
196 if ($field['type'] == 'ee_zipcode') {
198 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
201 if ($field['type'] == 'hr_zipcode') {
203 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
206 if ($field['type'] == 'by_zipcode') {
208 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
211 if ($field['type'] == 'ru_zipcode') {
213 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
216 if ($field['type'] == 'cn_zipcode') {
218 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
221 if ($field['type'] == 'jp_zipcode') {
223 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
226 if ($field['type'] == 'au_zipcode') {
228 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
231 if ($field['type'] == 'nz_zipcode') {
233 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
236 if ($field['type'] == 'dk_zipcode') {
238 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
241 if ($field['type'] == 'se_zipcode') {
243 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
246 if ($field['type'] == 'pt_zipcode') {
248 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
251 if ($field['type'] == 'cy_zipcode') {
253 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
256 if ($field['type'] == 'dz_zipcode') {
258 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
261 if ($field['type'] == 'gw_zipcode') {
263 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
266 if ($field['type'] == 'eg_zipcode') {
268 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
271 if ($field['type'] == 'bn_zipcode') {
273 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
276 if ($field['type'] == 'bd_zipcode') {
278 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
281 if ($field['type'] == 'in_zipcode') {
283 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE
),
291 * Implements hook_field().
293 * Define the behavior of a field type.
296 * What kind of action is being performed. Possible values:
297 * - "load": The node is about to be loaded from the database. This hook
298 * should be used to load the field.
299 * - "validate": The user has just finished editing the node and is
300 * trying to preview or submit it. This hook can be used to check or
301 * even modify the node. Errors should be set with form_set_error().
302 * - "presave": The user has just finished editing the node and the node has
303 * passed validation. This hook can be used to modify the node.
304 * - "insert": The node is being created (inserted in the database).
305 * - "update": The node is being updated.
306 * - "delete": The node is being deleted.
308 * The node the action is being performed on. This argument is passed by
309 * reference for performance only; do not modify it.
311 * The field the action is being performed on.
312 * @param &$node_field
313 * The contents of the field in this node. Changes to this variable will
314 * be saved back to the node object.
316 * This varies depending on the operation.
317 * - The "load" operation should return an object containing extra values
318 * to be merged into the node object.
319 * - The "insert", "update", "delete", "validate", and "presave" operations
320 * have no return value.
322 * In most cases, only "validate" operations is relevant ; the rest
323 * have default implementations in content_field() that usually suffice.
325 function zipcode_field($op, &$node, $field, &$node_field, $teaser, $page) {
327 case
'validate': // corresponds to hook phone_widget validate in zipcode-5.x
328 foreach ($node_field as
$delta => $item) {
329 if ($item['value'] != '') {
330 if ($field['type'] == 'fr_zipcode' && !valid_zipcode('fr', $item['value'])) {
331 form_set_error($field['field_name'], t('"%value" is not a valid French postal code.<br />Postal codes should only contains 4 or 5 numbers', array('%value' => $item['value'])));
333 if ($field['type'] == 'ca_zipcode' && !valid_zipcode('ca', $item['value'])) {
334 form_set_error($field['field_name'], t('"%value" is not a valid Canadian postal code.<br />Postal codes should be like Z5Z 5Z5 or Z5Z5Z5 ...', array('%value' => $item['value'])));
336 if ($field['type'] == 'us_zipcode' && !valid_zipcode('us', $item['value'])) {
337 form_set_error($field['field_name'], t('"%value" is not a valid US zipcode.<br />Zipcodes should be like 99999 or 99999-9999 ...', array('%value' => $item['value'])));
339 if ($field['type'] == 'gp_zipcode' && !valid_zipcode('gp', $item['value'])) {
340 form_set_error($field['field_name'], t('"%value" is not a valid Guadeloupe zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
342 if ($field['type'] == 'cl_zipcode' && !valid_zipcode('cl', $item['value'])) {
343 form_set_error($field['field_name'], t('"%value" is not a valid Chilean zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
345 if ($field['type'] == 'ar_zipcode' && !valid_zipcode('ar', $item['value'])) {
346 form_set_error($field['field_name'], t('"%value" is not a valid Argentinan zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
348 if ($field['type'] == 'br_zipcode' && !valid_zipcode('br', $item['value'])) {
349 form_set_error($field['field_name'], t('"%value" is not a valid Brazilian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
351 if ($field['type'] == 'uk_zipcode' && !valid_zipcode('uk', $item['value'])) {
352 form_set_error($field['field_name'], t('"%value" is not a valid United Kingom postal code.<br />Postal codes should be like AB1 C23 or AB1C23 ...', array('%value' => $item['value'])));
354 if ($field['type'] == 'be_zipcode' && !valid_zipcode('be', $item['value'])) {
355 form_set_error($field['field_name'], t('"%value" is not a valid Belgian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
357 if ($field['type'] == 'nl_zipcode' && !valid_zipcode('nl', $item['value'])) {
358 form_set_error($field['field_name'], t('"%value" is not a valid Dutch zipcode.<br />Zipcodes should contain 4 numbers followed by 2 letter', array('%value' => $item['value'])));
360 if ($field['type'] == 'de_zipcode' && !valid_zipcode('de', $item['value'])) {
361 form_set_error($field['field_name'], t('"%value" is not a valid German zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
363 if ($field['type'] == 'ch_zipcode' && !valid_zipcode('ch', $item['value'])) {
364 form_set_error($field['field_name'], t('"%value" is not a valid Swiss zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
366 if ($field['type'] == 'it_zipcode' && !valid_zipcode('it', $item['value'])) {
367 form_set_error($field['field_name'], t('"%value" is not a valid Italian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
369 if ($field['type'] == 'ad_zipcode' && !valid_zipcode('ad', $item['value'])) {
370 form_set_error($field['field_name'], t('"%value" is not a valid Andorran zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
372 if ($field['type'] == 'hu_zipcode' && !valid_zipcode('hu', $item['value'])) {
373 form_set_error($field['field_name'], t('"%value" is not a valid Hungarian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
375 if ($field['type'] == 'ee_zipcode' && !valid_zipcode('ee', $item['value'])) {
376 form_set_error($field['field_name'], t('"%value" is not a valid Estonian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
378 if ($field['type'] == 'hr_zipcode' && !valid_zipcode('hr', $item['value'])) {
379 form_set_error($field['field_name'], t('"%value" is not a valid Croatian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
381 if ($field['type'] == 'by_zipcode' && !valid_zipcode('by', $item['value'])) {
382 form_set_error($field['field_name'], t('"%value" is not a valid Belarusian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
384 if ($field['type'] == 'ru_zipcode' && !valid_zipcode('ru', $item['value'])) {
385 form_set_error($field['field_name'], t('"%value" is not a valid Russian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
387 if ($field['type'] == 'cn_zipcode' && !valid_zipcode('cn', $item['value'])) {
388 form_set_error($field['field_name'], t('"%value" is not a valid Chinese zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
390 if ($field['type'] == 'jp_zipcode' && !valid_zipcode('jp', $item['value'])) {
391 form_set_error($field['field_name'], t('"%value" is not a valid Japanese zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
393 if ($field['type'] == 'au_zipcode' && !valid_zipcode('au', $item['value'])) {
394 form_set_error($field['field_name'], t('"%value" is not a valid Australian zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
396 if ($field['type'] == 'nz_zipcode' && !valid_zipcode('nz', $item['value'])) {
397 form_set_error($field['field_name'], t('"%value" is not a valid New Zealand zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
399 if ($field['type'] == 'dk_zipcode' && !valid_zipcode('dk', $item['value'])) {
400 form_set_error($field['field_name'], t('"%value" is not a valid Danish zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
402 if ($field['type'] == 'se_zipcode' && !valid_zipcode('se', $item['value'])) {
403 form_set_error($field['field_name'], t('"%value" is not a valid Swedish zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
405 if ($field['type'] == 'pt_zipcode' && !valid_zipcode('pt', $item['value'])) {
406 form_set_error($field['field_name'], t('"%value" is not a valid Portuguese zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
408 if ($field['type'] == 'cy_zipcode' && !valid_zipcode('cy', $item['value'])) {
409 form_set_error($field['field_name'], t('"%value" is not a valid Cyprus zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
411 if ($field['type'] == 'dz_zipcode' && !valid_zipcode('dz', $item['value'])) {
412 form_set_error($field['field_name'], t('"%value" is not a valid Algerian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
414 if ($field['type'] == 'gw_zipcode' && !valid_zipcode('gw', $item['value'])) {
415 form_set_error($field['field_name'], t('"%value" is not a valid Guinea Bissau zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
417 if ($field['type'] == 'eg_zipcode' && !valid_zipcode('eg', $item['value'])) {
418 form_set_error($field['field_name'], t('"%value" is not a valid Egyptian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
420 if ($field['type'] == 'bn_zipcode' && !valid_zipcode('bn', $item['value'])) {
421 form_set_error($field['field_name'], t('"%value" is not a valid Brunei Darusalam zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
423 if ($field['type'] == 'bd_zipcode' && !valid_zipcode('bd', $item['value'])) {
424 form_set_error($field['field_name'], t('"%value" is not a valid Bangladeshi zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
426 if ($field['type'] == 'in_zipcode' && !valid_zipcode('in', $item['value'])) {
427 form_set_error($field['field_name'], t('"%value" is not a valid Indian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
433 case
'presave': // corresponds to hook phone_widget 'process form values' in phone-5.x
434 foreach ($node_field as
$delta => $item) {
435 //format the phone number
436 if ($item['value'] != '') {
437 if ($field['type'] == 'fr_zipcode') {
438 $node_field[$delta]['value'] = format_zipcode('fr', $node_field[$delta]['value'], $field);
440 if ($field['type'] == 'ca_zipcode') {
441 $node_field[$delta]['value'] = format_zipcode('ca', $node_field[$delta]['value'], $field);
443 if ($field['type'] == 'us_zipcode') {
444 $node_field[$delta]['value'] = format_zipcode('us', $node_field[$delta]['value'], $field);
446 if ($field['type'] == 'gp_zipcode') {
447 $node_field[$delta]['value'] = format_zipcode('gp', $node_field[$delta]['value'], $field);
449 if ($field['type'] == 'cl_zipcode') {
450 $node_field[$delta]['value'] = format_zipcode('cl', $node_field[$delta]['value'], $field);
452 if ($field['type'] == 'ar_zipcode') {
453 $node_field[$delta]['value'] = format_zipcode('ar', $node_field[$delta]['value'], $field);
455 if ($field['type'] == 'br_zipcode') {
456 $node_field[$delta]['value'] = format_zipcode('br', $node_field[$delta]['value'], $field);
458 if ($field['type'] == 'uk_zipcode') {
459 $node_field[$delta]['value'] = format_zipcode('uk', $node_field[$delta]['value'], $field);
461 if ($field['type'] == 'be_zipcode') {
462 $node_field[$delta]['value'] = format_zipcode('be', $node_field[$delta]['value'], $field);
464 if ($field['type'] == 'nl_zipcode') {
465 $node_field[$delta]['value'] = format_zipcode('nl', $node_field[$delta]['value'], $field);
467 if ($field['type'] == 'de_zipcode') {
468 $node_field[$delta]['value'] = format_zipcode('de', $node_field[$delta]['value'], $field);
470 if ($field['type'] == 'ch_zipcode') {
471 $node_field[$delta]['value'] = format_zipcode('ch', $node_field[$delta]['value'], $field);
473 if ($field['type'] == 'it_zipcode') {
474 $node_field[$delta]['value'] = format_zipcode('it', $node_field[$delta]['value'], $field);
476 if ($field['type'] == 'ad_zipcode') {
477 $node_field[$delta]['value'] = format_zipcode('ad', $node_field[$delta]['value'], $field);
479 if ($field['type'] == 'hu_zipcode') {
480 $node_field[$delta]['value'] = format_zipcode('hu', $node_field[$delta]['value'], $field);
482 if ($field['type'] == 'ee_zipcode') {
483 $node_field[$delta]['value'] = format_zipcode('ee', $node_field[$delta]['value'], $field);
485 if ($field['type'] == 'hr_zipcode') {
486 $node_field[$delta]['value'] = format_zipcode('hr', $node_field[$delta]['value'], $field);
488 if ($field['type'] == 'by_zipcode') {
489 $node_field[$delta]['value'] = format_zipcode('by', $node_field[$delta]['value'], $field);
491 if ($field['type'] == 'ru_zipcode') {
492 $node_field[$delta]['value'] = format_zipcode('ru', $node_field[$delta]['value'], $field);
494 if ($field['type'] == 'cn_zipcode') {
495 $node_field[$delta]['value'] = format_zipcode('cn', $node_field[$delta]['value'], $field);
497 if ($field['type'] == 'jp_zipcode') {
498 $node_field[$delta]['value'] = format_zipcode('jp', $node_field[$delta]['value'], $field);
500 if ($field['type'] == 'au_zipcode') {
501 $node_field[$delta]['value'] = format_zipcode('au', $node_field[$delta]['value'], $field);
503 if ($field['type'] == 'nz_zipcode') {
504 $node_field[$delta]['value'] = format_zipcode('nz', $node_field[$delta]['value'], $field);
506 if ($field['type'] == 'dk_zipcode') {
507 $node_field[$delta]['value'] = format_zipcode('dk', $node_field[$delta]['value'], $field);
509 if ($field['type'] == 'se_zipcode') {
510 $node_field[$delta]['value'] = format_zipcode('se', $node_field[$delta]['value'], $field);
512 if ($field['type'] == 'pt_zipcode') {
513 $node_field[$delta]['value'] = format_zipcode('pt', $node_field[$delta]['value'], $field);
515 if ($field['type'] == 'cy_zipcode') {
516 $node_field[$delta]['value'] = format_zipcode('cy', $node_field[$delta]['value'], $field);
518 if ($field['type'] == 'dz_zipcode') {
519 $node_field[$delta]['value'] = format_zipcode('dz', $node_field[$delta]['value'], $field);
521 if ($field['type'] == 'gw_zipcode') {
522 $node_field[$delta]['value'] = format_zipcode('gw', $node_field[$delta]['value'], $field);
524 if ($field['type'] == 'eg_zipcode') {
525 $node_field[$delta]['value'] = format_zipcode('eg', $node_field[$delta]['value'], $field);
527 if ($field['type'] == 'bn_zipcode') {
528 $node_field[$delta]['value'] = format_zipcode('bn', $node_field[$delta]['value'], $field);
530 if ($field['type'] == 'bd_zipcode') {
531 $node_field[$delta]['value'] = format_zipcode('bd', $node_field[$delta]['value'], $field);
533 if ($field['type'] == 'in_zipcode') {
534 $node_field[$delta]['value'] = format_zipcode('in', $node_field[$delta]['value'], $field);
543 * Implements hook_field_formatter_info().
545 * The default behavior of formatters is that they will create
546 * a theme for a single field value.
548 * Setting 'multiple values' to CONTENT_HANDLE_FIELD will create
549 * a formatter that will receive all the values of a field so you
550 * can, for instance, plot all the values on a map or in a graph.
552 * The 'view' operation (handled by the Content module) constructs the
553 * $node in a way that you can use drupal_render() to display the
554 * formatted output for an individual field.
556 * i.e. print drupal_render($node->field_foo);
558 * The code now supports both single value formatters, which theme an
559 * individual item value as has been done in previous version of CCK,
560 * and multiple value formatters, which theme all values for the field
561 * in a single theme. The multiple value formatters could be used, for
562 * instance, to plot field values on a single map or display them
563 * in a graph. Single value formatters are the default, multiple value
564 * formatters can be designated as such in formatter_info().
566 * The node array will look like:
568 * 'Single value' formatter:
569 * $node->content['field_foo'] = array(
570 * '#type' => 'content_field',
571 * '#title' => 'label'
572 * '#field_name' => 'field_name',
576 * '#theme' => $theme,
577 * '#field_name' => 'field_name',
578 * '#type_name' => $node->type,
579 * '#formatter' => $formatter_name,
580 * '#item' => $items[0],
583 * '#theme' => $theme,
584 * '#field_name' => 'field_name',
585 * '#type_name' => $node->type,
586 * '#formatter' => $formatter_name,
587 * '#item' => $items[1],
591 * 'Multiple value' formatter:
592 * $node->content['field_foo'] = array(
593 * '#type' => 'content_field',
594 * '#title' => 'label'
595 * '#field_name' => 'field_name',
598 * '#theme' => $theme,
599 * '#field_name' => 'field_name',
600 * '#type_name' => $node->type,
601 * '#formatter' => $formatter_name,
603 * '#item' => $items[0],
606 * '#item' => $items[1],
611 function zipcode_field_formatter_info() {
614 'label' => 'Default',
615 'field types' => array(
650 'multiple values' => CONTENT_HANDLE_CORE
,
656 * Implements hook_field_formatter().
658 * Prepare an individual item for viewing in a browser.
661 * The field the action is being performed on.
663 * An array, keyed by column, of the data stored for this item in this field.
665 * The name of the formatter being used to display the field.
667 * The node object, for context. Will be NULL in some cases.
668 * Warning: when displaying field retrieved by Views, $node will not
669 * be a "full-fledged" node object, but an object containg the data returned
670 * by the Views query (at least nid, vid, changed)
672 * An HTML string containing the formatted item.
674 * In a multiple-value field scenario, this function will be called once per
675 * value currently stored in the field. This function is also used as the handler
676 * for viewing a field in a views.module tabular listing.
678 * It is important that this function at the minimum perform security
679 * transformations such as running check_plain() or check_markup().
681 function zipcode_field_formatter($field, $item, $formatter, $node) {
682 if (!isset($item['value'])) {
685 if ($field['text_processing']) {
686 $text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview
));
689 $text = check_plain($item['value']);
695 * Implements hook_widget_info().
697 * Here we indicate that the content module will handle
698 * the default value and multiple values for these widgets.
700 * Callbacks can be omitted if default handing is used.
701 * They're included here just so this module can be used
702 * as an example for custom modules that might do things
705 * IMPORTANT! - field and widget names will be truncated to 32 characters in
706 * the database and in internal arrays, like content_fields().
708 function zipcode_widget_info() {
710 'zipcode_textfield' => array(
711 'label' => t('Textfield'),
712 'field types' => array(
747 'multiple values' => CONTENT_HANDLE_CORE
,
748 'callbacks' => array(
749 'default value' => CONTENT_CALLBACK_DEFAULT
,
756 * Implements hook_widget_settings().
758 * Handle the parameters for a widget.
761 * The operation to be performed. Possible values:
762 * - "form": Display the widget settings form.
763 * - "validate": Check the widget settings form for errors.
764 * - "save": Declare which pieces of information to save back to the database.
766 * The widget on which the operation is to be performed.
768 * This varies depending on the operation.
769 * - "form": an array of form elements to add to the settings page.
770 * - "validate": no return value. Use form_set_error().
771 * - "save": an array of names of form elements to be saved in the database.
773 function zipcode_widget_settings($op, $widget) {
777 $size = (isset($widget['size']) && is_numeric($widget['size'])) ?
$widget['size'] : 60;
778 $form['input']['size'] = array(
779 '#type' => 'textfield',
780 '#title' => t('Size of textfield'),
781 '#default_value' => $size,
782 '#element_validate' => array('_element_validate_integer_positive'),
791 return array('size');
797 * Implements hook_widget().
799 * Attach a single form element to the form. It will be built out and
800 * validated in the callback(s) listed in hook_elements. We build it
801 * out in the callbacks rather than here in hook_widget so it can be
802 * plugged into any module that can provide it with valid
803 * $field information.
805 * Content module will set the weight, field name and delta values
806 * for each form element. This is a change from earlier CCK versions
807 * where the widget managed its own multiple values.
809 * If there are multiple values for this field, the content module will
810 * call this function as many times as needed.
813 * the entire form array, $form['#node'] holds node information
815 * the form_state, $form_state['values'][$field['field_name']]
816 * holds the field's form values.
820 * array of default values for this field
822 * the order of this item in the array of subelements (0, 1, 2, etc)
825 * the form item for a single element for this field
827 function zipcode_widget(&$form, &$form_state, $field, $items, $delta = 0) {
829 '#type' => $field['widget']['type'],
830 '#default_value' => isset($items[$delta]) ?
$items[$delta] : NULL
,
836 * Implements hook_content_is_empty().
840 * This function tells the content module whether or not to consider
841 * the $item to be empty. This is used by the content module
842 * to remove empty, non-required values before saving them.
844 function zipcode_content_is_empty($item, $field) {
845 if (empty($item['value'])) {
852 * Implements FAPI hook_elements().
854 * Any FAPI callbacks needed for individual widgets can be declared here,
855 * and the element will be passed to those callbacks for processing.
857 * Drupal will automatically theme the element using a theme with
858 * the same name as the hook_elements key.
860 * Autocomplete_path is not used by text_widget but other widgets can use it
861 * (see nodereference and userreference).
863 function zipcode_elements() {
865 'zipcode_textfield' => array(
867 '#columns' => array('value'), '#delta' => 0,
868 '#process' => array('zipcode_textfield_process'),
869 '#autocomplete_path' => FALSE
,
875 * FAPI theme for an individual text elements.
877 * The textfield or textarea is already rendered by the
878 * textfield or textarea themes and the html output
879 * lives in $element['#children']. Override this theme to
880 * make custom changes to the output.
882 * $element['#field_name'] contains the field name
883 * $element['#delta] is the position of this element in the group
885 function theme_zipcode_textfield($element) {
886 return $element['#children'];
890 * Process an individual element.
892 * Build the form element. When creating a form using FAPI #process,
893 * note that $element['#value'] is already set.
895 * The $fields array is in $form['#field_info'][$element['#field_name']].
897 function zipcode_textfield_process($element, $edit, $form_state, $form) {
898 $field = $form['#field_info'][$element['#field_name']];
899 $field_key = $element['#columns'][0];
900 $delta = $element['#delta'];
901 $element[$field_key] = array(
902 '#type' => 'textfield',
903 '#default_value' => isset($element['#value'][$field_key]) ?
$element['#value'][$field_key] : NULL
,
904 '#size' => !empty($field['widget']['size']) ?
$field['widget']['size'] : 60,
905 '#autocomplete_path' => FALSE
,
906 // The following values were set by the content module and need
907 // to be passed down to the nested element.
908 '#title' => $element['#title'],
909 '#description' => $element['#description'],
910 '#required' => $element['#required'],
911 '#field_name' => $element['#field_name'],
912 '#type_name' => $element['#type_name'],
913 '#delta' => $element['#delta'],
914 '#columns' => $element['#columns'],
917 if (!empty($field['max_length'])) {
918 $element[$field_key]['#maxlength'] = $field['max_length'];
920 if (!empty($field['text_processing'])) {
921 $filter_key = $element['#columns'][1];
922 $format = isset($element['#value'][$filter_key]) ?
$element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT
;
923 $parents = array_merge($element['#parents'] , array($filter_key));
924 $element[$filter_key] = filter_form($format, 1, $parents);
927 // Used so that hook_field('validate') knows where to flag an error.
928 $element['_error_element'] = array(
930 '#value' => implode('][', array_merge($element['#parents'], array($field_key))),
937 * Theme function for 'default' text field formatter.
939 function theme_zipcode_formatter_default($element) {
940 return $element['#item']['value'];
944 * Country supported or not by the module ?
946 * @param string $countrycode
947 * @return boolean Returns a boolean containting the answer to the question.
949 function zipcode_supported_countrycode($countrycode) {
950 if ($countrycode == 'fr'
951 || $countrycode == 'ca'
952 || $countrycode == 'us'
953 || $countrycode == 'gp'
954 || $countrycode == 'cl'
955 || $countrycode == 'ar'
956 || $countrycode == 'br'
957 || $countrycode == 'uk'
958 || $countrycode == 'be'
959 || $countrycode == 'nl'
960 || $countrycode == 'de'
961 || $countrycode == 'ch'
962 || $countrycode == 'it'
963 || $countrycode == 'ad'
964 || $countrycode == 'hu'
965 || $countrycode == 'ee'
966 || $countrycode == 'hr'
967 || $countrycode == 'by'
968 || $countrycode == 'ru'
969 || $countrycode == 'cn'
970 || $countrycode == 'jp'
971 || $countrycode == 'au'
972 || $countrycode == 'nz'
973 || $countrycode == 'dk'
974 || $countrycode == 'se'
975 || $countrycode == 'pt'
976 || $countrycode == 'cy'
977 || $countrycode == 'dz'
978 || $countrycode == 'gw'
979 || $countrycode == 'eg'
980 || $countrycode == 'bn'
981 || $countrycode == 'bd'
982 || $countrycode == 'in'
987 //Country not taken into account yet
993 * Verification for zipcodes.
995 * @param string $countrycode
996 * @param string $zipcodestring
997 * @return boolean Returns boolean FALSE if the zipcode is not valid.
999 function valid_zipcode($countrycode, $zipcodestring) {
1001 $countrycode = trim($countrycode);
1002 $zipcodestring = trim($zipcodestring);
1004 if (zipcode_supported_countrycode($countrycode)) {
1005 //drupal_set_message('langue = ' . $countrycode, 'error');
1007 $valid_zipcode_function = 'valid_' .
$countrycode .
'_zipcode';
1008 module_load_include('inc', 'zipcode', 'countries/zipcode.' .
$countrycode);
1010 if (function_exists($valid_zipcode_function)) {
1011 return $valid_zipcode_function($zipcodestring);
1018 //Country not taken into account yet
1024 * Formatting for Zipcode.
1026 * @param string $countrycode
1027 * @param string $zipcodestring
1028 * @return string Returns a string containting the zipcode with some formatting.
1030 function format_zipcode($countrycode, $zipcodestring, $field) {
1032 $countrycode = trim($countrycode);
1033 $zipcodestring = trim($zipcodestring);
1035 if (zipcode_supported_countrycode($countrycode)) {
1036 //drupal_set_message('langue = ' . $countrycode, 'error');
1038 $format_zipcode_function = 'format_' .
$countrycode .
'_zipcode';
1039 module_load_include('inc', 'zipcode', 'countries/zipcode.' .
$countrycode);
1041 if (function_exists($format_zipcode_function)) {
1042 return $format_zipcode_function($zipcodestring, $field);
1049 //Country not taken into account yet
1055 * Implements hook_token_list().
1057 function zipcode_token_list($type = 'all') {
1058 if ($type == 'field' || $type == 'all') {
1059 $tokens['zipcode']['raw'] = t('Raw zipcode numbers');
1060 $tokens['zipcode']['formatted'] = t('Formatted zipcode numbers');
1066 * Implements hook_token_values().
1068 function zipcode_token_values($type, $object = NULL
, $options = array()) {
1069 if ($type == 'field') {
1071 $tokens['raw'] = $item['value'];
1072 $tokens['formatted'] = $item['view'];
1078 * Implements hook_node_import_fields().
1079 * Integrates with the node_import module.
1081 function zipcode_node_import_fields($type) {
1084 foreach (node_import_cck_fields($type, 'zipcode') as
$fieldname => $fieldinfo) {
1085 foreach ($fieldinfo['columns'] as
$colname => $colinfo) {
1086 $cck_fieldname = node_import_cck_name($fieldname, $colname);
1088 $fields[$cck_fieldname] = node_import_cck_field_defaults($fieldinfo);
1092 $fields[$cck_fieldname]['title'] = $fieldinfo['widget']['label'];
1093 $fields[$cck_fieldname]['is_required'] = $fieldinfo['required'];
1097 $fields[$cck_fieldname]['title'] = t('Unsupported:') .
' ' .
$fieldinfo['widget']['label'] .
' - ' .
$colname;