From f52b85768786986af2a2c25e98174e394acb1325 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 7 Dec 2021 12:21:13 +1300 Subject: [PATCH 1/2] ENH Add domain support for the main site. This ensures the AbsoluteLink for pages will always be correct for main site pages (if a domain is set up for the main site) - even when calling it from the context of a subsite. This allows easily linking to main site pages from subsites, as well as any other situation where the absolute URL for the main site needs to be fetched correctly. --- src/Admin/SubsiteAdmin.php | 27 +++++++++++++++++++++------ src/Extensions/SiteTreeSubsites.php | 4 +--- src/Model/Subsite.php | 10 ++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Admin/SubsiteAdmin.php b/src/Admin/SubsiteAdmin.php index 612fd2d4..6c1c4c8f 100644 --- a/src/Admin/SubsiteAdmin.php +++ b/src/Admin/SubsiteAdmin.php @@ -7,6 +7,7 @@ use SilverStripe\Forms\GridField\GridFieldPaginator; use SilverStripe\Subsites\Forms\GridFieldSubsiteDetailForm; use SilverStripe\Subsites\Model\Subsite; +use SilverStripe\Subsites\Model\SubsiteDomain; /** * Admin interface to manage and create {@link Subsite} instances. @@ -15,7 +16,14 @@ */ class SubsiteAdmin extends ModelAdmin { - private static $managed_models = [Subsite::class]; + private static $managed_models = [ + Subsite::class => [ + 'title' => 'Subsites' + ], + SubsiteDomain::class => [ + 'title' => 'Main site domains' + ], + ]; private static $url_segment = 'subsites'; @@ -31,11 +39,18 @@ public function getEditForm($id = null, $fields = null) { $form = parent::getEditForm($id, $fields); - $grid = $form->Fields()->dataFieldByName(str_replace('\\', '-', Subsite::class)); - if ($grid) { - $grid->getConfig()->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(100); - $grid->getConfig()->removeComponentsByType(GridFieldDetailForm::class); - $grid->getConfig()->addComponent(new GridFieldSubsiteDetailForm()); + if ($this->modelClass === Subsite::class) { + $grid = $form->Fields()->dataFieldByName(str_replace('\\', '-', Subsite::class)); + if ($grid) { + $grid->getConfig()->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(100); + $grid->getConfig()->removeComponentsByType(GridFieldDetailForm::class); + $grid->getConfig()->addComponent(new GridFieldSubsiteDetailForm()); + } + } + + if ($this->modelClass === SubsiteDomain::class) { + $grid = $form->Fields()->dataFieldByName($this->sanitiseClassName(SubsiteDomain::class)); + $grid->setList($grid->getList()->filter('SubsiteID', 0)); } return $form; diff --git a/src/Extensions/SiteTreeSubsites.php b/src/Extensions/SiteTreeSubsites.php index a1a22dbf..9aef2bc7 100644 --- a/src/Extensions/SiteTreeSubsites.php +++ b/src/Extensions/SiteTreeSubsites.php @@ -412,9 +412,7 @@ public function alternateAbsoluteLink($action = null) // Generate the existing absolute URL and replace the domain with the subsite domain. // This helps deal with Link() returning an absolute URL. $url = Director::absoluteURL($this->owner->Link($action)); - if ($this->owner->SubsiteID) { - $url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url); - } + $url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url); return $url; } diff --git a/src/Model/Subsite.php b/src/Model/Subsite.php index ab3221cb..cbb3e3f3 100644 --- a/src/Model/Subsite.php +++ b/src/Model/Subsite.php @@ -885,6 +885,16 @@ public function domain() */ public function getPrimarySubsiteDomain() { + // Main site + if (!$this->isInDB()) { + Subsite::disable_subsite_filter(true); + $domain = SubsiteDomain::get()->filter('SubsiteID', 0) + ->sort('"IsPrimary" DESC') + ->first(); + Subsite::disable_subsite_filter(false); + return $domain; + } + // Subsites return $this ->Domains() ->sort('"IsPrimary" DESC') From 0b7a63e0482a8e004ef34595233654124189fcbe Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 7 Dec 2021 13:01:57 +1300 Subject: [PATCH 2/2] ENH: Use sanitiseClassName for consistency. --- src/Admin/SubsiteAdmin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/SubsiteAdmin.php b/src/Admin/SubsiteAdmin.php index 6c1c4c8f..4e345c4e 100644 --- a/src/Admin/SubsiteAdmin.php +++ b/src/Admin/SubsiteAdmin.php @@ -40,7 +40,7 @@ public function getEditForm($id = null, $fields = null) $form = parent::getEditForm($id, $fields); if ($this->modelClass === Subsite::class) { - $grid = $form->Fields()->dataFieldByName(str_replace('\\', '-', Subsite::class)); + $grid = $form->Fields()->dataFieldByName($this->sanitiseClassName(Subsite::class)); if ($grid) { $grid->getConfig()->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(100); $grid->getConfig()->removeComponentsByType(GridFieldDetailForm::class);