Subdomain Posts
None | 3 min ago
None | 35 min ago
None | 2 hours ago
None | 7 hours ago
None | 7 hours ago
None | 14 hours ago
None | 14 hours ago
PHP | 1 day ago
PHP | 1 day ago
Objective C | 1 day ago
Recent Posts
C++ | 4 sec ago
Bash | 8 sec ago
None | 33 sec ago
None | 39 sec ago
None | 42 sec ago
HTML | 1 min ago
T-SQL | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Palocz Pal on the 7th of Feb 2010 12:06:48 PM Download | Raw | Embed | Report
  1. <?php
  2. // $Id: zipcode.module,v 1.15 2009/08/26 16:32:02 thierrygd Exp $
  3.  
  4. // Copyright 2008 Thierry GUEGAN http://www.arvoriad.com
  5.  
  6. /**
  7.  * @file
  8.  * Defines zipcodes fields for CCK.
  9.  * Provide some verifications on the zipcodes
  10.  */
  11.  
  12. /**
  13.  * Implementation of hook_field_info().
  14.  * Implementation of hook_field_info().
  15.  *
  16.  * Here we indicate that the content module will use its default
  17.  * handling for the view of this field.
  18.  *
  19.  * Callbacks can be omitted if default handing is used.
  20.  * They're included here just so this module can be used
  21.  * as an example for custom modules that might do things
  22.  * differently.
  23.  *
  24.  * If your module will provide its own Views tables or arguments,
  25.  * change CONTENT_CALLBACK_DEFAULT to CONTENT_CALLBACK_CUSTOM.
  26.  *
  27.  * IMPORTANT! - field and widget names will be truncated to 32 characters in
  28.  * the database and in internal arrays, like content_fields().
  29.  */
  30. function zipcode_field_info() {
  31.   return array(
  32.     'fr_zipcode' => array('label' => t('French postal codes')),
  33.     'ca_zipcode' => array('label' => t('Canadian postal codes')),
  34.     'us_zipcode' => array('label' => t('US zipcodes')),
  35.     'uk_zipcode' => array('label' => t('United Kingdom postal codes')),    
  36.     'nl_zipcode' => array('label' => t('Dutch zipcodes')),
  37.     'hu_zipcode' => array('label' => t('Hungarian zipcodes')),    
  38.     'au_zipcode' => array('label' => t('Australian zipcodes')),    
  39.     'hu_zipcode' => array('label' => t('Hungarian zipcodes')),    
  40.     'dk_zipcode' => array('label' => t('Danish zipcodes')),
  41.     'pt_zipcode' => array('label' => t('Portuguese zipcodes')),    
  42.    );
  43. }
  44.  
  45. /**
  46.  * Implementation of hook_theme().
  47.  */
  48. function zipcode_theme() {
  49.   return array(
  50.     'zipcode_textfield' => array(
  51.       'arguments' => array('element' => NULL),
  52.     ),
  53.     'zipcode_formatter_default' => array(
  54.       'arguments' => array('element' => NULL),
  55.     ),
  56.   );
  57. }
  58.  
  59. /**
  60.  * Implementation of hook_field_settings().
  61.  *
  62.  * Handle the settings for a field.
  63.  *
  64.  * @param $op
  65.  *   The operation to be performed. Possible values:
  66.  *   - "form": Display the field settings form.
  67.  *   - "validate": Check the field settings form for errors.
  68.  *   - "save": Declare which fields to save back to the database.
  69.  *   - "database columns": Declare the columns that content.module should create
  70.  *     and manage on behalf of the field. If the field module wishes to handle
  71.  *     its own database storage, this should be omitted.
  72.  *   - "filters": Declare the Views filters available for the field.
  73.  *     (this is used in CCK's default Views tables definition)
  74.  *     They always apply to the first column listed in the "database columns"
  75.  *     array.
  76.  * @param $field
  77.  *   The field on which the operation is to be performed.
  78.  * @return
  79.  *   This varies depending on the operation.
  80.  *   - "form": an array of form elements to add to
  81.  *     the settings page.
  82.  *   - "validate": no return value. Use form_set_error().
  83.  *   - "save": an array of names of form elements to
  84.  *     be saved in the database.
  85.  *   - "database columns": an array keyed by column name, with arrays of column
  86.  *     information as values. This column information must include "type", the
  87.  *     MySQL data type of the column, and may also include a "sortable" parameter
  88.  *     to indicate to views.module that the column contains ordered information.
  89.  *     TODO: Details of other information that can be passed to the database layer can
  90.  *     be found in the API for the Schema API.
  91.  *   - "filters": an array of 'filters' definitions as expected by views.module
  92.  *     (see Views Documentation).
  93.  *     When providing several filters, it is recommended to use the 'name'
  94.  *     attribute in order to let the user distinguish between them. If no 'name'
  95.  *     is specified for a filter, the key of the filter will be used instead.  
  96.  */
  97. function zipcode_field_settings($op, $field) {
  98.   switch ($op) {      
  99.     case 'database columns':
  100.       if ($field['type'] == 'fr_zipcode') {
  101.         $columns = array(
  102.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  103.         );
  104.       }
  105.       if ($field['type'] == 'ca_zipcode') {
  106.          $columns = array(
  107.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  108.           );
  109.       }
  110.       if ($field['type'] == 'us_zipcode') {
  111.          $columns = array(
  112.                  'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  113.           );
  114.       }
  115.       if ($field['type'] == 'uk_zipcode') {
  116.          $columns = array(
  117.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  118.           );
  119.       }    
  120.       if ($field['type'] == 'nl_zipcode') {
  121.         $columns = array(
  122.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  123.         );
  124.       }
  125.       if ($field['type'] == 'hu_zipcode') {
  126.         $columns = array(
  127.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  128.         );
  129.       }  
  130.       if ($field['type'] == 'au_zipcode') {
  131.         $columns = array(
  132.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  133.         );
  134.       }  
  135.       if ($field['type'] == 'dk_zipcode') {
  136.         $columns = array(
  137.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  138.         );
  139.       }  
  140.       if ($field['type'] == 'pt_zipcode') {
  141.         $columns = array(
  142.                 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  143.         );
  144.       }        
  145.       return $columns;
  146.   }
  147. }
  148.  
  149. /**
  150.  * Implementation of hook_field().
  151.  *
  152.  * Define the behavior of a field type.
  153.  *
  154.  * @param $op
  155.  *   What kind of action is being performed. Possible values:
  156.  *   - "load": The node is about to be loaded from the database. This hook
  157.  *     should be used to load the field.
  158.  *   - "validate": The user has just finished editing the node and is
  159.  *     trying to preview or submit it. This hook can be used to check or
  160.  *     even modify the node. Errors should be set with form_set_error().
  161.  *   - "presave": The user has just finished editing the node and the node has
  162.  *     passed validation. This hook can be used to modify the node.
  163.  *   - "insert": The node is being created (inserted in the database).
  164.  *   - "update": The node is being updated.
  165.  *   - "delete": The node is being deleted.
  166.  * @param &$node
  167.  *   The node the action is being performed on. This argument is passed by
  168.  *   reference for performance only; do not modify it.
  169.  * @param $field
  170.  *   The field the action is being performed on.
  171.  * @param &$node_field
  172.  *   The contents of the field in this node. Changes to this variable will
  173.  *   be saved back to the node object.
  174.  * @return
  175.  *   This varies depending on the operation.
  176.  *   - The "load" operation should return an object containing extra values
  177.  *     to be merged into the node object.
  178.  *   - The "insert", "update", "delete", "validate", and "presave" operations
  179.  *     have no return value.
  180.  *
  181.  * In most cases, only "validate" operations is relevant ; the rest
  182.  * have default implementations in content_field() that usually suffice.  
  183.  */
  184. function zipcode_field($op, &$node, $field, &$node_field, $teaser, $page) {
  185.   switch ($op) {
  186.     case 'validate': // corresponds to hook phone_widget validate in zipcode-5.x
  187.       foreach ($node_field as $delta => $item) {
  188.         if ($item['value'] != '') {
  189.           if ($field['type'] == 'fr_zipcode' && !valid_zipcode('fr', $item['value'])) {
  190.             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'])));
  191.           }
  192.           if ($field['type'] == 'ca_zipcode' && !valid_zipcode('ca', $item['value'])) {
  193.             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'])));
  194.           }
  195.           if ($field['type'] == 'us_zipcode' && !valid_zipcode('us', $item['value'])) {
  196.             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'])));
  197.           }
  198.           if ($field['type'] == 'uk_zipcode' && !valid_zipcode('uk', $item['value'])) {
  199.             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'])));
  200.           }              
  201.           if ($field['type'] == 'nl_zipcode' && !valid_zipcode('nl', $item['value'])) {
  202.             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'])));
  203.           }
  204.           if ($field['type'] == 'hu_zipcode' && !valid_zipcode('hu', $item['value'])) {
  205.             form_set_error($field['field_name'], t('"%value" is not a valid Hungarian zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value'])));
  206.           }        
  207.           if ($field['type'] == 'au_zipcode' && !valid_zipcode('au', $item['value'])) {
  208.             form_set_error($field['field_name'], t('"%value" is not a valid Australian zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value'])));
  209.           }        
  210.           if ($field['type'] == 'dk_zipcode' && !valid_zipcode('dk', $item['value'])) {
  211.             form_set_error($field['field_name'], t('"%value" is not a valid Danish zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value'])));
  212.           }
  213.           if ($field['type'] == 'pt_zipcode' &&  !valid_zipcode('pt', $item['value'])) {
  214.             form_set_error($field['field_name'], t('"%value" is not a valid Portuguese zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value'])));
  215.           }  
  216.         }
  217.       }
  218.       break;
  219.  
  220.     case 'presave': // corresponds to hook phone_widget 'process form values' in phone-5.x
  221.       foreach ($node_field as $delta => $item) {
  222.         //format the phone number
  223.         if ($item['value'] != '') {
  224.           if ($field['type'] == 'fr_zipcode') {
  225.             $node_field[$delta]['value'] = format_zipcode('fr', $node_field[$delta]['value'], $field);
  226.           }
  227.           if ($field['type'] == 'ca_zipcode') {
  228.             $node_field[$delta]['value'] = format_zipcode('ca', $node_field[$delta]['value'], $field);
  229.           }
  230.           if ($field['type'] == 'us_zipcode') {
  231.             $node_field[$delta]['value'] = format_zipcode('us', $node_field[$delta]['value'], $field);
  232.           }          
  233.           if ($field['type'] == 'uk_zipcode') {
  234.             $node_field[$delta]['value'] = format_zipcode('uk', $node_field[$delta]['value'], $field);
  235.           }
  236.           if ($field['type'] == 'nl_zipcode') {
  237.             $node_field[$delta]['value'] = format_zipcode('nl', $node_field[$delta]['value'], $field);
  238.           }          
  239.           if ($field['type'] == 'hu_zipcode') {
  240.             $node_field[$delta]['value'] = format_zipcode('hu', $node_field[$delta]['value'], $field);
  241.           }
  242.           if ($field['type'] == 'au_zipcode') {
  243.             $node_field[$delta]['value'] = format_zipcode('au', $node_field[$delta]['value'], $field);
  244.           }
  245.           if ($field['type'] == 'dk_zipcode') {
  246.             $node_field[$delta]['value'] = format_zipcode('dk', $node_field[$delta]['value'], $field);
  247.           }
  248.           if ($field['type'] == 'pt_zipcode') {
  249.             $node_field[$delta]['value'] = format_zipcode('pt', $node_field[$delta]['value'], $field);
  250.           }
  251.         }
  252.       }
  253.       break;
  254.   }
  255. }
  256.  
  257. /**
  258.  * Implementation of hook_field_view_item().
  259.  *
  260.  */
  261. /*
  262. function zipcode_field_view_item($field, $node_field_item) {
  263.     $zipcode =  check_plain($node_field_item['value']);
  264.     return $zipcode;
  265. }
  266. */
  267.  
  268. /**
  269.  *Implementation of hook_field_formatter_info().
  270.  * Implementation of hook_field_formatter_info().
  271.  *
  272.  * The default behavior of formatters is that they will create
  273.  * a theme for a single field value.
  274.  *
  275.  * Setting 'multiple values' to CONTENT_HANDLE_FIELD will create
  276.  * a formatter that will receive all the values of a field so you
  277.  * can, for instance, plot all the values on a map or in a graph.
  278.  *
  279.  * The 'view' operation (handled by the Content module) constructs the
  280.  * $node in a way that you can use drupal_render() to display the
  281.  * formatted output for an individual field.
  282.  *
  283.  * i.e. print drupal_render($node->field_foo);
  284.  *
  285.  * The code now supports both single value formatters, which theme an
  286.  * individual item value as has been done in previous version of CCK,
  287.  * and multiple value formatters, which theme all values for the field
  288.  * in a single theme. The multiple value formatters could be used, for
  289.  * instance, to plot field values on a single map or display them
  290.  * in a graph. Single value formatters are the default, multiple value
  291.  * formatters can be designated as such in formatter_info().
  292.  *
  293.  * The node array will look like:
  294.  *
  295.  *  'Single value' formatter:
  296.  *   $node->content['field_foo'] = array(
  297.  *     '#type' => 'content_field',
  298.  *     '#title' => 'label'
  299.  *     '#field_name' => 'field_name',
  300.  *     '#node' => $node,
  301.  *     'items' =>
  302.  *       0 => array(
  303.  *         '#theme' => $theme,
  304.  *         '#field_name' => 'field_name',
  305.  *         '#type_name' => $node->type,
  306.  *         '#formatter' => $formatter_name,
  307.  *         '#item' => $items[0],
  308.  *       ),
  309.  *       1 => array(
  310.  *         '#theme' => $theme,
  311.  *         '#field_name' => 'field_name',
  312.  *         '#type_name' => $node->type,
  313.  *         '#formatter' => $formatter_name,
  314.  *         '#item' => $items[1],
  315.  *       ),
  316.  *     ),
  317.  *   );
  318.  *  'Multiple value' formatter:
  319.  *   $node->content['field_foo'] = array(
  320.  *     '#type' => 'content_field',
  321.  *     '#title' => 'label'
  322.  *     '#field_name' => 'field_name',
  323.  *     '#node' => $node,
  324.  *     'items' => array(
  325.  *       '#theme' => $theme,
  326.  *       '#field_name' => 'field_name',
  327.  *       '#type_name' => $node->type,
  328.  *       '#formatter' => $formatter_name,
  329.  *       0 => array(
  330.  *         '#item' => $items[0],
  331.  *       ),
  332.  *       1 => array(
  333.  *         '#item' => $items[1],
  334.  *       ),
  335.  *     ),  
  336.  *   );
  337.  */
  338. function zipcode_field_formatter_info() {
  339.   return array(
  340.     'default' => array(
  341.       'label' => 'Default',
  342.       'field types' => array('fr_zipcode',
  343.                         'ca_zipcode',
  344.                         'us_zipcode',
  345.                         'uk_zipcode',
  346.                         'nl_zipcode',
  347.                         'hu_zipcode',
  348.                         'au_zipcode',
  349.                         'dk_zipcode',
  350.                         'pt_zipcode'                           
  351.                         ),
  352.       'multiple values' => CONTENT_HANDLE_CORE,                                
  353.     ),
  354.   );
  355.  }
  356.  
  357. /**
  358.  *Implementation of hook_field_formatter().
  359.  *
  360.  * Prepare an individual item for viewing in a browser.
  361.  *
  362.  * @param $field
  363.  *   The field the action is being performed on.
  364.  * @param $item
  365.  *   An array, keyed by column, of the data stored for this item in this field.
  366.  * @param $formatter
  367.  *   The name of the formatter being used to display the field.
  368.  * @param $node
  369.  *   The node object, for context. Will be NULL in some cases.
  370.  *   Warning: when displaying field retrieved by Views, $node will not
  371.  *   be a "full-fledged" node object, but an object containg the data returned
  372.  *   by the Views query (at least nid, vid, changed)
  373.  * @return
  374.  *   An HTML string containing the formatted item.
  375.  *
  376.  * In a multiple-value field scenario, this function will be called once per
  377.  * value currently stored in the field. This function is also used as the handler
  378.  * for viewing a field in a views.module tabular listing.
  379.  *
  380.  * It is important that this function at the minimum perform security
  381.  * transformations such as running check_plain() or check_markup().
  382.  */  
  383. function zipcode_field_formatter($field, $item, $formatter, $node) {
  384.   if (!isset($item['value'])) {
  385.     return '';
  386.   }
  387.   if ($field['text_processing']) {
  388.     $text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview));
  389.   }
  390.   else {
  391.     $text = check_plain($item['value']);
  392.   }
  393.   return $text;
  394. }
  395.  
  396. /**
  397.  * Implementation of hook_widget_info().
  398.  *
  399.  * Here we indicate that the content module will handle
  400.  * the default value and multiple values for these widgets.
  401.  *
  402.  * Callbacks can be omitted if default handing is used.
  403.  * They're included here just so this module can be used
  404.  * as an example for custom modules that might do things
  405.  * differently.
  406.  *
  407.  * IMPORTANT! - field and widget names will be truncated to 32 characters in
  408.  * the database and in internal arrays, like content_fields().  
  409.  */
  410. function zipcode_widget_info() {
  411.   return array(
  412.     'zipcode_textfield' => array(
  413.       'label' => t('Textfield'),
  414.       'field types' => array('fr_zipcode',
  415.                         'ca_zipcode',
  416.                         'us_zipcode',
  417.                         'uk_zipcode',
  418.                         'nl_zipcode',
  419.                         'hu_zipcode',
  420.                         'au_zipcode',
  421.                         'dk_zipcode',
  422.                         'pt_zipcode'                           
  423.                         ),
  424.       'multiple values' => CONTENT_HANDLE_CORE,
  425.       'callbacks' => array(
  426.         'default value' => CONTENT_CALLBACK_DEFAULT,
  427.       ),                               
  428.     ),
  429.   );
  430. }
  431.  
  432. /**
  433.  * Implementation of hook_widget_settings().
  434.  *
  435.  * Handle the parameters for a widget.
  436.  *
  437.  * @param $op
  438.  *   The operation to be performed. Possible values:
  439.  *   - "form": Display the widget settings form.
  440.  *   - "validate": Check the widget settings form for errors.
  441.  *   - "save": Declare which pieces of information to save back to the database.
  442.  * @param $widget
  443.  *   The widget on which the operation is to be performed.
  444.  * @return
  445.  *   This varies depending on the operation.
  446.  *   - "form": an array of form elements to add to the settings page.
  447.  *   - "validate": no return value. Use form_set_error().
  448.  *   - "save": an array of names of form elements to be saved in the database.  
  449.  */
  450. function zipcode_widget_settings($op, $widget) {
  451.   switch ($op) {
  452.     case 'form':
  453.  
  454.     case 'validate':
  455.       break; //do nothing
  456.  
  457.     case 'save':
  458.       return array();
  459.   }
  460. }
  461.  
  462.  
  463. /**
  464.  * Implementation of hook_widget().
  465.  *
  466.  * Attach a single form element to the form. It will be built out and
  467.  * validated in the callback(s) listed in hook_elements. We build it
  468.  * out in the callbacks rather than here in hook_widget so it can be
  469.  * plugged into any module that can provide it with valid
  470.  * $field information.
  471.  *
  472.  * Content module will set the weight, field name and delta values
  473.  * for each form element. This is a change from earlier CCK versions
  474.  * where the widget managed its own multiple values.
  475.  *
  476.  * If there are multiple values for this field, the content module will
  477.  * call this function as many times as needed.
  478.  *
  479.  * @param $form
  480.  *   the entire form array, $form['#node'] holds node information
  481.  * @param $form_state
  482.  *   the form_state, $form_state['values'][$field['field_name']]
  483.  *   holds the field's form values.
  484.  * @param $field
  485.  *   the field array
  486.  * @param $items
  487.  *   array of default values for this field
  488.  * @param $delta
  489.  *   the order of this item in the array of subelements (0, 1, 2, etc)
  490.  *
  491.  * @return
  492.  *   the form item for a single element for this field  
  493.  */
  494. function zipcode_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  495.   $element = array(
  496.     '#type' => $field['widget']['type'],
  497.     '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
  498.   );
  499.   return $element;
  500. }
  501.  
  502. /**
  503.  * Implementation of hook_content_is_empty().
  504.  *
  505.  * NEW REQUIRED HOOK!
  506.  *
  507.  * This function tells the content module whether or not to consider
  508.  * the $item to be empty. This is used by the content module
  509.  * to remove empty, non-required values before saving them.
  510.  */
  511. function zipcode_content_is_empty($item, $field) {
  512.   if (empty($item['value'])) {
  513.     return TRUE;
  514.   }
  515.   return FALSE;
  516. }
  517.  
  518. /**
  519.  * Implementation of FAPI hook_elements().
  520.  *
  521.  * Any FAPI callbacks needed for individual widgets can be declared here,
  522.  * and the element will be passed to those callbacks for processing.
  523.  *
  524.  * Drupal will automatically theme the element using a theme with
  525.  * the same name as the hook_elements key.
  526.  *
  527.  * Autocomplete_path is not used by text_widget but other widgets can use it
  528.  * (see nodereference and userreference).
  529.  */
  530.  function zipcode_elements() {
  531.   return array(
  532.     'zipcode_textfield' => array(
  533.       '#input' => TRUE,
  534.       '#columns' => array('value'), '#delta' => 0,
  535.       '#process' => array('zipcode_textfield_process'),
  536.       '#autocomplete_path' => FALSE,
  537.       ),
  538.   );
  539. }
  540.  
  541. /**
  542.  * FAPI theme for an individual text elements.
  543.  *
  544.  * The textfield or textarea is already rendered by the
  545.  * textfield or textarea themes and the html output
  546.  * lives in $element['#children']. Override this theme to
  547.  * make custom changes to the output.
  548.  *
  549.  * $element['#field_name'] contains the field name
  550.  * $element['#delta]  is the position of this element in the group
  551.  */
  552. function theme_zipcode_textfield($element) {
  553.   return $element['#children'];
  554. }
  555.  
  556. /**
  557.  * Process an individual element.
  558.  *
  559.  * Build the form element. When creating a form using FAPI #process,
  560.  * note that $element['#value'] is already set.
  561.  *
  562.  * The $fields array is in $form['#field_info'][$element['#field_name']].
  563.  */
  564. function zipcode_textfield_process($element, $edit, $form_state, $form) {
  565.   $field = $form['#field_info'][$element['#field_name']];
  566.   $field_key = $element['#columns'][0];
  567.   $delta = $element['#delta'];
  568.   $element[$field_key] = array(
  569.     '#type' => 'textfield',
  570.     '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
  571.     '#autocomplete_path' => FALSE,
  572.     // The following values were set by the content module and need
  573.     // to be passed down to the nested element.
  574.     '#title' => $element['#title'],
  575.     '#description' => $element['#description'],
  576.     '#required' => $element['#required'],
  577.     '#field_name' => $element['#field_name'],
  578.     '#type_name' => $element['#type_name'],
  579.     '#delta' => $element['#delta'],
  580.     '#columns' => $element['#columns'],
  581.   );
  582.  
  583.   if (!empty($field['max_length'])) {
  584.     $element[$field_key]['#maxlength'] = $field['max_length'];
  585.   }
  586.   if (!empty($field['text_processing'])) {
  587.     $filter_key = $element['#columns'][1];
  588.     $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
  589.     $parents = array_merge($element['#parents'] , array($filter_key));
  590.     $element[$filter_key] = filter_form($format, 1, $parents);
  591.   }
  592.  
  593.   // Used so that hook_field('validate') knows where to flag an error.
  594.   $element['_error_element'] = array(
  595.     '#type' => 'value',
  596.     '#value' => implode('][', array_merge($element['#parents'], array($field_key))),
  597.   );
  598.  
  599.   return $element;
  600. }
  601.  
  602. /**
  603.  * Theme function for 'default' text field formatter.
  604.  */
  605. function theme_zipcode_formatter_default($element) {
  606.   return $element['#item']['value'];
  607. }
  608.  
  609. /**
  610.  * Verification for zipcodes.  
  611.  *
  612.  * @param string $text
  613.  * @return boolean Returns boolean FALSE if the zipcode is not valid.
  614.  * On success, returns a string containting the zipcode with some formatting.
  615.  */
  616.  
  617. /**
  618.  * Verification for zipcodes.
  619.  *
  620.  * @param string $countrycode
  621.  * @param string $zipcodestring
  622.  * @return boolean Returns boolean FALSE if the zipcode is not valid.  
  623.  */
  624. function valid_zipcode($countrycode, $zipcodestring) {
  625.  
  626.   $countrycode = trim($countrycode);
  627.   $zipcodestring = trim($zipcodestring);
  628.  
  629.   if ($countrycode == 'fr'
  630.         || $countrycode == 'ca'
  631.         || $countrycode == 'us'
  632.         || $countrycode == 'uk'
  633.         || $countrycode == 'nl'
  634.         || $countrycode == 'hu'
  635.         || $countrycode == 'au'
  636.         || $countrycode == 'dk'
  637.         || $countrycode == 'pt'        
  638.         ) {  
  639.        
  640.         //drupal_set_message('langue = ' . $countrycode, 'error');
  641.  
  642.         $valid_zipcode_function = 'valid_'. $countrycode . '_zipcode';         
  643.         module_load_include('inc', 'zipcode', 'zipcode.'. $countrycode);
  644.        
  645.         if (function_exists($valid_zipcode_function)) {
  646.             return $valid_zipcode_function($zipcodestring);
  647.         }
  648.         else  {
  649.             return FALSE;      
  650.         }
  651.   }
  652.   else {
  653.         //Country not taken into account yet
  654.         return FALSE;
  655.   }
  656. }  
  657.        
  658. /**
  659.  * Formatting for Zipcode.  
  660.  *
  661.  * @param string $countrycode
  662.  * @param string $zipcodestring
  663.  * @return string Returns a string containting the zipcode with some formatting.
  664.  */
  665. function format_zipcode($countrycode, $zipcodestring, $field) {
  666.  
  667.   $countrycode = trim($countrycode);
  668.   $zipcodestring = trim($zipcodestring);
  669.  
  670.   if ($countrycode == 'fr'
  671.         || $countrycode == 'ca'
  672.         || $countrycode == 'us'
  673.         || $countrycode == 'uk'
  674.         || $countrycode == 'nl'
  675.         || $countrycode == 'hu'
  676.         || $countrycode == 'au'
  677.         || $countrycode == 'dk'
  678.         || $countrycode == 'pt'        
  679.         ) {    
  680.        
  681.         //drupal_set_message('langue = ' . $countrycode, 'error');      
  682.  
  683.         $format_zipcode_function = 'format_'. $countrycode . '_zipcode';       
  684.         module_load_include('inc', 'zipcode', 'zipcode.'. $countrycode);
  685.        
  686.         if (function_exists($format_zipcode_function)) {
  687.             return $format_zipcode_function($zipcodestring, $field);
  688.         }
  689.         else {
  690.             return FALSE;
  691.         }
  692.   }
  693.   else {
  694.         //Country not taken into account yet  
  695.         return FALSE;
  696.   }
  697. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: