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