Jump to:

10994 Posts in 2728 Topics by 1819 members

All other Modules

SilverStripe Forums » All other Modules » How to extend Newsletter

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1170 Views
  • davepolyester
    Avatar
    Community Member
    47 Posts

    How to extend Newsletter Link to this post

    I have Newsletter V0.4 running well on SS 2.3.7. Subscribe, unsubscribe, my templates, and viewing and editing saved drafts is all working great thanks in no small part to the helpful advice on this forum.

    I now want to add some advanced functionality to my template so that it draws info from other parts of the site automatically. For example, I would like the latest five events from my events calendar to be added to the newsletter email.

    I already have this working fine on my home page (again thanks to the forums and tutorials), along with the latest news and some random content that displays a different Featured Group every time the page is refreshed.

    This was simple enough to do via the HomePage.ss page type, which extends the Page.ss page type, but the same workflow doesn't seem to apply to the Newsletter module.

    Can anyone give me a basic workflow on how to achieve this, or some good reasons why it cannot be done?

    Is there a way to reference external PHP files with the required code from within my newsletter email template, without having to mess around with the Newsletter module code?

    I've seen a few posts asking for similar advice but no real answers so I'm putting it out there again.

    Many thanks!

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: How to extend Newsletter Link to this post

    Yes this can be done but unless there's been changes made in how the Newsletter module works you'll have to make changes in the Newsletter module files. The problem is basically getting the info you want into the Newsletter template.

    If we take your example of adding the latest five events from your event calendar you'll have to edit NewsletterAdmin and NewsletterEmailProcess to make sure that the extra data gets added to the newsletter.

    In the case of NewsletterEmailProcess you're looking for $e->populateTemplate on line 84. In NewsletterAdmin things start at line 569. In both cases you have access to the newsletter being sent so I'd make a newsletter decorator with a method that fetches the latest 5 events and add that through
    Object::add_extension("Newsletter", "NewsletterDecorator"); in _config.php
    so if we assume you that you call that method getLatestEvents() you'd write

    in NewsletterAdmin on line 572

    $e->LatestEvents = $newsletter->getLatestEvents();


    in NewsletterEmailProcess line 84 replace the current statement with

    $e->populateTemplate(array(
       'Member' => $member,
       'FirstName' => $member->FirstName,
       'NameForEmail'=> $nameForEmail,
       'LatestEvents'=> $this->newsletter->getLatestEvents()
    ));


    With this you should be able to write <% control LatestEvents %> in your newsletter template to get the latest 5 events.

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