Semicolon
Semicolon

Reputation: 1914

SilverStripe best practice regarding pagetypes

Imagine you have two pagetypes: Page and HomePage. Obviously Page will serve as the basic page type for all pages and HomePage only for the homepage. For every basic page you want a custom header photo, and for homepage none.

What would be the ideal setup, as in best practice?

Page extends sitetree and has ImageUpload field for header

HomePage extends Page and disables the ImageUpload field for header

OR

Page extends sitetree and has ImageUpload field for header

HomePage extends sitetree without any additional CMS Fields

Upvotes: 4

Views: 179

Answers (2)

ifusion
ifusion

Reputation: 2233

This is the way I do it:

Page.php

Every Page and any pages that extend Page will inherit the Image upload field.

class Page extends SiteTree {

    private static $has_one = array(
        'Image' => 'Image'
    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', UploadField::create('Image'), 'Content');
        return $fields;
    }

}

HomePage.php

Then on any page where you don't want the image upload field just call $fields->removeByName('Image');

class HomePage extends Page {

    private static $db = array();

    public function getCMSFields() {

        $fields = parent::getCMSFields();

        $fields->removeByName('Image');

        return $fields;
    }
}

Upvotes: 4

user2187
user2187

Reputation: 78

You can create another page type that inherits Page. Let's call it "BasicPage".

HomePage directly inherits Page.

BasicPage also directly inherits Page.

Then, attach the image "has_one" relationship to BasicPage.

I avoid creating pages of type "Page" in my site tree. I tend to mostly use "Page" as if it was an abstract type. The problem you'll encounter is that you'll stuff a lot of shared features in Page, and then one day, you'll need to create a new page type that has a very different look or set of features than most of the page types you've previously developed. When that'll happen, you'll be stuck with all the stuff inherited from Page and it will encumber both you and your application. Both you and your application will be slowed down.

Upvotes: 2

Related Questions