I'm in a situation where I would like to use the address field module but am having some difficulties. The problem is that the address stored is for magazine subscriptions which need to store a lot of values individually. I believe this has something to do with the ability to look up the address and make sure it exists.
What I need to store is
• Street
• House number
• Something best translated as section, I believe it's used for when you have several apartment buildings using the same house number
• Floor
• Side (If there are several living on the same floor, normal for apartment buildings)
• Apartment (I don't actually know the use case for this one)
I don't know if it would be overkill to create a column for each one, some of these might also be specific for Denmark, as it's only Danish addresses that needs to be stored. I can see two solutions to this, one way would be to actually create columns for all of these use cases (I might not be the only one). That would make it possible to store these things if a field were created for them
Another solution could be to create a "data" or "additional fields" column which could store some serialized data. It would then be up to module creating the fields to make sure the extra fields were serialized. Going this route would also require to exposing the `hook_field_presave` and `hook_field_load` for the addressfield through a custom hook, as it otherwise is very difficult to do this.
Comment | File | Size | Author |
---|---|---|---|
#28 | addressfield-render_plugin_fields-1225292-28.patch | 1.9 KB | DuaelFr |
#16 | addressfield-render_plugin_fields-1225292-16.patch | 1.27 KB | dooug |
Comments
Comment #1
sportel CreditAttribution: sportel commentedI would also like the ability to store data in seperate fields. See also http://drupal.org/node/1034158#comment-4374880
Comment #2
googletorp CreditAttribution: googletorp commentedI talked with DamZ on IRC about this, and seems like the solution is to use a micro format, e.g. saving the street + house number as
"Street, housenumer" and use php to divide the two. With the data columns already present, this should solve my problem.
Comment #3
davidwhthomas CreditAttribution: davidwhthomas commentedsubscribing.
Comment #4
davidwhthomas CreditAttribution: davidwhthomas commentedI implemented my own addressfield plugin and added new fields, however they're never saved and when the address field is rendered, it shows the form element fields only, with no values. Is there something I'm missing there?
Comment #5
davidwhthomas CreditAttribution: davidwhthomas commentedOK, I've made some progress on extending with custom fields using a custom addressfield plugin.
the approach is basically to
One problem I've noticed is addressfield needs a patch to support the "render callback" key in the ctools plugin.
At the moment, addressfield only provides a single render callback for all format plugins afaik.
I guess ideally the addressfield module would handle storing and loading the field values to/from the data column in hook_field_presave and hook_field_load - but that's another issue.
Proof of extended field storage and load concept tested and working, may have a code snippet tomorrow.
cheers,
DT
Comment #6
davidwhthomas CreditAttribution: davidwhthomas commentedOK, this was a difficult exercise but finally have found success.
Here's the key code snippets posted to help others looking to do the same thing
Any additional fields added in the addressfield plugin are stored and loaded automatically from the "data" column.
Example data column looks like this:
a:6:{s:20:"flat_room_suite_type";s:5:"Suite";s:21:"flat_room_suite_value";s:3:"602";s:16:"floor_level_type";s:5:"Floor";s:17:"floor_level_value";s:1:"6";s:16:"block_tower_type";s:5:"Block";s:17:"block_tower_value";s:11:"Iona Towers";}
Note, in order to control the rendering of the new fields, I had to add to the addressfield plugin this, as the default addressfield render only renders the default field child elements, and the 'render callback' isn't used in the ctools plugin:
I hope that helps!
cheers,
DT
Comment #7
nclavaud CreditAttribution: nclavaud commentedThanks David, that helps a lot indeed.
I wish there would be a simpler way to extend those address fields. Actually the hard-coded arrays of field names in addressfield.module are somehow forcing us to rewrite a bunch of code...
Comment #8
xatoo CreditAttribution: xatoo commentedThe hardcoded arrays are indeed forcing plugin users to hack into the addressfield module.
It prevents, for instance, fields from Address Field Phone to be rendered:
See #2012798: Phone fields not rendered on display
Here is a patch that gets rid of the hardcoded arrays.
Comment #9
AaronBaumanThis patch results in a fatal error when submitting any form with an addressfield on it:
It does work to display phone fields already in the database.
However, (and maybe i'm opening a can of worms here) in the case of Address Field Phone, phone number gets rendered in the middle of the address block.
Seems like there should be a way to determine where the value should be rendered.
Since this data is serialized, I'm guessing that's not easy to build with views either.
Comment #10
xatoo CreditAttribution: xatoo commentedHmm, I was a bit overenthusiastic removing both hardcoded arrays but apparently I missed something.
Here is a patch that only removes the hardcoded array when rendering:
Comment #11
AaronBaumanYes, this works now to collect and display phone data.
However, outputting the phone between street address and city, state, zip is extremely confusing.
I think this needs to be addressed before this patch moves ahead.
Comment #12
xatoo CreditAttribution: xatoo commentedaaronbauman, what you describe is a different bug in Addressfield Phone. See:
#2010452: Phone gets rendered between address field 1 and state country fields (CR: weight change)
I'll post a patch for that one soon. Please restrict your feedback on this issue to the patch in #10.
Comment #13
steven.wichers CreditAttribution: steven.wichers commentedThis has fixed the issue for me.
Comment #14
cameron prince CreditAttribution: cameron prince commentedI tried the patch listed in number 10 and it failed for both current release and dev versions of addressfield. Attached is a quick re-roll and I can confirm that this does cause the extra fields to be rendered.
Comment #15
cameron prince CreditAttribution: cameron prince commentedThe patch in #14 wouldn't apply due to a header problem. Here's a replacement.
Comment #16
dooug CreditAttribution: dooug commentedpatch in #15 didn't apply and said it was corrupt. Re-rolled.
Confirmed that it shows the plugin field in the case of the addressfield_phone module.
Comment #17
TBarina CreditAttribution: TBarina commentedConfirmin #11.
Output of the phone is between street address and city, state and zip.
Besides, adding a label (Phone, Fax) would be much clearer.
Comment #18
TBarina CreditAttribution: TBarina commentedSorry, I overlooked #12.
Comment #19
5n00py CreditAttribution: 5n00py commentedAlso we have problems with loading default value. When we add some additional data to field we need to add new keys to $address variable in addressfield_field_widget_form.
There is no way to extend it. Function addressfield_default_values return hardcoded data:
In result we can't have full object.
And when its done, appears another "hole" called addressfield_process_format_form.
And don't know, maybe addressfield_process_format_form task can be achieved via '#process' callback in format definition.
This things make addressfield broken as api module.
Comment #20
Stomper CreditAttribution: Stomper commentedI'm currently using Address Field within a taxonomy for later use with one of the various geolocation/mapping modules. Like the OP, I'd also like the ability to filter and display addresses by component - city, state, zipcode etc.
I currently have a CSV file of several thousand addresses. Unfortunately, the addresses are stored in separate components, a row/column for street address, city, state, zipcode etc.
I'd like to bulk import these addresses into my taxonomy vocabulary but an unsure whether this module, or a import/export module like Taxonomy CSV or Feeds is able to map and format separated address components into the Address Field.
If not, I will have to abandon using this module for the addresses, and instead create individual text fields for each address component
Comment #21
5n00py CreditAttribution: 5n00py commentedHi Stomper!
This issue is not related to Your question.
As I see addressfield have feeds support. See next issue:
https://drupal.org/node/1408796
Comment #22
bdone CreditAttribution: bdone commented@stomper: migrate is another solution, and w/ the help of this patch (committed in 7.x-1.x-dev), it includes addressfield support.
Comment #23
Stomper CreditAttribution: Stomper commentedThanks for the tips, I will look to those approaches.
I tried it with Taxonomy CSV just to see what would happen, but since my vocabulary has less fields then by CSV has rows/columns, it didn't work. No worries, will try the other approaches.
One other thing, is there a means to use multiple fields to create an address (Address Field). Basically, if I have to separate the street address to match the format of my CSV list, since each "component" is being stored separately. How can I convert the separate fields into one Address Field address?
Comment #24
5n00py CreditAttribution: 5n00py commentedIn #19 I say that addressfield_process_format_form is problem for extending, but I solve my problem via #process. For more details see #2121651: Better form processing.
As for this issue.
Addressfield shouldn’t render/process data that stored separately (data column included).
Addressfield module just don't know how to do that.
So if You provide additional data & form elements, then You need to to this jobs with #process & #pre_render callbacks.
I think we need to write some docs (how to do this) and close this issue without any of prev. patches.
Comment #25
Stomper CreditAttribution: Stomper commentedI'd definitely appreciate some documentation on how to do it. If possible, an approach to accomplish this for non-programmers would be much appreciated, GUI would be perfect.
With some research, it seems possible to do what I need to do with Feeds Tamper/Explode, although not 100% sure.
https://drupal.org/node/1217356
Comment #26
rszrama CreditAttribution: rszrama commentedI was considering the patch but ultimately came to a similar conclusion as 5n00py; I don't necessarily like this module blindly printing anything out it finds in the array, because we just don't know what might put data in there.
However, I wouldn't mind a patch similar to #16 that let modules explicitly define the keys that should be rendered via the default rendering process. This would require some appropriate hook (no ideas on name atm), address field registering its own keys, and other modules implementing the hook as necessary. If a module doesn't use the hook, we'd assume it is using the callbacks 5n00py mentions above to show the values of whatever elements its adding to the address.
Comment #27
vasikeimho #16's patch is more than enough.
anyone who's want to extend the addressfield data with extra address elements, i think will now to use the '#access' property for the extra element unneeded to be displayed.
so i think we have a RTBC.
Comment #28
DuaelFrThe same kind of thing might be done in
_addressfield_process_format_form()
, isn't it ?