Jump to:

23480 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Passing a value from cmsfield to function?

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

Page: 1
Go to End
Author Topic: 449 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    Passing a value from cmsfield to function? Link to this post

    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

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Passing a value from cmsfield to function? Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Passing a value from cmsfield to function? Link to this post

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

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Passing a value from cmsfield to function? Link to this post

    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

    449 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.