Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Create DataObject automatically to add images simply.

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

Page: 1
Go to End
Author Topic: 445 Views
  • ChanteLame
    Avatar
    Community Member
    3 Posts

    Create DataObject automatically to add images simply. Link to this post

    Hello,
    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(){
    if(empty($this->ID)){
    $this->ID = $this->write();
    // Reload page here ??
    }
    $fields = new FieldList();
    $imageField = new UploadField('image', 'Illustration');
    $imageField->allowedExtensions = array('jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG');
       $fields->push($imageField);
    return $fields;
    }

    Thanks a lot for your help !

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: Create DataObject automatically to add images simply. Link to this post

    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.

  • ChanteLame
    Avatar
    Community Member
    3 Posts

    Re: Create DataObject automatically to add images simply. Link to this post

    Hello,
    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,

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: Create DataObject automatically to add images simply. Link to this post

    You may also want to take a look at this module https://github.com/frankmullenger/silverstripe-gallery not sure if it deals with saving a data object but it has a sortable has_many images field.

    445 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.