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

Blog Posts in Newsletter


Reply

7 Posts   897 Views

Avatar
bones

18 December 2010 at 4:37am Community Member, 104 Posts

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.

Avatar
bones

5 January 2011 at 1:56am Community Member, 104 Posts

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.

Avatar
MarcusDalgren

5 January 2011 at 3:02am Community Member, 288 Posts

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.

Avatar
MarcusDalgren

5 January 2011 at 3:29am Community Member, 288 Posts

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

Avatar
bones

5 January 2011 at 3:50am Community Member, 104 Posts

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 :) :) :)

Avatar
MarcusDalgren

5 January 2011 at 3:59am Community Member, 288 Posts

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.

Avatar
bones

5 January 2011 at 4:49am Community Member, 104 Posts

Thanks again. That's very helpful :)