Jump to:

10994 Posts in 2728 Topics by 1819 members

All other Modules

SilverStripe Forums » All other Modules » Newsletter 0.4 - customize a part of newsletter subscription form

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 643 Views
  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Newsletter 0.4 - customize a part of newsletter subscription form Link to this post

    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,

    643 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.