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

Trouble extending blogentry page with Title Image field


Go to End


6 Posts   2077 Views

Avatar
Kakedis

Community Member, 2 Posts

19 February 2011 at 1:54pm

Hi,

I'm a newbie to silverstripe and by no means a developer. I was playing around with the blog module and trying to add an image to my blog entries, much like I would add a title to a post on the blogentry page through the backend (admin section). Seeing that I'm no developer I trolled the various blog entries on the blog module trying to find a suitable solution for my conundrum; This is how far I got;

Firstly I created a php file called; BlogEntryDecorator under the ....blog\code directory. I placed the following code in the page;

<?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'); 
   } 
} 
?>

After creating the php file I pasted the following snippet into the _config under the ....\mysite directory

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

Great!So after I applied the two changes I was able upload a image through a Image Title field for a BlogEntry page through the admin section. Everything seemed to work well. Now, all it seems I had to do was to reference the image field in the template pages by inserting the $ImageTitle variable (and I think this is where I might have gone, due to my lack of programming experience). I added the referenced the $ImageTitle variable in the BlogEntry.ss page under the ...themes\simplestripe\templates\Layout directory in the following manner;

	<h1>$Parent.Title</h1>
		<article>
                      <p class="ImageTitle">$ImageTitle</p>
          <hgroup class="BlogTitle">

However, after refreshing the page I was still not able to see the image or any kind of reference of the image on the page. This led me to believe that I did something wrong :) and I came to the following conclusion;
1. Either I'm referencing the $ImageTitle variable incorrectly or I reference in the variable in the wrong .ss page.
2. I should not have created a separate BlogEntryDecorator php for the field definition.
3. Neither of the above and I'm totally off the path and shouldn't even have tried in the first place.

Can someone help me figuring out where I've gone wrong?

Thanks in advance

Attached Files
Avatar
Ryan M.

Community Member, 309 Posts

19 February 2011 at 4:24pm

The variable name needs to match what you have coded. =)

$TitleImage

And, move your custom decorator to mysite/code/.

Avatar
CHD

Community Member, 219 Posts

31 May 2011 at 2:44pm

why didnt you just add the new field directly into the blogEntry.php file rather than extending it?

Avatar
Invader_Zim

Community Member, 141 Posts

31 May 2011 at 9:33pm

^^ This way (by extending) ensures an easy way for updates,
without the need to edit BlogEntry.php again if a new version appears.

Cheers
Christian

Avatar
Pix

Community Member, 158 Posts

14 June 2012 at 4:34am

Edited: 14/06/2012 5:31am

I swear I can't get this to work at all. I even tried the tutorial here:

http://www.ssbits.com/tutorials/2011/adding-an-image-to-a-blog-post-using-decorators-and-the-silverstripe-blog-module/?f=123

Either way after I create the decorator and add it in the config, my home page turns to:

"Sorry, there was a problem with handling your request.
Visit www.silverstripe.org to download the CMS"

If I remove the extension I am up and running again. Is there something about this code that would be incompatible with SS 2.4.5?

I would be really grateful if someone could post for download a BlogEntryDecorator.php that does not blow up a 2.4.5 site, that would be awesome!

Thanks for any help!

Avatar
Pix

Community Member, 158 Posts

14 June 2012 at 6:47am

Nevermind, problem solved duh I was not rebuilding the Database.

However, I'm pretty sure I rebuilt it while tring the code from the tutorial and still did not work. I am not an SS expert, but I noticed the code in the tutorial uses:
class BlogEntryDecorator extends SiteTreeDecorator

whereas here it is using:
class BlogEntryDecorator extends DataObjectDecorator

doesn't seem like the firs one is quite right? Anyway, problem solved.