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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Creating related pages in SS3


Go to End


2 Posts   550 Views

Avatar
lozhowlett

Community Member, 151 Posts

1 October 2013 at 3:55am

Hi everyone

Just trying to create a simple related pages, which oddly I can not find a tut for anywhere! I will make one once this is finished.

What I want to do is pull through the Related Page details into the summary fields, so I can see whats related instead of just the ID. I have no idea how to do this...

So i have...

<?php
class RelatedPage extends DataObject {

    public static $db = array(
            "SortOrder" => "Int",
            "RelatedPageID" => "Int"
    );

    public static $has_one = array(
            "Page" => "Page"
    );

    public static $has_many = array(
    );

    public static $field_labels = array( 
    );

     static $summary_fields = array (
        $this->Link;
    );

    function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main',new TreeDropdownField("RelatedPageID", "Select related page", "SiteTree"));
        $fields->removeByName('SortOrder');
        $fields->removeByName('PageID');
        return $fields;
    }

    public function Link(){
        if ($this->RelatedPageID){
            $doSet = DataObject::get_by_id("Page", $this->RelatedPageID);
            $parentID = $doSet->ParentID;
            $URLString = $doSet->URLSegment;
            while ($parentID != 0){
                $doSet = DataObject::get_by_id("Page", $parentID);
                $parentID = $doSet->ParentID;
                $URLString = $doSet ->URLSegment . '/' . $URLString;
            }
            return Director::BaseURL() . $URLString;
        } else {
            return false;
        }
    }

}

On the summary page of "Page" i would like to see

Page Title, Thumbnail (which is and has_one image on Page) and Link (which is the function above).

Any ideas?

Thanks!

Avatar
Bambii7

Community Member, 254 Posts

1 October 2013 at 6:31pm

Hi Lozhowlett.

There might be a better way. But you could easily mimic the Link function to return the summary of the related page. But I think there is an error in the summary_fields at the mo. Try updating the below code, renaming Link to getLink. Then reference 'Link' in the summary field.

static $summary_fields = array ( 
    'Summary',
    'Link'
);
public function getSummary() {
    $this->Page()->Content->Summary();
}
public function getLink() {
    ....
}