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.

Customising the CMS /

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

Multiple many_many as checkboxsetfields


Go to End


879 Views

Avatar
SnowB1

Community Member, 11 Posts

17 June 2015 at 4:22am

Hello all,

I have a page type which has two different many_many's. Both of them have the $belong_many_many back to the page type. I display both of them as checkboxsetfields on different tabs. The first one works as expected, where the second one will save to the database and show on the front end but the checkboxes do not stay as checked so on the next save it removes all checked items. Some items that might be important: This is a multilingual site and the images are set on the English side and pulled through so as to only upload the images one time. I have included the code below and any help is greatly appreciated.

/* DataSheetPage */

<?php
class DataSheetPage extends Page {
	/**/
	private static $description = "Data Sheet Style Page";
	/**/
	private static $db = array(
	);
	/**/
	private static $many_many = array(
		'ApplicationImages' => 'ApplicationImage',
		'SpecificationImages' => 'SpecificationImage'
	);
...
	/**/
	public function getCMSFields() {
		$fields = parent::getCMSFields();

		//TABS
		$fields->findOrMakeTab("Root.RAImages", "Recommended Applications");
		$fields->findOrMakeTab("Root.SpecImages", "Specification Images");

/* This is the one working */

		//APPLICATION IMAGES
		$config = SiteConfig::get()->filter(array("Locale"=>"en_US"))->First();
		$ApplicationImages = ApplicationImage::get()->filter(array('SiteConfigID'=>$config->ID));
		$appImageMap = $ApplicationImages->map('ID', 'ApplicationTitle_'.$this->Locale);
		$fields->addFieldToTab('Root.RAImages', new CheckboxsetField('ApplicationImages', 'Application Images', $appImageMap));

/* This one is not functioning properly */

		//SPECIFICATION IMAGES
		$config = SiteConfig::get()->filter(array("Locale"=>"en_US"))->First();
		$SpecificationImages = SpecificationImage::get()->filter(array('SiteConfigID'=>$config->ID));
		$specImageMap = $SpecificationImages->map('ID', 'Title');
		$fields->addFieldToTab('Root.SpecImages', new CheckboxsetField('SpecificationImages', 'Specification Images', $specImageMap));

	....
		return $fields;
	}

/* ApplicationImage */
<?php
/**/
class ApplicationImage extends DataObject {
	/**/
	static $singular_name = 'Application Image';
  	static $plural_name = 'Application Images';
	/**/
	private static $db = array(
		'ApplicationTitle_en_US' => 'Text',
		'ApplicationTitle_es_ES' => 'Text'
	);
	/**/
	private static $has_one = array(
		'SiteConfig' => 'SiteConfig',
		'ApplicationImage' => 'Image'
	);
	/**/
	private static $belongs_many_many = array(
		'DataSheetPages' => 'DataSheetPage'
	);
	/**/
	private static $summary_fields = array(
		"ApplicationImage.CMSThumbnail" => "Image",
		'ApplicationTitle_en_US' => 'Application Title'
	);
	/**/
	private static $default_sort = 'ApplicationTitle_en_US';
	/**/
	public function getCMSFields() {

		//APPLICATION IMAGE
			$ApplicationImage = new UploadField(
				$name = 'ApplicationImage',
				$title = 'Upload a Recommended Application Image'
			);
			$ApplicationImage->setFolderName('ApplicationImages');
			$ApplicationImage->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png','gif'));
			
		return new FieldList(
			new TextField('ApplicationTitle_en_US', 'English Application Image Title'),
			new TextField('ApplicationTitle_es_ES', 'Spanish Application Image Title'),
			$ApplicationImage
		);
		
	}
}

/* Specification Image */

<?php
/**/
class SpecificationImage extends DataObject {
	/**/
	static $singular_name = 'Specification Image';
  	static $plural_name = 'Specification Images';
	/**/
	private static $db = array(
		'Title' => 'Text'
	);
	/**/
	private static $has_one = array(
		'SiteConfig' => 'SiteConfig',
		'SpecificationImage' => 'Image'
	);
	/**/
	private static $belongs_many_many = array(
		'DataSheetPages' => 'DataSheetPage'
	);
	/**/
	private static $summary_fields = array(
		"SpecificationImage.CMSThumbnail" => "Image",
		'Title' => 'Title'
	);
	/**/
	private static $default_sort = 'Title';
	/**/
	public function getCMSFields() {

		//RECOMMENDED IMAGE
			$SpecificationImage = new UploadField(
				$name = 'SpecificationImage',
				$title = 'Upload a Specification Image'
			);
			$SpecificationImage->setFolderName('SpecificationImages');
			$SpecificationImage->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png','gif'));

		return new FieldList(
			new TextField('Title', 'Specification Image Title'),
			$SpecificationImage
		);

	}
}