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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

Blog Posts in Newsletter


Go to End


7 Posts   1249 Views

Avatar
bones

Community Member, 110 Posts

18 December 2010 at 4:37am

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

Community Member, 110 Posts

5 January 2011 at 1:56am

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

Community Member, 288 Posts

5 January 2011 at 3:02am

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

Community Member, 288 Posts

5 January 2011 at 3:29am

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

Avatar
bones

Community Member, 110 Posts

5 January 2011 at 3:50am

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

Community Member, 288 Posts

5 January 2011 at 3:59am

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

Community Member, 110 Posts

5 January 2011 at 4:49am

Thanks again. That's very helpful :)