Site Picker
Tools for working with multiple site definitions
The sitePicker component provides tools for working with Multisite configuration for your website. The best place to include this is in your page or layout template.
# Basic Usage
[sitePicker]
___TWIG0___
The following is an example of displaying a dropdown that switches between sites. It is used in conjunction with the this.site Twig property.
<select class="form-control" onchange="window.location.assign(this.value)">
___TWIG0___
<option value="___TWIG1___" ___TWIG2___>
___TWIG3___
</option>
___TWIG4___
</select>
Another example is generating alternative page links using meta tags
___TWIG0___
<link rel="alternate" hreflang="___TWIG1___" href="___TWIG2___" />
___TWIG3___
# Loading Sites for a Different Page
By default, the sites property will return sites configured for the current page and the url will resolve to the current page. The pageSites() function can be used to locate sites for a different page, where the first argument is the CMS page name.
In the example below, each site will have a url set to the CMS page found in pages/blog/index.htm. If the page is not found, then the result will be an empty array.
[sitePicker]
___TWIG0___
# Translating URL Parameters
By default, the sitePicker component isn't aware of model parameters in the URL, such as page slugs and identifiers. The cms.sitePicker.overrideParams global event is used to override the URL parameters to their translated versions. A good place to put this event is inside the init or the onRun method of a CMS Component class.
For example, if the model implements the Multisite trait, the newOtherSiteQuery method is used to locate the model for the proposed site and modify the URL parameters.
$myModel = MyModel::find(1);
$otherModels = $myModel->newOtherSiteQuery()->get();
Event::listen('cms.sitePicker.overrideParams', function($page, $params, $currentSite, $proposedSite) use ($otherModels) {
$otherModel = $otherModels->where('site_id', $proposedSite->id)->first();
if ($otherModel) {
$params['id'] = $otherModel->id;
$params['slug'] = $otherModel->slug;
$params['fullslug'] = $otherModel->fullslug;
}
return $params;
});