Skip to content

Commit

Permalink
ENH Add domain support for the main site.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
GuySartorelli committed Dec 7, 2021
1 parent 2a16e1e commit f52b857
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
27 changes: 21 additions & 6 deletions src/Admin/SubsiteAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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';

Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/Extensions/SiteTreeSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Model/Subsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit f52b857

Please sign in to comment.