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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

Newsletter signup form widget


Go to End


3 Posts   3724 Views

Avatar
Phill

Community Member, 81 Posts

2 April 2009 at 4:18am

Hey

Im trying to build a widget which allows people to sign up to a newsletter iv been able to build a form which does this on a page but cant get it to work as a widget.

This is the code iv been using to generate the form on a page which all works fine

function NewsletterForm() {
return new Form($this, "FormJoin", new FieldSet(
// List the your fields here
new EmailField("Email", "Enter your email address: ", "")

), new FieldSet(
// List the action buttons here
new FormAction("doform", "Subscribe")

), new RequiredFields(
"Email"
));
}
function doform($data, $form) {
// Create a new Member object and load the form data into it
$member = new Member();
$form->saveInto($member);

// 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 = 'mailing-list'")) {
$member->Groups()->add($group);
}

// Redirect to a page thanking people for registering
Director::redirect('/thanks-for-joining');
}

but i cant work out how to do the same for a widget it doesnt work if i put the code in the widgets file and i cant access it from the widget if its stored in page.php any ideas on how i could do this?

thanks

Avatar
rokryan

Community Member, 13 Posts

19 August 2009 at 1:30pm

I'm in the same boat, does anyone have a stable solution for a Newsletter Subscription Widget??

Avatar
CHD

Community Member, 219 Posts

11 June 2011 at 4:06am

this may come in handy for anybody who wants to easily include forms in a sidebar or any other area of the site, without controlling a hidden page:

http://www.clickheredigital.co.uk/blog/how-to-include-a-silverstripe-form-on-any-every-page/

it also has an easy "query database for existing member" function that isn't covered in the form tutorials...