Defining the primary address of a CiviCRM contact I have a webform with CiviCRM fields on it (webform 7.x.4.1, civicrm_webform 7.x-4.9). 

I have configured 2 addresses fieldset, one for work address and one for home address. 
I find it non intuitive to let the user choose the location for each address and set the first one as the primary. So i have instead defined one field that let the user choose which one is its primary address (simple select field with Home or Work value).

Now, i need to synchronize this with CiviCRM after the form is submitted. I use hook_webform_submission_insert and hook_webform_submission_update.

My question is : what is the best way to define which address is the primary for a contact (the address are already saved in the database). Api ? other ?

Alternative question : maybe there is another (more simple) way to do the same thing ?
Webform-CiviCRM will always set the first address as "primary".

You don't need to expose the location-type field if you don't want to - you can set it on the backend form. This has the downside of forcing the location type of the primary address to change to whatever you've set the first address to.

Here is a solution that will solve your problem for both creating and updating contacts, and you don't need to resort to custom code and hooks:


Set the location-type field for both addresses to be exposed (user select)
Change both location-type webform fields to type "hidden"
Set the default value for the first one to be "home" and the other "work"


Now when the user updates their address, the location type will not be changed (your default values will be overwritten by the existing data). But when creating new addresses you'll be setting sensible defaults.