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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

How to replicate ImageDataObjectManager behaviour in SS3?


Reply

5 Posts   1207 Views

Avatar
JonoM

13 September 2012 at 8:47am (Last edited: 13 September 2012 8:48am), Community Member, 103 Posts

Does anyone know how to replicate ImageDataObjectManager behaviour in SS3? I'm referring to the ability to have a class such as this:

class PhotoGalleryImage extends DataObject
{
   static $db = array (
      'Caption' => 'Text'
   );

   static $has_one = array (
      'Image' => 'Image',
      'PhotoGalleryPage' => 'PhotoGalleryPage'
   );
}

Where the focus of the object is on the image relationship, and easily import a bunch of photos at once in to a page like this:

class PhotoGalleryPage extends Page {
   
   static $has_many = array (
      'PhotoGalleryImages' => 'PhotoGalleryImage'
   );
   
   public function getCMSFields() {
      $fields = parent::getCMSFields();

      //What to put here??

      return $fields;
   }

}

Then add captions etc. if necessary afterwards. I tried using GridField but the best I could get to happen is to create one PhotoGalleryImage object at a time - which then has to be saved before adding an image to it. Very time consuming compared to the old way of doing it with ImageDOM and Uploadify!

Thanks for reading

Avatar
zenmonkey

7 December 2012 at 1:36am Community Member, 528 Posts

I just ended up extending the image class with the extra fields and just using the upload field. You upload all the images an hit edit on it to add the extra stuff. Not quite as good as the ImageDOM but its works better than the 1 at a time.

Avatar
JonoM

11 December 2012 at 8:37am Community Member, 103 Posts

Hi zenmonkey, thanks for the reply - any chance you could provide the code you used for this? Sounds like a good solution.

Cheers

Avatar
zenmonkey

12 December 2012 at 5:16am Community Member, 528 Posts

There's two ways to handle this extension either create a new inidividual class or extend the whole image class. I chose the second way since the option could be used on multiple relations.

Here is the code

The image extension

class CustomImageExtension extends DataExtension {
   
   static $db = array(
      'Feature' => 'Boolean',
      'Caption' => 'Varchar',
      'Cover' => 'Boolean'
   );
   
   static $has_one = array(
      'Listing' => 'Listing',
      'Room' => 'Room',
      'MLSListing' => 'MLSListing'
   );
   
   public function updateCMSFields(FieldList $fields) {
      $fields->addFieldToTab('Root.Main', new TextField('Caption', 'Caption'), 'Name');
      $fields->addFieldToTab('Root.Main', new CheckboxField('Feature', 'Is Feature'), 'Name');
      $fields->addFieldToTab('Root.Main', new CheckboxField('Cover', 'Is Cover'), 'Name');
      $fields->removeByName('OwnerID');
      $fields->removeByName('ParentID');
      $fields->removeByName('BackLinkCount');
   }
   
}


Then in your config add
Object::add_extension('Image', 'CustomImageExtension');

This adds those fields to all Image Objects, the indidual class option instead of extending DataExtension you'd extend the Image, forget about the line in your config and on your dataobject call the relation to CustomImage instead of Image. Really depends on the functionality you need. I was really just adding a few booleans so I could pull feature image into carousels on parent pages, as I need this on almost every instance of image I just added to the Image Class

Hope that helps

Avatar
JonoM

12 December 2012 at 5:57am Community Member, 103 Posts

Thanks man!