Thank you Klas for your help!
I will
let the steps solution I took if someone else need something like that:
For
this solution I used BeforeRender plugin and changed Dynamic select field on the
fly as I need in the server side (I am not a programmer, so I´m not using
"the best practices", but just the shortest way I found).
1) I discovered that $fields['mydynamicfieldname']->form gives me a string with all html markup of the dynamic field like this:
<select id="est_city" name="est_city" style="width:268px; height: 34px;"> // First Line
<option value="">- Select-</option>
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>
2) I took this string and extracted the first line of It using these php lines:
$options=$fields['est_city']->form;
$options = substr($options, 0, strpos($options, "<option")-1) // this line extracts the very first line from the string above
3)Then I make a simple sql statement to retrieve the new content to the dynamic field from my custom database:
$namestate=$fields['est_state']->value;
$selected=$fields['est_city']->value;
//If $namestate is empty, I will use default state as "SP" and default city as "São Paulo"
If ($namestate==''){
$namestate='SP';
$selected='São Paulo'; }
$db = JFactory::getDbo();
$query = "Select name_cidty From #__cities Where name_state='".$namestate."'" ; // retrieves all cities from specific state in the state field of your form
$cities= $db->loadColumn();
4)Use a foreach php loop to complete the string with all new options you need:
$options .= '<option value="">- Select -</option>'; // put a "Select" label to the string - Second line
foreach ($cities as $city) {
if ($city<>$selected){
$options .= '<option value="'.$city.'">'.$city.'</option>'; }
else { $options .= '<option value="'.$city.'" selected="selected">'.$city.'</option>';}
}
$options .= '</select>' ; // Last line closing the tag
$fields['est_city']->form=$options;
Hope It
helps someone!This
solution makes the dynamic storage works. No need to use auxiliar fields.
EDIT: Very important! To all this code work the dynamic city field MUST load originally ALL city options, like suggested by Klas!