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, biapar, Willr, Ingo, swaiba, simon_w

Newsletter 0.4 - customize a part of newsletter subscription form


Reply

649 Views

Avatar
Myrdhin

24 March 2010 at 3:54am (Last edited: 24 April 2010 12:32am), Community Member, 67 Posts

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,