Silverstripe - Various tabs and fields on the page.

In SilverStripe 3.1, you can add various tabs and fields on the about page.

And then various tabs and fields on the services page, for example.

About Page - Images Tab / Attachments Tab

Tools Page - Pictures Tab / Attachments Tab / Personnel Tab

Below is a sample code. I added if statements around the snippet that works. But it only works for all pages, showing the same tabs on all pages.

I've done video tutorials on the SilverStripe website and I see that you can create page types, but I really need to know if you can achieve this without creating additional page types.

// I want this on the about page 

// if page=about {

class Page extends SiteTree {

    private static $has_one = array (
        'Photo' => 'image',
        'Brochure' => 'file',
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Images', $photo = UploadField::create('Photo'));
        $fields->addFieldToTab('Root.Attachments', $brochure = UploadField::create('Brochure'));

        return $fields;
    }

}

// I want this on the services page 

// } elseif page=services {

class Page extends SiteTree {

    private static $has_one = array (
        'Photo' => 'image',
        'Brochure' => 'file',
        'Staff Person' => 'image',
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Images', $photo = UploadField::create('Photo'));
        $fields->addFieldToTab('Root.Attachments', $brochure = UploadField::create('Brochure'));
        $fields->addFieldToTab('Root.Staff', $staff = UploadField::create('Staff'));
        return $fields;
    }

}

// }

class Page_Controller extends ContentController {

    private static $allowed_actions = array();

    public function init() {
        parent::init();
    }
}

      

+3


source to share


1 answer


I would recommend using separate page types for what you want to do.

However, if you only want to use one page type, you can use an if statement in your function getCMSFields

to display different fields.



In this code example, I am checking URLSegment

, although you can check for something else like Title

.

class Page extends SiteTree {

    private static $has_one = array (        
        'Photo' => 'image',   
        'Brochure' => 'file', 
        'Staff Person' => 'image', 
    );

    public function getCMSFields() {        
        $fields = parent::getCMSFields();

        if ($this->URLSegment == 'about' || $this->URLSegment == 'services') {
            $fields->addFieldToTab('Root.Images', $photo = UploadField::create('Photo'));
            $fields->addFieldToTab('Root.Attachments', $brochure = UploadField::create('Brochure'));     
        }
        if ($this->URLSegment == 'services') {
            $fields->addFieldToTab('Root.Staff', $staff = UploadField::create('Staff'));
        }

        return $fields;         
    }

}

      

+2


source







All Articles