Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved] UploadField not working in DataObject / Gridfield Image Gallery

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

Page: 1
Go to End
Author Topic: 1382 Views
  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    [Solved] UploadField not working in DataObject / Gridfield Image Gallery Link to this post

    Hi,

    I am trying to build a image gallery. So far i have 2 files. ImageGalleryPage.php with a $has_many relationship with ImageGalleryEntry.php. As far as i can see, i have set the relationships up correctly, but for some reason when i try to upload a image using gridfield it does not attach the image correctly. Also when i try to use existing files in the cms i get an error message that says 'Forbidden'.

    Everything else works 100%, i just can get the image to attach to the record. Here is the code:

    -----------------------------------------------------------
    ImageGalleryPage.php
    ------------------------------------------------------------

    <?php
    class ImageGalleryPage extends Page {

       public static $db = array(                  
       );

       public static $has_many= array(
       'ImageGalleryEntries'=>'ImageGalleryEntry'                     
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $gridFieldConfig = GridFieldConfig::create()->addComponents(
             new GridFieldToolbarHeader(),
    new GridFieldAddNewButton('toolbar-header-right'),
             new GridFieldSortableHeader(),
             new GridFieldFilterHeader(),
             new GridFieldDataColumns(),
             new GridFieldPaginator(20),
             new GridFieldEditButton(),
             new GridFieldDeleteAction(),
             new GridFieldDetailForm()
          );

          $gridfield = new GridField("ImageGalleryEntries", "Gallery Images:", $this->ImageGalleryEntries(), $gridFieldConfig);
          $fields->addFieldToTab('Root.ImageGallery', $gridfield); // add grid field to tave
          return $fields;
       }
    }

    -----------------------------------------------------------
    ImageGalleryEntry.php
    -----------------------------------------------------------

    <?php
    class ImageGalleryEntry extends DataObject {

       public static $db = array(
       'Title'=>'Varchar(255)',               
       'Description'=>'Text',
       'Sort' => 'Int',
       'Link' => 'Text',
       );

       public static $has_one = array(
       'ImageGalleryPage'=>'ImageGalleryPage',
       'GalleryImage'=>'Image'
       );

       function getCMSFields() {
       
       $fields = new FieldList (   
       new TextField('Title', 'Image Title'),
       new TextField('Description', 'Description'),
       new TextField('Sort', 'Sort Order'),
       new TextField('Link', 'Image Link'),
       new UploadField('GalleryImage', 'Gallery Image', $this->GalleryImage())
       );
       return $fields;
       }
    }

  • simon_w
    Avatar
    Forum Moderator
    474 Posts

    Re: [Solved] UploadField not working in DataObject / Gridfield Image Gallery Link to this post

    From what I can tell, your problem is with the "new UploadField('GalleryImage', 'Gallery Image', $this->GalleryImage())" line. The third argument to an UploadField expects a SS_List, where you're passing in a GalleryImage object. It should be able to automatically detect what to save into, so you should just be able to use "new UploadField('GalleryImage', 'Gallery Image')".

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [Solved] UploadField not working in DataObject / Gridfield Image Gallery Link to this post

    Thanks for the reply.

    Tried what you suggested. Still does not work. Nothing attaches and when i use an image from the filestore i get 'Forbidden' as an error message.

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [Solved] UploadField not working in DataObject / Gridfield Image Gallery Link to this post

    Ok i found something interesting.

    When using the existing code i can add a GalleryImage using the gridfield only when i have already saved the record. In other words i click on 'Add Image Gallery Item' in the CMS, then fill in the Title, Description etc (everything except uploading or attaching the photo). If i then create the record with the aforementioned info and then after it is created try to upload or attach a image...then it works!?

    So it is almost asif it needs the GalleryImage class to be created first before the $has_one relationship with 'Image' starts to work.

    Just for interest sake...i tried making use of $many_many relationships as well...they all yielded the same results.

    Any ideas?

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [Solved] UploadField not working in DataObject / Gridfield Image Gallery Link to this post

    Ok found a couple of more forum posts on the matter. It seems currently you have to first create the record before you can attach/upload a image.

    Here is another threads that deals with the issue:

    http://www.silverstripe.org/general-questions/show/20653
    http://www.silverstripe.org/general-questions/show/20924
    http://www.silverstripe.org/form-questions/show/20225

    Here is a gallery plugin based on the answers in the threads above:
    http://www.silverstriperesources.com/modules/silvestripe-3-gallery-plugin-module/

    Will test and let you know

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [Solved] UploadField not working in DataObject / Gridfield Image Gallery Link to this post

    Ok tested it and it works

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