Let's say I have the following multi-site setup:
$sites['hello-world-a.com'] = 'a';
$sites['stage.hello-world-a.com'] = 'a';
$sites['dev.hello-world-a.com'] = 'a';
$sites['hello-world-a.localhost'] = 'a';
$sites['hello-world-b.com'] = 'b';
$sites['stage.hello-world-b.com'] = 'b';
$sites['dev.hello-world-b.com'] = 'b';
$sites['hello-world-b.localhost'] = 'b';
Works fine. Both share the same theme and more or less the same features. Now let's say I have a very simple preprocess function that adds a CSS class to the body:
/**
* Implements template_preprocess_html().
*/
function MYTHEME_preprocess_html(&$variables) {
$variables['attributes']['class'][] = 'foo';
}
How can I now add the multi-site instance's name as body class dynamically?
- Without going through all possible domain names and string comparison?
- Also possibly without working with the human readable site name, which may change?
- Maybe with cache contexts?
- Or UUID?
/**
* Implements template_preprocess_html().
*/
function MYTHEME_preprocess_html(&$variables) {
$variables['attributes']['class'][] = 'foo';
// Basically along the following pattern.
if ( @@@ SITE A @@@ ) {
$variables['attributes']['class'][] = @@@ SITE A @@@;
}
}
DrupalKernel::findSitePath
, which includes sites/sites.php, breaks down the current host into an appropriate string, and checks the$sites
array for a matching key. If your requirements can be satisfied by getting the site folder, e.g. "sites/a" for your first group of sites, then you can just call the staticDrupalKernel::findSitePath
directly with the current request.