Can someone please advise how I associate multiple testimonials (created via ModelAdmin) with a page. Below is the code for the page class that the testimonials should be added to (mysite/code/ServicePage.php). This shows a testimonial ListboxField, but it's blank and provides no records to add, even though I've created two via the ModelAdmin:
class ServicePage extends Page {
private static $db = array (
'Intro' => 'Text',
'USPStripIntro' => 'Varchar(255)'
);
private static $has_one = array (
'Banner' => 'Image'
);
private static $has_many = array (
'Sections' => 'ServiceSection',
'USPs' => 'ServiceUsp'
);
private static $many_many = array (
'Testimonials' => 'Testimonial'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', TextareaField::create('Intro','The introduction for the page'),'Content');
$fields->addFieldToTab('Root.Main', TextField::create('USPStripIntro','USP Intro Text'),'Content');
$fields->addFieldToTab('Root.Attachments', $banner = UploadField::create(
'Banner',
'The banner image for the page'
));
$fields->addFieldToTab('Root.Sections', GridField::create(
'Sections',
'Sections for this page',
$this->Sections(),
GridFieldConfig_RecordEditor::create()
));
$fields->addFieldToTab('Root.USPs', GridField::create(
'USPs',
'USPs for this page',
$this->USPs(),
GridFieldConfig_RecordEditor::create()
));
$fields->addFieldToTab('Root.Main', ListboxField::create(
'Testimonials',
'Selected testimonials',
$this->Testimonials()->map('ID','Name')
),'Content');
$banner
->setFolderName('page-banners')
->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png'));
return $fields;
}
}
class ServicePage_Controller extends Page_Controller {
}
And here's my code for the Testimonials, which are created via a ModelAdmin (mysite/code/TestimonialAdmin.php):
class Testimonial extends DataObject {
private static $db = array (
'Name' => 'Varchar',
'Role' => 'Varchar',
'Company' => 'Varchar',
'Testimonial' => 'Text'
);
private static $belongs_many_many = array (
'Pages' => 'ServicePage'
);
public function getCMSfields() {
$fields = FieldList::create(TabSet::create('Root'));
$fields->addFieldsToTab('Root.Main', array(
TextField::create('Name','Client Name'),
TextField::create('Role'),
TextField::create('Company'),
TextareaField::create('Testimonial')
));
return $fields;
}
}