When new addresses are added in the function "uc_addresses_get_address_form_submit", the new "aid" can be saved within the form_state. This makes it easier for subsequent submit-functions to modify or use this data.
proposal :
uc_addresses.module, function uc_addresses_get_address_form_submit (r971-973)
- elseif ($view == 'new' || $view == 'add') { // Insert into datebase
- _uc_addresses_db_add_address($address);
- }
+ elseif ($view == 'new' || $view == 'add') { // Insert into datebase
+ $form_state['values']['panes']['address']['aid'] = _uc_addresses_db_add_address($address);
+ }
Comments
Comment #1
freixas commentedThe basic idea behind your request sounds reasonable.
I'm not sure if your proposal is the best solution. The first things I notice are that
A much better solution would be to create a hook function or two. Could you tell me more about how you would like to use the data when an address is added or edited?
Comment #2
HostYou commentedWhen you for example implement other modules that add some fields to the address, it would be possible to update or insert those fields, but i'll have to say: your hook-idea is indeed a better solution.
Implementation of following hooks would be easy to implement and awsome :
hook_address_presave, hook_address_insert, hook_address_delete and hook_address_update
I can write a patch implementing those hooks if you want?
Comment #3
freixas commentedOK, so if I understand you properly, you would be satisfied if uc_addresses provided a clean way to add additional address fields to the forms and databases. Correct?
I would love a patch. The idea sounds great. Now, let's look at the best way to write the patch. I guarantee that it is never as easy as it seems with uc_addresses.
One design pattern is to use some sort of "load" function (as with "user_load()"). Whenever an address is needed, load_addresses() is called which then calls module_invoke() (or equivalent) to allow other modules to add their data to the $address object. The additional data usually comes from a separate database table and requires a separate database fetch. This design pattern would be better than the hooks you suggest because it follows a standard Drupal practice for extending a database object.
uc_addresses was the first Drupal open source module I wrote and I inherited code written by others. I've since written uc_product_keys, which uses a much more efficient design pattern by taking advantage of the Drupal 6 drupal_write_record() function.
In this design pattern, you would add any new fields to the uc_addresses table and the schema. Records are read from the database by reading all fields (*) and creating an $address object. Records are inserted or updated by calling drupal_write_record(), which writes all the fields in the schema.
uc_addresses would need to ensure that the $address object is available for modification whenever it is reasonable to think that another module might want to update its fields. For forms, the $address object should be stored in $form or $form_state (I forget right now which is the appropriate one to use), but not in the submit handler—do this in the form creation function, so that the object is available for all submit handlers.
Since you modify the form after the form is created, the ordering problem is avoided. Any _alter() functions can be sure that the $address object has been defined and can initialize their own fields.
Outside of forms, any places where an $address object is created must call a hook to allow other modules to add their fields. Any other places where an existing $address object is modified needs to be analyzed to see if other modules might want to hook in as well.
Again, there is an example of this design pattern in uc_product_keys. It is more efficient to run and requires less code from those extending the database table (you don't need to write your own table insert/update/delete). It is just going to be a lot of work for you, if you decide to tackle this feature.
Why not just put in some quick-and-dirty hooks? Well, I believe that when code is written for re-use, it should aim for the best possible quality, performance and extensibility. By having one person take extra time, we save multiple developers time and reduce wasted CPU cycles on multiple installations.
Let me know if you still feel this is something you want to tackle. The hooks you suggest are a lot less work than anything I've suggested, so I can understand if you don't want to invest the time.
Comment #4
megachrizHi freixas,
I'm currently trying to integrate your module with uc_extra_fields_pane and I came across this problem. It was no problem to save extra data when an address is edited, but not when an address is added. Did you make any progress on this?
By the way, I have made a patch to how I fixed it right now, but it's not the most neat solution.
Comment #5
freixas commentedComment #6
megachrizWith the 6.x-2.x version you are able to modify or use address data upon form submit. Feel free to reopen this issue if you encounter still issues with this.