1

I need to write a custom module to map some static pages used as front page to some specific domains. For example,

  • If domain is www.abc.com, the front page should be static_page_1
  • If the domain is www.bcd.com, the front page should be static_page_2

I have made a form for administrator users to enter the domain name and the static page they want as front page, but now I don't have any idea on where and how to write the code for detecting the domain name and set the front page. Any information and suggestion will be very appreciated.

  • Have you tried Domain Access or looked at their code? – leymannx Oct 22 at 8:22
  • @leymannx my supervisor doesn't allow me to install a module. he suggest me to find a hook or search about event subscriber. I check Domain Access code on its gitHub but cant find about the set homepage part. is it still possible to do this without Domain Access module? – Yulia Giarto Oct 22 at 9:11
  • Frontpage is a config setting, take a look at the extra module domain_config – Hudri Oct 22 at 11:51
0

I've already find the answer by referring to Programmatically set the front page per site?. It is much simpler than the reference, because I just want to map the domain with my static page and I don't have a multisite drupal. So, I just add an event subscriber and put all the mapping there.

In the getSubscribedEvents method you need to set the weight to something greater than 30 which overrides the Page Cache and Dynamic Cache.

$events['kernel.request'] = ['kernel_request', '35'];

Add this function in the same event subscriber file

public function kernel_request(Event $event) {
$config = \Drupal::configFactory()->getEditable('system.site');
$site_path = \Drupal::service('site.path');
$site_path = explode('/', $site_path);
$site_name = $site_path[1];

if ($site_name === 'site2' && $event->getRequest()->getPathInfo() == '/') {
  $config->set('page.front', '/node/239')->save();
}
else {
  $config->set('page.front', '/home-content')->save();

}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.