Thanks for posting about this problem, rwestera. Unfortunately, your fix didn't do the trick for me. For some reason, I can't seem to set the value of the MultiSelectField until after it has already been initialized, where it looks like this needs to be done during initialization.
If I add a var_dump() to the Field() method in the MultiSelectField class for the selected values, it returns an empty array every time. But if I add the field using the following code:
$oFields->addFieldToTab('Root.PreferredProducts', $oPreferredProducts = new MultiSelectField(
'PreferredProducts',
'Voorkeursproducten',
$oProducts->map('ID', 'Title'),
$aPreselectedProducts
));
var_dump($oPreferredProducts->getSelected());
The var_dump() returns an array with all of the options I passed (in $aPreselectedProducts), but they aren't selected at all in the HTML or on the screen!
In the end I went the ugly route by using the following code:
$jsSelectOptions = "";
foreach ($oPreferredProducts as $oProduct) {
// Ugly JS fix
$jsSelectOptions .= "jQuery('#Root_PreferredProducts select.multiselect-unselected option[value=\"".$oProduct->ID."\"]').attr('selected', 'selected');".chr(10);
}
// Preselect all default products and fire the click event for the "Add" button
Requirements::customScript("
(function($) {
".$jsSelectOptions."
$('#Root_PreferredProducts .multiselect-add').click();
})(jQuery);
");