Hello,
I try to have a subscription form on all my pages. So i add this function in my Page_Controller in Page.php (and i use it in my template) :
function NewsletterSubscribeForm($newsletterTitle){
$subscribeForm = DataObject::get_one('SubscribeForm', "`Title`='$newsletterTitle'");
if($subscribeForm) {
if(Translatable::is_enabled()){
$locale = Translatable::get_current_locale();
$subscribeForm = $subscribeForm->getTranslation($locale);
}
$formController = new SubscribeForm_Controller($subscribeForm);
return $formController->Form()->renderWith('MyNewsletterSubscribeForm', 'Form');
}
return false;
}
It's OK, it works.
But I have only one newsletter and i don't want to select a newsletter in the list... I would like to default checked this newsletter. I try to add this code juste before the return in my function :
// I get the source to have the keys
$source = $formController->Form()->Fields()->fieldByName('Newsletters')->getSource();
$values = array();
// I create an array "values" with source keys and values set to 1 (to have HTML 'checked="checked"' when the form will render)
foreach($source as $index => $item) {
if ( is_a($item, 'DataObject') ) $key = $item->ID;
else $key = $index;
$values[$key] = 1;
}
// I set Value for this CheckboxSetField
$formController->Form()->Fields()->fieldByName('Newsletters')->setValue($values);
But it doesn't work :'(
Help
Thanks,