Change default select for CMS LeftAndMain

Silverstripe Version: 3

How to change default select for page holder in CMS?

Problem is as follows: client has some articles that will have three custom statuses as HouseStatus (sold, in sale and booked). If client changes article status to one of those, the article must “move” to corresponding article holder. I found the extension suggestion
here, but it wont load. Any ideas/suggestions?

Maybe you can use an onBeforeWrite() like

protected function onBeforeWrite(){
    if ($this->isChanged('HouseStatus') && $this->HouseStatus == 'sold') {
        $soldPage = Page::get()->filter('URLSegment', 'sold')->first();
        $this->setParent($soldPage);
    }
    parent::onBeforeWrite();
}
1 Like

Hi JonoM!

First of all I want to thank you for your quick response and input. I managed to do what I needed with your help!

I ended up with following solution!

class ArticlePage extends Page {
    static $allowed_children = 'none';
...
    public function getCMSFields() {
...
    }

    public function onBeforeWrite() {
        if ($this->isChanged('HouseStatus') && $this->HouseStatus == 'Sold') {
            switch ($this->HouseStatus){
                case "Sale":
                    $soldPage = Page::get() -> filter('ID', '111') -> first();
                    break;
                case "Booked":
                    $soldPage = Page::get() -> filter('ID', '112') -> first();
                    break;
                case "Sold":
                    $soldPage = Page::get() -> filter('ID', '113') -> first();
                    break;
            };
            $this->setParent($soldPage);
        }
        parent::onBeforeWrite();
    }
}

Just for info, if you know the IDs of the page, you can use Page::get()->byID(111); instead of using a filter() and first()

You’re welcome @unoteu :slight_smile:

Just FYI - it looks like there’s a logical problem in the final code you posted. The first if block will only be executed if $this->HouseStatus == 'Sold', so the first two cases in your switch block are redundant, and the last will fire every time. If you take out the && $this->HouseStatus == 'Sold' part from your if block I think it will work as intended.

Hi all!

Thanks for inputs! Sadly I posted the solution before I “fixed” it even more. The end code I went with differs a lot from the code I posted (including get id, setParents part and logic bug for sold part). But cause I’m newbie, Admins review my posts and as long as the posts are not reviewed, I cannot change them :slight_smile:
Here is the actual code then:

class ArticlePage extends Page {
    static $allowed_children = 'none';
    ...
    public function getCMSFields() {
        ...
    }

    public function onBeforeWrite() {
         if ($this->isChanged('HouseStatus')) {
             switch ($this->HouseStatus){
                 case "Sale":
                    //$parentPage = Page::get() -> filter('URLSegemnt', 'for-sale') -> first();
                     $this -> setParent('111');
                     break;
                 case "Booked":
                     //$parentPage = Page::get() -> filter('URLSegemnt', 'booked') -> first();
                     $this -> setParent('112');
                     break;
                 case "Sold":
                     //$parentPage = Page::get() -> filter('URLSegemnt', 'sold-out') -> first();
                     $this -> setParent('113');
                     break;
             };
             //$this->setParent($parentPage);
         }
         parent::onBeforeWrite();
     }
 }

Basically as You can see, I did both solution (whichever is needed in the future).

Thanks again!