CMS Drupal Mastering Development

Getting site ID from module (vs theme)

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?

Leave a Reply

Your email address will not be published. Required fields are marked *