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

Passing a value from cmsfield to function?


Go to End


4 Posts   807 Views

Avatar
Harley

Community Member, 165 Posts

8 August 2012 at 2:36am

Hi people,

I wonder if someone can give me some pointers here. I have a project which uses the 'Subsites' module. The challenge for me is that there is a possiblity of duplicate content required in one of the sections for one of the subsites and what I am trying to achieve is a dropdown in the cms which allows users to select which subsite the page they are editing belongs to. So in short they can duplicate a page and its children and then select from the dropdown another subsite for that dupicate to sit in. Simple?

... well almost, I've come quite far but I'm a little bit stumped on how I can use the data from the updateCMSFields to populate the SQL Update. For now in the code I have marked it 'somethingneededhere' in the sql for people to see. I want the value from the DropdownField to appear here...

<?php

class SubsiteSelector extends DataObjectDecorator {

	function getMyObjectOptions(){
		if($Subsite = DataObject::get("Subsite")){
			return $Subsite->map("ID", "Title", "Please select");
		} else {
			return array("No objects found");
		}
	}

	//CMS fields
	function updateCMSFields(FieldSet &$fields) {
		$fields->addFieldToTab("Root.Behaviour", 
			new DropdownField(
				'Subsites', 
				'Change subsite this page belongs to', 
				$this->getMyObjectOptions(),
				Session::get('SubsiteID')
			));
		}

	// update which subsite page belongs to
	function onAfterWrite(){
		parent::onAfterWrite();

		DB::query('UPDATE `sitetree` SET `SubsiteID` = "somethingneededhere" WHERE `ID` = '.$this->owner->ID.'');
	}
}

Can anyone enlighten me on this please?

Regards

Avatar
Harley

Community Member, 165 Posts

9 August 2012 at 7:19am

Apologies for being one of those "bump" dudes but I'm going nuts on this one. Does anybody out there have any advice on how to overcome this problem?

Regards

Avatar
(deleted)

Community Member, 473 Posts

9 August 2012 at 3:26pm

Why not just name the DropdownField SubsiteID, so that the ORM handles it for you?

Avatar
Harley

Community Member, 165 Posts

9 August 2012 at 11:47pm

Thanks Simon,

I think I've been looking at code for too long and gone mental, why didn't I see this, it was obvious! Thank you, got it working fine now!

... Well almost! All except a page's children. Ideally it would be nice to be able to update the children at the same time. Is there a built in way to cascade changes to children too or would my original idea be needed where an sql query is requried to update any records where ParentID = ID?

Regards