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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to associate multiple testimonials (created via ModelAdmin) with a page?


Go to End


6 Posts   1379 Views

Avatar
Turismo

Community Member, 28 Posts

19 May 2016 at 3:36am

Edited: 23/05/2016 9:17pm

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;
    }
}

Avatar
Turismo

Community Member, 28 Posts

23 May 2016 at 9:56pm

Managed to solve this by changing the testimonials call in the class to:

$fields->addFieldToTab('Root.Testimonials', new CheckboxSetField(
	'Testimonials',
	'Selected testimonials',
	Testimonial::get()->map('ID', 'Title'))
);

Only minor preference would be to make this a ListboxField as opposed to CheckboxSetField, but how do I modify the above to achieve this? Obviously tried changing the CheckboxSetField reference to ListboxField, but this seemed to output as a dropdown, as if it were a $has_many, not $many_many.

Avatar
dhensby

Community Member, 253 Posts

24 May 2016 at 2:00am

Edited: 24/05/2016 2:03am

`ListBoxField` has a common ancestor with `CheckboxsetField` (`DropDownField`), so you should be able to drop in `ListBoxField` where you have `CheckboxSet`

Avatar
Turismo

Community Member, 28 Posts

24 May 2016 at 2:14am

Tried changing it to the below, but still not working. I get the 'Sorry, there was a problem handling your request.' message:

$fields->addFieldToTab('Root.Testimonials', new ListBoxField(
	'Testimonials',
	'Selected testimonials',
	Testimonial::get()->map('ID', 'Title')->toArray()
));

Avatar
martimiz

Forum Moderator, 1391 Posts

24 May 2016 at 2:25am

Edited: 24/05/2016 2:28am

setMultiple should do the trick:

        $fields->addFieldToTab(
            'Root.Main',
            ListboxField::create(
                'Testimonials',
                'Testimonials',
               Testimonial::get()->map('ID', 'Name')->toArray()
            )
            ->setMultiple(true) ,
            'Content'
        );

[EDIT]: saw you already noticed the toArray() bit :)

You are using 'Name' though, not 'Title'

Avatar
Turismo

Community Member, 28 Posts

24 May 2016 at 2:35am

This works perfect, thanks martimiz!