I have a node and I need to populate a field programmatically, so here is what I do :
$campaigns = $client->get_campaigns();
$tab_campaign = array(""=>"Dernière newsletter");
foreach ($campaigns->response as $camp){
$tab_campaign[$camp->CampaignID] = $camp->Name;
}
$form['field_last_newsletter'] = array(
'#type' => 'select',
'#required' => true,
'#options' => $tab_campaign,
'#title' => 'Choisir la dernière newsletter',
);
}
This work, I have my select field populated but when I select one and click on save nothing is saved, if I come back to the edit page the select have the default value, what I am doing wrong ?
Thanks.
I think you're looking for a
allowed_values_functionsetting for option fields. It is a perfect solution for fields with dynamic options.First, you need to change the current field settings to use the function to set the allowed values. To do this, modify the field settings in features (if used):
If you do not use features, you can make this change by executing the PHP code or using
hook_update_NAfter saving the new settings, you need to implement the callback function for the dynamic allowed values.