Jump to:

1851 Posts in 1606 Topics by 561 members

Blog Module

SilverStripe Forums » Blog Module » Trouble extending blogentry page with Title Image field

Discuss the Blog Module.

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

Page: 1
Go to End
Author Topic: 1140 Views
  • Kakedis
    Avatar
    Community Member
    2 Posts

    Trouble extending blogentry page with Title Image field Link to this post

    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
  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Trouble extending blogentry page with Title Image field Link to this post

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

    $TitleImage

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

  • CHD
    Avatar
    Community Member
    218 Posts

    Re: Trouble extending blogentry page with Title Image field Link to this post

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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Trouble extending blogentry page with Title Image field Link to this post

    ^^ 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

  • Pix
    Avatar
    Community Member
    135 Posts

    Re: Trouble extending blogentry page with Title Image field Link to this post

    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!

  • Pix
    Avatar
    Community Member
    135 Posts

    Re: Trouble extending blogentry page with Title Image field Link to this post

    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.

    1140 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.