silverstripesilverstripe-5

Using silverstripe-linkfield with subsites


I have a silverstripe site with 6 subsites, and have used LinkField throughout for creating various links in banners etc, however the 'Page on this site' TreeDropdown shows all pages in the 6 sites, and I have tried various options but cannot get it to filter by subsite - any ideas?


Solution

  • Yes, that was me :) This was what I ended up with:

    class SiteTreeLinkExtension extends Extension
    {
        public function updateCMSFields(FieldList $fields)
        {
            $fields->removeByName('PageID');
    
            $currentSubsiteID = SubsiteState::singleton()->getSubsiteId();
    
            $fields->insertAfter(
                'LinkText',
                TreeDropdownField::create(
                    'PageID',
                    _t(__CLASS__ . '.PAGE_FIELD_TITLE', 'Page'),
                    SiteTree::class,
                    'ID',
                    'TreeTitle'
                )->setSourceObject(SiteTree::class)
                ->setFilterFunction(function ($page) use ($currentSubsiteID) {
                    return $page->SubsiteID == $currentSubsiteID;
                })
            );
        }
    }