Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm trying to programmatically add an address through php. Are there any APIs for doing so?
And also to check if a user has an address assigned to them.
Are there APIs for doing so or should I just do a SQL call, if so what are the tables?
Thanks
Comments
Comment #1
michaelfavia CreditAttribution: michaelfavia commentedThe address field leverages drupals field api. As such there are a number of ways you can populate it with data. Programatic form executions, node_save(), etc.
Comment #2
trevorwh CreditAttribution: trevorwh commentedReviving this issue for the moment. I've added addressfield as profile item, but when I submit via drupal_submit_form(), for some reason the validation in addressfield causes a $rebuild = true; for the form, which means that it doesn't continue to the submit handler.
When i remove the addressform field, it works perfectly.
Any ideas on why this is occuring? I'm looking at line 821 in the drupal_process_form function.
if ($form_state['process_input']) {
drupal_validate_form($form_id, $form, $form_state);
// drupal_html_id() maintains a cache of element IDs it has seen,
// so it can prevent duplicates. We want to be sure we reset that
// cache when a form is processed, so scenarios that result in
// the form being built behind the scenes and again for the
// browser don't increment all the element IDs needlessly.
drupal_static_reset('drupal_html_id');
var_dump($form_state);
if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) {
}
Because form_state['rebuild'] is true after drupal_validate form, it doesn't continue.
Comment #3
drupalninja99 CreditAttribution: drupalninja99 commented+1. What if an address isn't a node, like with the commerce module the address belongs to entities.
Comment #4
drupalninja99 CreditAttribution: drupalninja99 commentedOR programatically save commerce_customer_profile, which includes addresses, havent figured that out yet.
Comment #5
rszrama CreditAttribution: rszrama commentedI don't know what was going on with the rebuild loop above - this module should only trigger a rebuild if it detects the country value has changed and the AJAX refresh didn't occur (i.e. JavaScript was turned off).
However, for the other folks wondering how to programmatically set an address, you can either use the Fields API directly or use the entity metadata wrapper from the Entity API module like so:
(Note: this example comes from Commerce; change the entity type and field name for your situation, but note that you always have to set the country.)
Comment #7
Marko B CreditAttribution: Marko B commentedI would try with this here http://drupal.stackexchange.com/questions/124056/save-user-fields-in-com... it has much more information that in the post from Ryan