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.

Customising the CMS /

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

Create DataObject automatically to add images simply.

Go to End

4 Posts   1094 Views


Community Member, 3 Posts

3 December 2012 at 12:34am

Edited: 03/12/2012 12:51am

I am facing a recurrent problem using Silverstripe, and I was wondering if you have a magic solution for me.
Everytime I have a DataObject with images, I must to be sure the current DataObject has not an empty ID. It forces my customer to click on the button "create" first, and then add images.

So I want to perform a create action immediatly on the click of the "add" GriedField button, and redirect then the customer to the edition of the DataObject.

For now, I try to call the write() method in the getCMSFields() when it has an empty ID. But It's doesn't work, and it's ugly. Do you have any idea how to do this ? I am working with Silverstripe v3.0.2.

    public function getCMSFields(){
            $this->ID = $this->write();
            // Reload page here ??
        $fields = new FieldList();
        $imageField = new UploadField('image', 'Illustration');
        $imageField->allowedExtensions = array('jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG');
        return $fields;

Thanks a lot for your help !


Community Member, 545 Posts

7 December 2012 at 1:54am

Edited: 07/12/2012 2:03am

I usually take the reverse approach and only show the field if the object has an ID. That way the customer can't try to add images until its saved. I believe a solution is being worked on for 3.1 that would cache all the object creation and fire all them on save.

The other option depending on the relation between the DataObject and image is to overload or extend the image class to add the fields to the image itself. That way the client would upload the image and fill I the fields on the image instead of the DataObject. I the DataObject is only there to store extra data for the image itself (ie it's a gallery image that needs a caption or something like that) it makes much sense for the user upload the image and add the caption than create an object with title and caption and add an image to it.


Community Member, 3 Posts

3 January 2013 at 6:03am

Sorry for the late answer, I put my problem to a lower priority :/

In fact I have no answer for now.
I try to customise the AddNewButton of gridfield to create an instance and go directly to the edition page.
My goal is to do a gallery DataExtention, to make my code much clearer. So I cannot extends Image (and it's a non-sense to have a News extends Image... ).

My customer doesn't want to create first and add image then. And I totally agree : when you click on "add a news", you explicitally said : "I want to add a news", so why a creation phase ?

So, if someone have already done such modifications in GriedField actions, please let me know. I will post my answer if I succeed.
Thanks a lot,


Community Member, 545 Posts

3 January 2013 at 6:38am

You may also want to take a look at this module not sure if it deals with saving a data object but it has a sortable has_many images field.