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 module: Widget for subscription form


Reply


3 Posts   1526 Views

Avatar
Chiasmata

Community Member, 5 Posts

19 October 2010 at 1:14am

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.

Avatar
swaiba

Forum Moderator, 1808 Posts

19 October 2010 at 9:20pm

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

Avatar
Chiasmata

Community Member, 5 Posts

20 October 2010 at 3:10am

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.