Jump to:

10994 Posts in 2728 Topics by 1819 members

All other Modules

SilverStripe Forums » All other Modules » Blog Posts in Newsletter

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 891 Views
  • bones
    Avatar
    Community Member
    104 Posts

    Blog Posts in Newsletter Link to this post

    I'm trying to add my 3 most recent Blog posts into my Newsletter. The weird thing is that it works when clicking "preview this newsletter", but not in the actual emailed newsletter.

    This is what I've done:

    In newsletter/code/newsletter.php, I've added:

    function BlogLatest($number=3) {
    $holder = DataObject::get_one('BlogHolder', "Title = 'Blog'");
        return DataObject::get('BlogEntry', "ParentID = {$holder->ID}","Created DESC", false, $number);
    }


    And in mysite/templates/email/template.ss, I've added:

    <% control BlogLatest %>
    <h3><a href="$Link">$Title</a></h3>
    <p>$Content<a href="$Link">Read more...</a></p>
    <% end_control %>

    I've done the usual dev/build and ?flush=all but can't understand why it would work in Preview, but not in the actual email.

    Can anybody help, please? Many thanks.

  • bones
    Avatar
    Community Member
    104 Posts

    Re: Blog Posts in Newsletter Link to this post

    Still no luck on this. I had hoped that it was just something stuck in the cache, and it would miraculously work after the Christmas break, but no such luck.

    Can anyone help, please? Thanks.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Blog Posts in Newsletter Link to this post

    The problem is that in the newsletter template you're not accessing a Newsletter object, it's an Email.
    If you pull the latest branches via SVN http://svn.silverstripe.com/open/modules/newsletter/tags/rc/0.4.0-rc1 I think there's a fix in there that assigns the newsletter to a variable called Newsletter so you can write $Newsletter.BlogLatest and that should work.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Blog Posts in Newsletter Link to this post

    In fact this fix went into trunk so pull that instead.

  • bones
    Avatar
    Community Member
    104 Posts

    Re: Blog Posts in Newsletter Link to this post

    Fantastic, thank you.

    Instead of adding

    function BlogLatest($number=3) {
    $holder = DataObject::get_one('BlogHolder', "Title = 'Blog'");
    return DataObject::get('BlogEntry', "ParentID = {$holder->ID}","Created DESC", false, $number);
    }


    to newsletter/code/newsletter.php, I added it to newsletter/code/email/NewsletterEmail.php. I didn't change anything else, or even use $Newsletter.BlogLatest to call it (mysite/templates/email/template.ss remained exactly as I'd originally written it).

    Thank you again

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Blog Posts in Newsletter Link to this post

    If you want to keep the code in it's original state so you can update it if a new release of the module comes out you can use a decorator instead.

    For example

    NewsletterEmailExtension extends Extension {
    function BlogLatest($number=3) {
    $holder = DataObject::get_one('BlogHolder', "Title = 'Blog'");
    return DataObject::get('BlogEntry', "ParentID = {$holder->ID}","Created DESC", false, $number);
    }
    }

    Then add this in your _config.php in mysite

    Object::add_extension("Newsletter_Email", "NewsletterEmailExtension");

    Flush everything with a ?flush=all and this should work. It's a great way of adding functionality without making changes in core files.

  • bones
    Avatar
    Community Member
    104 Posts

    Re: Blog Posts in Newsletter Link to this post

    Thanks again. That's very helpful

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