Multisite

Improvements?

Let us know by posting here.

Concrete CMS version 9.0 introduced multisite functionality, enabling developers to manage multiple websites within a single Concrete installation. This capability allows for centralized management while maintaining distinct configurations for each site. From a developer's perspective, leveraging multisite support requires understanding how to adapt packages and themes, detect multisite setups, and identify the active site context during various operations.

Important Classes

Some classes that are important to multisite functionality are the following:

Detecting a Multisite Setup

To determine if the Concrete CMS installation is configured for multisite, you can check the number of sites managed by the system. If more than one site exists, multisite functionality is active.

Example: Checking for Multisite Configuration

use Concrete\Core\Site\InstallationService;
use Concrete\Core\Support\Facade\Application;

$service = $app->make(InstallationService::class);
$isMultisite = $service->isMultisiteEnabled();

Accessing the Current Site

To retrieve information about the current site, utilize the Site facade. This class offers methods to obtain details such as the site's handle, name, and configuration.

Example: Retrieving the Current Site's Handle

use Concrete\Core\Support\Facade\Site;

$site = Site::getSite();
$siteHandle = $site->getSiteHandle();

In this example, Site::getSite() fetches the current site entity, and getSiteHandle() returns its handle.

Note: There is no danger of using Site methods even when multisite has not been enabled. In the abave example, the site handle will be defualt if multisite has not been enabled.

Loading Site-Specific Configurations

Each site within a multisite setup can have its own configuration settings. To access these settings programmatically, use the getConfigRepository() method from the Site class.

Example: Accessing a Site-Specific Configuration Value

use Concrete\Core\Support\Facade\Site;

$site = Site::getSite();
$config = $site->getConfigRepository();
$customValue = $config->get('name');

Here, $config->get('name') retrieves the value of name specific to the current site. These settings can be found in application/config/generated_overrides/sites.php in the sites array.

Note: this is a contrived example. A site name can be accessed by calling getSiteName() on the current site entity.

Determining the Site of a Page

When a page is being edited or viewed, you can determine which site the page belongs to by accessing the page's associated site information.

Example: Getting the Site of the Current Page

use Concrete\Core\Page\Page;

$page = Page::getCurrentPage();
$site = $page->getSite();
$siteHandle = $site->getSiteHandle();

In this example, $page->getSite() returns the Site entity associated with the current page, allowing you to retrieve its handle (or default if multisite has not been enabled) or other site-specific information.

Determining the Theme Skin Used for a Site

Multisite allows for each site to have a unique theme and/or skin. The following example shows how to check for the theme style set on a site.

Example: Getting the Theme Stylesheets for a Site

use Concrete\Core\Support\Facade\Site;
use Concrete\Core\StyleCustomizer\Skin\SkinInterface;

$site = Site::getSite();
$skinIdentifier = SkinInterface::SKIN_DEFAULT;
if ($site->getThemeSkinIdentifier()) {
    $skinIdentifier = $site->getThemeSkinIdentifier();
}
$skin = $this->themeObject->getSkinByIdentifier($skinIdentifier);
$stylesheet = $skin->getStylesheet();
return $stylesheet;

In this example, getThemeSkinIdentifier() returns the theme identifier associated with the current site.