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.

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



Community Member, 70 Posts

24 March 2010 at 3:54am

Edited: 24/04/2010 12:32am


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) {
			$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

But it doesn't work :'(