I have the following code in my custom theme that works well and allows me to test against the multisite_id in my twig files:
function mytheme_preprocess_page(&$variables) {
$site_path = \Drupal::service('site.path');
$site_path = explode('/', $site_path);
$site_name = $site_path[1];
$variables['multisite_id'] = $site_name;
}
I need the same functionality inside a module. I can't seem to access the multisite_id var from the module.
I'm new to module development. I can't seem to get the same preprocess hook to work from my .module file. Is there a different approach when using a module?
{{ kint() }}
tell you?preprocess_PAGE
hook, it would ordinarily (only) be made available in/templates/page.html.twig
. If you want it intemplates/foo.html.twig
, you can put this snippet inmodule_preprocess_foo(&$variables)
. (Note that theme hook suggestions will muddy this 1:1 correlation)