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.

General Questions

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

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

Passing a value from cmsfield to function?


Reply

4 Posts   457 Views

Avatar
Harley

8 August 2012 at 2:36am Community Member, 153 Posts

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

9 August 2012 at 7:19am Community Member, 153 Posts

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
simon_w

9 August 2012 at 3:26pm Forum Moderator, 474 Posts

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

Avatar
Harley

9 August 2012 at 11:47pm Community Member, 153 Posts

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