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

Adding blog image to blog module in silverstripe 3.1


Go to End


2 Posts   2384 Views

Avatar
nmshah

Community Member, 21 Posts

5 June 2013 at 2:33am

I am trying to add image to blogs in silvertstripe 3.1 with the help of the following code:

<?php 
class BlogEntryDecorator extends DataExtension {

   static $has_one = array( 
      'BlogImage' => 'Image' 
   );

   public function updateCMSFields(FieldList $fields) { 
      parent::updateCMSFields($fields); 
      $fields->addFieldToTab("Root.Main", $BlogImage = new UploadField('BlogImage'), 'Content'); 
      $BlogImage->getValidator()->setAllowedExtensions(File::$app_categories['image']); 
   }

} 
?>

<?php 
#------------------------------------------------------------- 
# Edit file: mysite/code/_config.php 
#------------------------------------------------------------- 
Object::add_extension('BlogEntry', 'BlogEntryDecorator'); 
?>

When I try to build database again, I get an error.

On further research I realized Object::add_extension is deprecated and I have to use something like the following

BlogEntry:
  extensions:
    - BlogEntryDecorator

I am not sure about the above code & which file should I put the same in.
Kindly suggest

Avatar
Matty Balaam

Community Member, 74 Posts

9 June 2013 at 7:56am

Edited: 09/06/2013 8:02am

You need to put the new code into a file called config.yml.

If you have the existing code in your mysite folder, this should be located in a folder in mysite called _config

Another way you could do this would be to create a mini-module.

File structure would be:


your-site-root {top level folder}

BlogEntryDecorator {second level folder}

_config.php {third level file}

_config {third level folder}
config.yml {forth level file}

code {third level folder}
BlogEntryDecorator.php  {forth level file}

The advantage of using the second method is that you can then copy that folder easily into another site.