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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Multiple Images Using DataObjectManager and DataObjects as pages


Go to End


2 Posts   2155 Views

Avatar
ShadeFrozen

Community Member, 10 Posts

5 August 2013 at 2:11am

Edited: 05/08/2013 2:35am

Hi,

I have everything working satisfactorily using SS 2.4 (and am about to implement the UploadField Patch) but I do have one question about the below code. Currently I can only attach one image at a time to a news item. So my question is; how to attach multiple images to a single news item in one action:

NewsListingPage.php
------------------------------------

<?php
/**
* Defines the News Listing Page type
*/
class NewsListingPage extends DataObjectAsPageHolder
{

}

class NewsListingPage_Controller extends DataObjectAsPageHolder_Controller
{
//The Class of the DataObject you want this page to list
static $item_class = 'NewsItem';
//Set the sort for the items (defaults to Created DESC)
static $item_sort = 'Title ASC';
}

?>

NewsItem.php
-------------------------

<?php
/**
* Defines the NewsItem DataObject type
*/
class NewsItem extends DataObjectAsPage {

//The class of the page which will list this DataObject
static $listing_class = 'NewsListingPage';

//Class Naming (optional but recommended)
static $plural_name = 'News Items';
static $singular_name = 'News Item';

//Relations
//add the image(s)
static $has_many = array (
'NewsImages' => 'NewsImage'
);

public static $db = array (
);

public static $has_one = array (
);

//Popup window for adding/editing
public function getCMSFields_forPopup() {
$fields = parent::getCMSFields_forPopup();
//add the (image) DataObject
$manager = new ImageDataObjectManager(
$this, // Controller
'NewsImages', // Source name
'NewsImage', // Source class
'MyNewsImage', // File name on DataObject
array('NewsImageTitle' => 'NewsImageTitle'), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);
$fields->addFieldToTab('Root.Content.NewsImage', $manager);
return $fields;

}

}

?>

NewsImage.php
-----------------------

<?php

class NewsImage extends DataObject
{
static $db = array (
'NewsImageTitle' => 'Varchar(100)'
);
static $has_one = array (
'MyNewsImage' => 'Image', //relation needed for the DataObject
'BelongToEventPage' => 'NewsItem' //relation pointing to pagetype/dataobject.
);

public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('NewsImageTitle','News Image Title'),
new ImageUploadField('MyNewsImage')
);
}

}

?>

Any suggestions and/or improvements are welcomed.

***********

TIA
Mark

Avatar
ShadeFrozen

Community Member, 10 Posts

6 August 2013 at 12:52am

Any ideas people? Uncle Cheese did suggest this was by design and I needed to either extend the NewsImage class with extra_images OR use the normal DOM and not the imageDOM but I cannot figure it out.

To be quite honest I am trying to get my head around this as I don't understand why the above isn't working properly. And yes it It works, but as I said, only by adding one image at a time.