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.

Blog Module

Discuss the Blog Module.

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

displaying blog entry with image in homepage


Reply

8 Posts   3258 Views

Avatar
Rishi

15 January 2010 at 11:43pm Community Member, 97 Posts

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

16 January 2010 at 1:40am (Last edited: 16 January 2010 1:40am), Community Member, 288 Posts

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

23 June 2010 at 4:29am Community Member, 69 Posts

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

23 June 2010 at 4:41am Community Member, 5 Posts

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

23 June 2010 at 5:21am (Last edited: 23 June 2010 5:22am), Community Member, 69 Posts

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

23 June 2010 at 9:09am Community Member, 288 Posts

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

23 June 2010 at 9:24am Community Member, 69 Posts

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

22 February 2011 at 7:55am Community Member, 3 Posts

LEGEND!