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.

Data Model Questions /

[Solved] UploadField not working in DataObject / Gridfield Image Gallery


Reply


6 Posts   1483 Views

Avatar
Optic Blaze

Community Member, 165 Posts

15 October 2012 at 8:34pm

Edited: 15/10/2012 8:43pm

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;
   }
}

Avatar
simon_w

Forum Moderator, 474 Posts

15 October 2012 at 9:48pm

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')".

Avatar
Optic Blaze

Community Member, 165 Posts

15 October 2012 at 10:01pm

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.

Avatar
Optic Blaze

Community Member, 165 Posts

16 October 2012 at 12:32am

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?

Avatar
Optic Blaze

Community Member, 165 Posts

16 October 2012 at 12:59am

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

Avatar
Optic Blaze

Community Member, 165 Posts

16 October 2012 at 1:33am

Ok tested it and it works