Playing with the CCK Address module
As If - August 3, 2008 - 22:07
This topic is for posting hacks, patches and overrides for various applications of the CCK Address module. Like the excellent patch here for breaking out the address subfields in a View. The CCK Address module has a lot of usefullness under the hood but IMHO, you need to get inside and poke around to get the maximum value out of it. Post your tweaks here, and you may help others with similar issues.

Allowing All Countries and "Non US/CA" States
For US/CA traffic the default modules are fine, but if you want to allow other countries you would have to create an extension module. I didn't want to do that ;-) and I decided that state-vs-country validation was not vital for my purposes. I wanted two things: (1) the State dropdown should include a "Non-US/CA" option, and (2) the Countries dropdown should allow every country in the world. Here's how I did it...
INPUT ALL COUNTRIES TO DB
Add all the countries you want to your cck_address_countries table. Click here for a full list and a table dump.
ADD NON-US/CA STATE TO DB
If you use the Canada extension, the cck_address_states table will already include all states/provinces for the US and Canada. But to add one more option for anyone from "Non-US/CA" states or provinces, insert the following entry to your cck_address_states table:
INSERT INTO cck_address_states (state_id, state_name, state_abbrv, country_code) VALUES ('', 'Non-US/CA', '--', '--');NOTE that the country code "--" comes before "CA" alphabetically because we want this option to show up first on the list.
Ok, now it's time to open up cck_address.module and start hacking. Our first goal is to disable the checking of states against countries (we don't want to input all the states and provinces of every country in the world). So here's what we do...
MODIFY CCK_ADDRESS_GET_STATES()
Find the function called cck_address_get_states() and comment out everything from line 1302 to 1318 inclusive. Be sure to keep the first line
$state_options = array();and keep the sql query itself (line 1319). Comment out everything between them. This removes the WHERE clause and ensures that ALL states/provinces will be returned.MODIFY ALL CALLS TO CCK_ADDRESS_GET_STATES()
Now go through cck_address.module and remove the arguments from every call to the cck_address_get_states() function. This call appears multiple times throughout the file. For example, on line 48, we will change
$state_options = cck_address_get_states($all_countries);to$state_options = cck_address_get_states();. Just remove the argument in the parentheses. Do the same thing to every one of them.MODIFY CCK_ADDRESS_FIELD_FORMATTER()
We also need to modify the field formatter to include all countries and all states at all times. Find the following lines in cck_address_field_formatter(), and comment them out as shown below:
// if ($field['other_countries']) {
$country[$item['country']] = $item['country'];
// }
// else {
// $country[$item['country']] = $all_countries[$item['country']];
// }
// if ($field['state_abbrv'] == 2) {
// $cck_address_state = strip_tags($item['state']);
// }
// else {
// $states = cck_address_get_states($country);
$states = cck_address_get_states();
// }
(Notice the modified "cck_address_get_states()" from the previous step. Be sure to keep that.)
DISABLE LIMITATION IN FIELD SETTINGS
To allow our new settings to work on the Field Settings page without choking the module, we need to modify the cck_address_field_settings() function. Find these lines and comment them out:
// if (($field['other_countries'] == 0) && ($field['state_abbrv'] == 2)) {// form_set_error('other_countries', t('"Allow <em>other</em> countries?" must be checked if you are using <em>Free-text entry</em> for states.'));
// }
// if (($field['other_countries'] == 1) && ($field['state_abbrv'] != 2)) {
// form_set_error('state_abbrv', t('You must use "<em>Free text entry</em>" for states if you "Allow <em>other</em> countries".'));
// }
DISABLE STATE-COUNTRY CHECK IN NODE FORM
Likewise, to disable the checking on the Node Add/Edit pages, find these lines in cck_address_field() and comment them out:
// if (!$field['other_countries'] && !array_key_exists($item['state'], $state_options)) {// $errors['state'] = t('Illegal value for %name\'s State field. You must select a State from the dropdown list.', array('%name' => t($field['widget']['label'])));
// }
All changes are made. Upload the modified module file and...
CONFIGURE YOUR ADDRESS FIELD SETTINGS
1. For "Display States/Provinces as:" choose "Select with Full Names"
2. Check the "Allow other countries?" checkbox.
3. Check all the countries you want to include (there should be a LOT of them now)
4. Save your field settings.
You're done.
Hello- Working on applying
Hello-
Working on applying your hack. I noticed that the line numbers do not match up with the current version, is there something I am missing?
Thanks for a great tutorial.
venusrising
adjusted
Not sure what happened, but here are the current (version 5.x-2.1) line numbers:
MODIFY CCK_ADDRESS_GET_STATES()
should say "comment out everything from line 1295 to 1311 inclusive". The first commented line is
$where = 'WHERE ';and the last commented line is the closing bracket of theelse.MODIFY ALL CALLS TO CCK_ADDRESS_GET_STATES()
Instead of line 48, look at line 141 for an example. But you still need to do all of them the same way.
Everything else should still be good.
Thanks for the update will
Thanks for the update will check work through it now. BTW do you offer any Drupal services?
venusrising
One more question, the
One more question, the portion on the CCK_Address formatter field looks different than the example too
venusrising
found it down in the code
found it down in the code
Sometimes it seems I do nothing else!
(Not that I really mind, you understand.)
Sure do. Hit my profile for TMI.
-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com
When I hit the allow other
When I hit the allow other countries check box it turns it in to a text field and makes the state box almost disappear. I have made sure everything is commented out so I am not sure if this new version is the issue. Does country and state/provinces need to be added separately (as fields instead of using the display type within the module?)
venusrising