From a3c2f1f7551eba8ead4f7c9aea5bc6ce22d191d0 Mon Sep 17 00:00:00 2001 From: Marcus Nyeholt Date: Sun, 15 Mar 2020 21:42:22 +1100 Subject: [PATCH 1/2] new(Site) Added ability to supply cascading themes to be chosen as the site theme rather than a single --- README.md | 45 ++++++++++--------- .../MultisitesControllerExtension.php | 6 ++- src/Extension/MultisitesSecurityExtension.php | 8 ++-- src/Model/Site.php | 22 ++++----- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 7cf44d8..b3568bf 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ ## Overview -Allows for multiple websites to be managed through a single site tree. +Allows for multiple websites to be managed through a single site tree. This is an alternative module to the Subsites module; it avoids any session -tracking of the 'current' website, and doesn't perform any query modification +tracking of the 'current' website, and doesn't perform any query modification at runtime to change the 'site' context of queries you execute **Compatible with SilverStripe 4.0.x** @@ -21,7 +21,7 @@ The following important changes have happened * Themes must now be explicitly configured in your project config. Set `Site.available_themes` in yml config. This must be a map of themename: Label -* Site specific Assets folders are not currently supported due to the +* Site specific Assets folders are not currently supported due to the fundamental change to asset management. This will be reviewed over time @@ -37,32 +37,35 @@ The following important changes have happened ## Setting up sites (and additional sites) -* In the CMS go to the Pages section and click on the Website -* Enter in the full path to the site in the 'Host' field, without the `http://` +* In the CMS go to the Pages section and click on the Website +* Enter in the full path to the site in the 'Host' field, without the `http://` lead - eg `mysitedomain.com` or `localhost/sub/folder` for a development site * Hit save -* To add a new site, click the Pages section; you should have an 'Add site' +* To add a new site, click the Pages section; you should have an 'Add site' button * Enter details about the new site, the Host field being the most important ## Configuration ``` -Site: +Site: available_themes: name: Label ``` +To support cascading themes, provide a comma-separated list of themes for 'name' in +configuration. + ## Assets management -NOTE: This is currently NOT working in SS4 due to the change to the asset -management layer. Once clearer, this will be re-enabled. +NOTE: This is currently NOT working in SS4 due to the change to the asset +management layer. Once clearer, this will be re-enabled. -You can optionally manage each site's assets in it's own subfolder of the -root assets/ directory. Add the following extensions in your mysite/config.yml -file and run ?flush=1. When editing a Site in the CMS, you now have the option -to select a subfolder of assets/ to contain all assets for that site. This -folder will be automatically created upon a) saving the site or b) visiting a +You can optionally manage each site's assets in it's own subfolder of the +root assets/ directory. Add the following extensions in your mysite/config.yml +file and run ?flush=1. When editing a Site in the CMS, you now have the option +to select a subfolder of assets/ to contain all assets for that site. This +folder will be automatically created upon a) saving the site or b) visiting a page in the cms that has an upload field. @@ -77,7 +80,7 @@ HtmlEditorField_Toolbar: ``` Files uploaded through the HTMLEditor will now be uploaded into -assets/yoursite/Uploads. If you have custom upload fields in the cms +assets/yoursite/Uploads. If you have custom upload fields in the cms however, you will need to add the following configuration to them explicitly. ```php @@ -89,16 +92,16 @@ images/page-images' to 'currentsitesubfolder/images/page-images' ## Known issues -When linking to a page that belongs to a different site, SiteTree::Link() will -return a bad link as it prepends the base URL. Currently the best way to work -around this is to implement the following in your Page.php (model class). +When linking to a page that belongs to a different site, SiteTree::Link() will +return a bad link as it prepends the base URL. Currently the best way to work +around this is to implement the following in your Page.php (model class). ```php /** - * Overrides SiteTree->Link. Adds a check for cases where we are linking to a + * Overrides SiteTree->Link. Adds a check for cases where we are linking to a page on a - * different site in this multisites instance. - * @return String + * different site in this multisites instance. + * @return String **/ public function Link($action = null) { if($this->SiteID && $this->SiteID == Multisites::inst()->getCurrentSiteId()) { diff --git a/src/Extension/MultisitesControllerExtension.php b/src/Extension/MultisitesControllerExtension.php index 6276eda..1248e9d 100644 --- a/src/Extension/MultisitesControllerExtension.php +++ b/src/Extension/MultisitesControllerExtension.php @@ -54,7 +54,9 @@ public function onAfterInit() if (!$this->owner instanceof \SilverStripe\Admin\LeftAndMain) { $theme = $site->getSiteTheme(); if ($theme) { - SSViewer::set_themes([$theme, SSViewer::DEFAULT_THEME]); + $selectedThemes = explode(',', $theme); + $selectedThemes[] = SSViewer::DEFAULT_THEME; + SSViewer::set_themes($selectedThemes); } } @@ -85,4 +87,4 @@ public function onBeforeHTTPError($code, $request) throw new HTTPResponse_Exception($response, $code); } } -} \ No newline at end of file +} diff --git a/src/Extension/MultisitesSecurityExtension.php b/src/Extension/MultisitesSecurityExtension.php index 94cc22f..d9e8b6b 100644 --- a/src/Extension/MultisitesSecurityExtension.php +++ b/src/Extension/MultisitesSecurityExtension.php @@ -9,7 +9,7 @@ /** * Sets the site theme when someone tries to login on a particular URL - * + * * @package silverstripe-multisites */ class MultisitesSecurityExtension extends Extension @@ -23,7 +23,9 @@ function onBeforeSecurityLogin() $site = Multisites::inst()->getCurrentSite(); if ($site && $site->Theme) { - SSViewer::set_themes([$site->Theme, SSViewer::DEFAULT_THEME]); + $selectedThemes = explode(',', $site->Theme); + $selectedThemes[] = SSViewer::DEFAULT_THEME; + SSViewer::set_themes($selectedThemes); } } -} \ No newline at end of file +} diff --git a/src/Model/Site.php b/src/Model/Site.php index 28b4e39..fa26400 100644 --- a/src/Model/Site.php +++ b/src/Model/Site.php @@ -40,7 +40,7 @@ class Site extends Page implements HiddenClass, PermissionProvider { private static $table_name = 'Site'; - + private static $singular_name = 'Site'; private static $plural_name = 'Sites'; private static $description = 'A page type which provides a subsite.'; @@ -159,7 +159,7 @@ public function getUrl() { public function AbsoluteLink($action = null){ return $this->getURL() . '/'; } - + public function Link($action = null) { if ($this->ID && $this->ID == Multisites::inst()->getCurrentSiteId()) { return parent::Link($action); @@ -197,7 +197,7 @@ public function onBeforeWrite() { //Set MenuTitle to NULL so that Title is used $this->MenuTitle = NULL; - + if($this->ID && Multisites::inst()->assetsSubfolderPerSite() && !$this->Folder()->exists()){ $this->FolderID = $this->createAssetsSubfolder(); } @@ -274,26 +274,26 @@ public function requireDefaultRecords() { */ static public function get_by_link($link, $cache = true) { $current = Multisites::inst()->getCurrentSiteId(); - + if(trim($link, '/')) { $link = trim(Director::makeRelative($link), '/'); } else { $link = RootURLController::get_homepage_link(); } - + $parts = Convert::raw2sql(preg_split('|/+|', $link)); - + // Grab the initial root level page to traverse down from. $URLSegment = array_shift($parts); - + $sitetree = DataObject::get_one ( SiteTree::class, "\"URLSegment\" = '$URLSegment' AND \"ParentID\" = " . $current, $cache ); - + if (!$sitetree) { return false; } - + /// Fall back on a unique URLSegment for b/c. if(!$sitetree && self::nested_urls() && $page = DataObject::get(SiteTree::class, "\"URLSegment\" = '$URLSegment'")->First()) { return $page; @@ -307,7 +307,7 @@ static public function get_by_link($link, $cache = true) { $next = DataObject::get_one ( SiteTree::class, "\"URLSegment\" = '$segment' AND \"ParentID\" = $sitetree->ID", $cache ); - + if(!$next) { return false; } @@ -342,7 +342,7 @@ public function hasFeature($feature){ if(!$this->DevID) return false; $sites = Config::inst()->get('Multisites', 'site_features'); - + if(!isset($sites[$this->DevID])) return false; $features = ArrayLib::valuekey($sites[$this->DevID]); From 5b1e622258b5e0381f5922cc856a88b5080ab2a8 Mon Sep 17 00:00:00 2001 From: Marcus Nyeholt Date: Mon, 16 Mar 2020 18:54:24 +1100 Subject: [PATCH 2/2] fix(MultisitesController) array_walk trim across the configured theme names --- src/Extension/MultisitesControllerExtension.php | 1 + src/Extension/MultisitesSecurityExtension.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Extension/MultisitesControllerExtension.php b/src/Extension/MultisitesControllerExtension.php index 1248e9d..ffc57f1 100644 --- a/src/Extension/MultisitesControllerExtension.php +++ b/src/Extension/MultisitesControllerExtension.php @@ -56,6 +56,7 @@ public function onAfterInit() if ($theme) { $selectedThemes = explode(',', $theme); $selectedThemes[] = SSViewer::DEFAULT_THEME; + array_walk($selectedThemes, 'trim'); SSViewer::set_themes($selectedThemes); } } diff --git a/src/Extension/MultisitesSecurityExtension.php b/src/Extension/MultisitesSecurityExtension.php index d9e8b6b..e4aeb5f 100644 --- a/src/Extension/MultisitesSecurityExtension.php +++ b/src/Extension/MultisitesSecurityExtension.php @@ -25,6 +25,7 @@ function onBeforeSecurityLogin() if ($site && $site->Theme) { $selectedThemes = explode(',', $site->Theme); $selectedThemes[] = SSViewer::DEFAULT_THEME; + array_walk($selectedThemes, 'trim'); SSViewer::set_themes($selectedThemes); } }