Skip to main content
The app.root and site.path services are deprecated in Drupal 9 and will be removed in Drupal 10.
Source Link
norman.lol
  • 19.1k
  • 6
  • 75
  • 129

I once had a question on how to get current multi-site instance name. You can use the accepted answer's code to add another template suggestion based on the name then:

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function MYTHEME_theme_suggestions_node_alter(array &$suggestions, array $variables) { $site_path = \Drupal\Core\DrupalKernel::findSitePath(\Drupal::servicerequest('site.path')); // e.g.: 'sites/default' $site_path = explode('/', $site_path); $site_name = $site_path[1]; $site_specific_suggestions = []; foreach ($suggestions as $suggestion) { $site_specific_suggestions[] = $suggestion . '__site_' . $site_name; } $suggestions = array_merge($suggestions, $site_specific_suggestions); } 

Before:

<!-- FILE NAME SUGGESTIONS: * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 

After:

<!-- FILE NAME SUGGESTIONS: * node--236--full--site-default.html.twig * node--236--site-default.html.twig * node--page--full--site-default.html.twig * node--page--site-default.html.twig * node--full--site-default.html.twig * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 

I once had a question on how to get current multi-site instance name. You can use the accepted answer's code to add another template suggestion based on the name then:

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function MYTHEME_theme_suggestions_node_alter(array &$suggestions, array $variables) { $site_path = \Drupal::service('site.path'); // e.g.: 'sites/default' $site_path = explode('/', $site_path); $site_name = $site_path[1]; $site_specific_suggestions = []; foreach ($suggestions as $suggestion) { $site_specific_suggestions[] = $suggestion . '__site_' . $site_name; } $suggestions = array_merge($suggestions, $site_specific_suggestions); } 

Before:

<!-- FILE NAME SUGGESTIONS: * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 

After:

<!-- FILE NAME SUGGESTIONS: * node--236--full--site-default.html.twig * node--236--site-default.html.twig * node--page--full--site-default.html.twig * node--page--site-default.html.twig * node--full--site-default.html.twig * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 

I once had a question on how to get current multi-site instance name. You can use the accepted answer's code to add another template suggestion based on the name then:

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function MYTHEME_theme_suggestions_node_alter(array &$suggestions, array $variables) { $site_path = \Drupal\Core\DrupalKernel::findSitePath(\Drupal::request()); $site_path = explode('/', $site_path); $site_name = $site_path[1]; $site_specific_suggestions = []; foreach ($suggestions as $suggestion) { $site_specific_suggestions[] = $suggestion . '__site_' . $site_name; } $suggestions = array_merge($suggestions, $site_specific_suggestions); } 

Before:

<!-- FILE NAME SUGGESTIONS: * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 

After:

<!-- FILE NAME SUGGESTIONS: * node--236--full--site-default.html.twig * node--236--site-default.html.twig * node--page--full--site-default.html.twig * node--page--site-default.html.twig * node--full--site-default.html.twig * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 
Source Link
norman.lol
  • 19.1k
  • 6
  • 75
  • 129

I once had a question on how to get current multi-site instance name. You can use the accepted answer's code to add another template suggestion based on the name then:

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function MYTHEME_theme_suggestions_node_alter(array &$suggestions, array $variables) { $site_path = \Drupal::service('site.path'); // e.g.: 'sites/default' $site_path = explode('/', $site_path); $site_name = $site_path[1]; $site_specific_suggestions = []; foreach ($suggestions as $suggestion) { $site_specific_suggestions[] = $suggestion . '__site_' . $site_name; } $suggestions = array_merge($suggestions, $site_specific_suggestions); } 

Before:

<!-- FILE NAME SUGGESTIONS: * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig --> 

After:

<!-- FILE NAME SUGGESTIONS: * node--236--full--site-default.html.twig * node--236--site-default.html.twig * node--page--full--site-default.html.twig * node--page--site-default.html.twig * node--full--site-default.html.twig * node--236--full.html.twig * node--236.html.twig x node--page--full.html.twig * node--page.html.twig * node--full.html.twig * node.html.twig -->