First you must create a decorator for the blog entries.
1. Create a file in mysite/code called: BlogEntryDecorator.php and paste the following code:
class BlogEntryDecorator extends SiteTreeDecorator {
function extraStatics() {
return array(
'has_one' => array(
'AttachedImage'=>'Image'
)
);
}
function updateCMSFields(& $fields){
$fields->addFieldToTab('Root.Content.Image', new ImageField('AttachedImage','Attach an image to this blog entry'));
}
}
2. Add the following line to your _config.php:
DataObject::add_extension('BlogEntry', 'BlogEntryDecorator');
3. Add your code to BlogHolder.php file (after the line 231 and before line 240):
new FileUploadField('AttachedImage', 'Upload an image', array ( 'buttonText' => 'Upload an Image')),
you can use ImageUploadField instead of FileUploadField (since we want to upload only images):
new ImageUploadField('AttachedImage', 'Upload an image', array ( 'buttonText' => 'Upload an Image')),
4. Add the following code to your templates (BlogSummary.ss, BlogEntry.ss, etc.)
<% if AttachedImage %>
$AttachedImage.CroppedImage(width-value,height-value)
<% end_if %>
5. Before you try, visit your-site.com/dev/build?flush=all
And you're done!