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.

Blog Module /

Discuss the Blog Module.

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

displaying blog entry with image in homepage


Go to End


8 Posts   4180 Views

Avatar
Rishi

Community Member, 97 Posts

15 January 2010 at 11:43pm

Hello
I am displaying blog entry (last 3) in my home page .its working perfectly,
the only problem is when I enter a Image in my blog entry then images are not shown in homepage.
I have limited the number of word to be displayed in homepage as 10,no matter how much i increase the word count ,image is never displayed in homepage.
Please help me out in displaying images in homepage,
My code is

in homepage .php

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

in template homepage.ss
<% control LatestNews %>
<p><a href="$Link">$Title</a><br>
$Content.LimitWordCount(10) </p>
<% end_control %>

thank you in advance

Avatar
MarcusDalgren

Community Member, 288 Posts

16 January 2010 at 1:40am

Edited: 16/01/2010 1:40am

I do believe LimitWordCount will do strip_tags or something to that effect so no matter how many words you get back you'll never get the image tag.

Your best bet is probably to add an image field to the blog entry and use that instead.
You could do something like:

<?php
   class BlogEntryDecorator extends DataObjectDecorator {
   	
	public function extraStatics(){
		return array(
			'has_one' => array(
				"TitleImage" => "Image"
			)
		);

	}	

	public function updateCMSFields(FieldSet &$fields) {
			$fields->addFieldToTab("Root.Content.Main", new ImageField("TitleImage", "Title image"), 'Content');
	}
}
?>

And then add this to _config.php:

Object::add_extension('BlogEntry', 'BlogEntryDecorator');

After you upload the image through the image field you can display it by writing $TitleImage in the template.

Avatar
Matty Balaam

Community Member, 74 Posts

23 June 2010 at 4:29am

How could I go about adding another field to this to allow the photo to have a caption? I'd assumed I could just amend it to this, but while the field appears in the CMS, when I save the page the data is lost. Thanks.

<?php

class BlogEntryDecorator extends DataObjectDecorator {
   
   public function extraStatics(){
      return array(
         'has_one' => array(
            "TitleImage" => "Image",
            "PhotoCaption" => "HTMLText"
         )
      );
   }   

   public function updateCMSFields(FieldSet &$fields) {
         $fields->addFieldToTab("Root.Content.Main", new ImageField("TitleImage", "Title image"), 'Content');
         $fields->addFieldToTab("Root.Content.Main", new TextField("PhotoCaption", "Photo Caption"), 'Content');
   }

}
?>

Avatar
kidcardboard

Community Member, 5 Posts

23 June 2010 at 4:41am

Not 100% sure but I think you might need to add your new fields to the db array also. ie

static $db = array(
		"TitleImage" => "Image",
		"PhotoCaption" => "HTMLText"
	);

Avatar
Matty Balaam

Community Member, 74 Posts

23 June 2010 at 5:21am

Edited: 23/06/2010 5:22am

Thanks. I tried doing that, but I wasn't sure how to it works in relation to 'return array', so couldn't get that to work.

Avatar
MarcusDalgren

Community Member, 288 Posts

23 June 2010 at 9:09am

When you're decorating you add db fields pretty much the same way you add extra relationships through decoration. The code you have is pretty close, this should work.

class BlogEntryDecorator extends DataObjectDecorator {

public function extraStatics(){
return array(
'db' => array("PhotoCaption" => "HTMLText"),
'has_one' => array("TitleImage" => "Image")
);
}

public function updateCMSFields(FieldSet &$fields) {
$fields->addFieldToTab("Root.Content.Main", new ImageField("TitleImage", "Title image"), 'Content');
$fields->addFieldToTab("Root.Content.Main", new TextField("PhotoCaption", "Photo Caption"), 'Content');
}
}

Try this and build the database after changing the code. If everything's setup correctly the text field should get added to the BlogEntry table.

Avatar
Matty Balaam

Community Member, 74 Posts

23 June 2010 at 9:24am

Thank you so much, been trying again for the last hour but not quite getting it - but this works perfectly. I'm slowly getting the hang of how these relationships work.

Avatar
DannyC

Community Member, 3 Posts

22 February 2011 at 7:55am

LEGEND!