Jump to:

11004 Posts in 2735 Topics by 1824 members

All other Modules

SilverStripe Forums » All other Modules » Newsletter module: Widget for subscription form

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1469 Views
  • Chiasmata
    Avatar
    Community Member
    5 Posts

    Newsletter module: Widget for subscription form Link to this post

    Hi

    I'm running SS 2.4.2 and have installed the newsletter module.
    I have setup a newsletter subscription page type just containing an email field so that users can register easily.
    What I'd like to do though is make this form appear as a widget so I can insert it in the sidebar of my home page (which has already been setup to display widgets).

    How do I do this?

    I tried using the page content and single page widgets in the hope that they would simply display the form content from my subscription page within the widget, however it seems that these widgets assume a normal sitetree page type and will only output the 'content' field rather than the form.

    Are there any other ways round this? Am I missing an obvious solution?

    Any help will be much appreciated.

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: Newsletter module: Widget for subscription form Link to this post

    Hi, How's this...

    mysite\widgets\NewsletterSignUpWidget.php

    <?php

    class NewsletterSignUpWidget extends Widget
    {
       static $title = "";
       static $cmsTitle = "Newsletter Sign Up";
       static $description = "Newsletter Sign Up - requires page with URL segement 'thanks-for-joining' in the CMS";
    }

    class NewsletterSignUpWidget_Controller extends Widget_Controller
    {
       function NewsletterSignUpForm()
       {
          $fields = new FieldSet(
             new TextField("FirstName", "Enter your first name: ", ""),
             new EmailField("Email", "Enter your email address: ", "")
          );

          $actions = new FieldSet(
             new FormAction("doform", "Subscribe")
          );

          $required =new RequiredFields(
             "FirstName",
             "Email"
          );

          return new Form($this, "dosignupform", $fields, $actions, $required);
       }

       function dosignupform($data)
       {
          $member = new Member();
          $member->FirstName = $data['FirstName'];
          $member->Email = $data['Email'];

          // Write it to the database. This needs to happen before we add it to a group
          $member->write();

          // Add the member to group. (Check if it exists first)
          if($group = DataObject::get_one('Group', "Code = 'Newsletter'"))
          {
             $member->Groups()->add($group);
          }

          // Redirect to a page thanking people for registering, this needs creating int he CMS
          Director::redirect('/thanks-for-joining');
       }
    }

    mysite\widgets\NewsletterSignUpWidget.ss

    $NewsletterSignUpForm

  • Chiasmata
    Avatar
    Community Member
    5 Posts

    Re: Newsletter module: Widget for subscription form Link to this post

    Hi Swaiba

    That's great, thanks! Works a treat.
    I'm still quite new to SS, so am still getting my head around the way things work, but this has been a big help in more ways than one.

    Thanks again.

    1469 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.