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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Adding Image to DataObject


Go to End


7 Posts   694 Views

Avatar
Bagzli

Community Member, 71 Posts

3 August 2014 at 6:08am

Edited: 03/08/2014 6:09am

Hello,

I am trying to add an image to a DataObject. Basically I have a Work page which has all the videos and videos page has a title, a url and an image to be uploaded. I can't seem to figure out how to add the image. Here is what I have so far:

<?php
class Video extends DataObject {
    private static $db = array(
        'VideoTitle' => 'Varchar',
        'VideoURL' => 'Varchar'
    );
    private static $has_one = array(
        'Work' => 'Work'
    );
}

I also have a Work page which lists all the videos as follows:

<?php
class Work extends Page {
    private static $has_many = array(
        'Videos' => 'Video'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();   
        $config = GridFieldConfig_RelationEditor::create();
        $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            'VideoTitle' => 'Video Title',
            'VideoURL' => "Video URL"
        )); 
        $studentsField = new GridField(
            'Videos',
            'All Videos',
            $this->Videos(),
            $config
        );
        $fields->addFieldToTab('Root.Videos', $studentsField); 
        return $fields;
    }
}
class Work_Controller extends Page_Controller {
}

How can I allow the user to upload the image and in the grid field display the name of the image or even the image itself (which would be way better). Is this possible?

Avatar
geekdenz

Community Member, 37 Posts

3 August 2014 at 10:41pm

Do you want to display just one image per Work page or one image per video in the GridField?

Avatar
Bagzli

Community Member, 71 Posts

4 August 2014 at 5:59am

1 image per video, hence 1 image per grid row.

Avatar
geekdenz

Community Member, 37 Posts

4 August 2014 at 9:21am

Do you want something similar to this?

http://www.silverstripe.org/general-questions/show/20136

Avatar
geekdenz

Community Member, 37 Posts

4 August 2014 at 9:29am

Avatar
Bagzli

Community Member, 71 Posts

4 August 2014 at 12:27pm

That was very helpful. Thank you.

Avatar
geekdenz

Community Member, 37 Posts

4 August 2014 at 12:50pm

Always glad to be of help where I can.