Search code examples
phpuploadifysilverstripe

Attach Image to Page with Uploadify in Silverstripe CMS


We want to use the Uploadify module so Silverstripe CMS users can simply upload images that are then attached to Pages. At the basic level this is the code:

class Page extends SiteTree {

       static $has_many = array( 
         "PageImages" => "PageImage" 
      );

       function getCMSFields(){ 
          $fields = parent::getCMSFields(); 
          $fields->addFieldToTab("Root.Content.PageImages", new MultipleFileUploadField('PageImages','Add Images to Page'));       
          return $fields; 
       } 
    }

    class PageImage extends Image { 

       static $has_one = array( 
          "Page" => "Page" 
       ); 

    }

However when a file is Uploaded through the "Upload New" tab it isn't automatically attached to the page. We thought this would be the default behaviour.

Instead CMS users have to click on the "Choose existing" tab and select/ Import the images they are after.

I'm guessing we've missed something very very simple, any help would be appreciated.


Solution

  • Do you really extend the image itself? It might be possible, but I've always used a DataObject instead. So Page has_many PageImages, PageImage has_one Page and PageImage has_one Image.

    See also http://deadlytechnology.com/silverstripe/silverstripe-image-gallery/ or https://github.com/xeraa/silverstripe-book/tree/master/chapter-07/module_gallery/code for complete examples. Note: Both use the DataObjectManager module.

    And I second ryanwachtl's suggestion to split up the file (if you haven't done so and this is merely some styling issue on stackoverflow).