Add Contact Form to Sidebar in Silverstripe CMS

In this tutorial we are going to add a form to a sidebar that can be included on any page on a website. You will be able to set whether to show the Form on a particular page via checkbox in the CMS.

1. Create a file called CustomSiteConfig.php in /mysite/code/ to hold the CustomSiteConfig class:

<?php
class CustomSiteConfig extends DataObjectDecorator {
function extraStatics() {
return array(
'db' => array(
'Mailto' => 'Varchar(100)',
'SubmitText' => 'Text',
)
);
}
public function updateCMSFields(FieldSet &$fields) {
$fields->addFieldToTab("Root.SideBar", new TextField('Mailto', 'Email submissions to'));
$fields->addFieldToTab("Root.SideBar", new TextareaField('SubmitText', 'Text on Submission'));
}
}

2. In mysite/_config.php add lines

DataObject::add_extension('SiteConfig', 'CustomSiteConfig')

3. In mysite/code/page.php Page_controller add lines:

class Page_Controller extends ContentController {
public static $allowed_actions = array (
);
public function init() {
parent::init();
}
function LoyaltyForm() {
$fields = new FieldSet(
new TextField('Name', 'Name'),
new TextField('Address', 'Address'),
new TextField('Phone', 'Phone'),
new EmailField('Email', 'Email')
);
$actions = new FieldSet(
new FormAction('SendLoyaltyForm', 'Sign me up!')
);
$validator = new RequiredFields('Name', 'Email');
$validator->setJavascriptValidationHandler('none');
return new Form($this, 'LoyaltyForm', $fields, $actions, $validator);
}
function SendLoyaltyForm($data) {
$siteConfig = $this->SiteConfig();
$From = $data['Email'];
$Name = $data['Name'];
$To = $siteConfig->Mailto;
$Subject = "$Name has sent a request to sign up for the loyalty card";
$email = new Email($From, $To, $Subject)
$email->setTemplate('LoyaltyEmail');
$email->populateTemplate($data);
$success = $email->send();
Director::redirect(Director::baseURL(). $this->Link("/?loyaltyformsuccess=1"));
}
public function LoyaltyFormSuccess() {
return isset($_REQUEST['loyaltyformsuccess']) && $_REQUEST['loyaltyformsuccess'] == "1";
}
}

4. In Page.php before the controller add lines like here but keep the originals:

class Page extends SiteTree {
public static $db = array(
'ShowLoyaltyForm' => 'Boolean'
);
static $defaults = array(
'ShowLoyaltyForm' => false
);
function getCMSFields(){
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Behaviour', new CheckboxField("ShowLoyaltyForm", "Show Loyalty form on this page?"));
return $fields;
}
}

5. In themes/myTheme/templates/Includes/LoyaltyForm.ss add the following:

<% if ShowLoyaltyForm %>
<% if LoyaltyFormSuccess %>
<h2 class="white">Thanks!</h2>
<p class="success">$SiteConfig.SubmitText</p>
<% else %>
<h2 class="white">Sign up for our Loyalty Program!</h2>
$LoyaltyForm
<% end_if %>
<% end_if %>

6. In themes/myTheme/templates/Page.ss use this line between sidebar lines to get the form:

<% include LoyaltyForm %>

7. Last thing you need to do is the email template  – themes/myTheme/templates/Emails/LoyaltyEmail.ss

<p>
$Name has requested to sign up to the loyalty program,
you may respond to $Name by replying to this email.
</p>
<dl>
<dt>Name:</dt>
<dd>$Name</dd>
<dt>Address:</dt>
<dd>$Address</dd>
<dt>Phone:</dt>
<dd>$Phone</dd>
<dt>Email:</dt>
<dd>$Email</dd>
</dl>

Looking for quality SilverStripe Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

2 Comments on Add Contact Form to Sidebar in Silverstripe CMS

  1. Mike says:

    Doesn’t seem to work in SilverStripe 3? Some of the terms (DataObjectDecorator) are now deprecated.

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + = 8

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>