Skip to content

Commit

Permalink
Merge pull request #100 from symbiote/feature-cascading-themes
Browse files Browse the repository at this point in the history
Added ability to supply cascading themes to be chosen
  • Loading branch information
nyeholt authored Mar 16, 2020
2 parents 9b3fa1d + 5b1e622 commit 7d99c5a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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


Expand All @@ -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.


Expand All @@ -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
Expand All @@ -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()) {
Expand Down
7 changes: 5 additions & 2 deletions src/Extension/MultisitesControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ 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;
array_walk($selectedThemes, 'trim');
SSViewer::set_themes($selectedThemes);
}
}

Expand Down Expand Up @@ -85,4 +88,4 @@ public function onBeforeHTTPError($code, $request)
throw new HTTPResponse_Exception($response, $code);
}
}
}
}
9 changes: 6 additions & 3 deletions src/Extension/MultisitesSecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Sets the site theme when someone tries to login on a particular URL
*
*
* @package silverstripe-multisites
*/
class MultisitesSecurityExtension extends Extension
Expand All @@ -23,7 +23,10 @@ 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;
array_walk($selectedThemes, 'trim');
SSViewer::set_themes($selectedThemes);
}
}
}
}
22 changes: 11 additions & 11 deletions src/Model/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit 7d99c5a

Please sign in to comment.