I'm trying to associate multiple images with the basic page type "Page" using FileDataObjectManager. Below are my files:
ImageAttachment.php
-------------
<?php
class ImageAttachment extends DataObject
{
static $db = array (
'Name' => 'Text',
'URL' => 'Text'
);
static $has_one = array (
'Attachment' => 'File',
'Page' => 'Page'
);
public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Name'),
new TextField('URL'),
new FileIFrameField('Attachment')
);
}
}
?>
-------------
Page.php
-------------
<?php
class Page extends SiteTree {
public static $db = array(
);
public static $has_one = array(
'HeaderImage' => 'Image'
);
static $has_many = array (
'ImageAttachments' => 'ImageAttachment'
);
public function getCMSFields()
{
$f = parent::getCMSFields();
$f->addFieldToTab("Root.Content.HeaderImage", new ImageField('HeaderImage'));
$manager = new FileDataObjectManager(
$this, // Controller
'ImageAttachments', // Source name
'ImageAttachment', // Source class
'Attachment', // File name on DataObject
array(
'Name' => 'Name',
'URL' => 'URL'
), // Headings
'getCMSFields_forPopup', // Detail fields (function name or FieldSet object)
"PageID = {$this->ID}" // Filter clause
// Sort clause
// Join clause
);
$manager->setParentClass('Page');
// If undefined, all types are allowed. Pass with or without a leading "."
$manager->setAllowedFileTypes(array('jpg','gif','swf'));
// Label for the upload button in the popup
$manager->setBrowseButtonText("Upload (JPG, GIF or SWF)");
// In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
$manager->setGridLabelField('Name');
// Plural form of the objects being managed. Used on the "Add" button.
// If left out, this defaults to [MyObjectName]s
$manager->setPluralTitle('Images');
$f->addFieldToTab("Root.Content.Images", $manager);
return $f;
}
}
class Page_Controller extends ContentController {
...
}
?>
-------------
The filter clause "PageID = {$this->ID}" was added to ensure that only the ImageAttachments that have PageID set to the current page ID are actually displayed in the list, instead of all images which have been added.
Now, this works. However, when you actually attempt to add an attachment, PageID is not set to the current page ID. It's left as '0'. Only when I change the PageID directly in the database (e.g. using phpMyAdmin) can I actually see the filter "PageID = {$this->ID}" working.
Incidentally, line $manager->setParentClass('Page'); does not seem to make any difference.
In short, how do you go about using FileDataObjectManager to associate multiple images with page type "Page" while showing only those images with PageID set to the current page?