Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Newsletter 0.4 - customize a part of newsletter subscription form


Go to End


945 Views

Avatar
Myrdhin

Community Member, 70 Posts

24 March 2010 at 3:54am

Edited: 24/04/2010 12:32am

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,