Domain Module and its extensions in Drupal 8 (and 9)

May 14, 2021
By Vasyl
Drupal

There are times when we need several sites that will have a similar interface or part of the functionality. There are three popular ways to do that in Drupal: multisites, profiles, and domains. 

Today, we are going to cover the last one. We’ll describe the pros and cons of using domains, the most common modules, and give tips and tricks for working with them. 

 

What makes domains different? 

Multisites have their own, isolated database and they do not share content between them. It is the same with profiles. 

In contrast to multisites and profiles, domains share a database. This means that they can easily share users, content, and configuration between themselves. 

Domains are helpful when you want each of your sites to display similar content because you won’t need to duplicate it across multiple sites. 

Pros: 

  • Easy content sharing between sites. 
  • Easy content management for all sites.

Cons:

  • It can be hard to manage access for admins or editors to different domains. 
  • It can be hard to add exceptions that don’t follow the common site structure. 

Overall, domains make sense if sites share content and users and if the functionality of all the sites (and the content types posted on them) will be broadly similar. 

 

What does the Domain module contain? 

Inside the Domain access module, there are the following modules: 

  • Domain. This is the main module that enables registering multiple domains inside one Drupal installation. Also, it allows users to be assigned as domain administrators, and provides additional contexts.
  • Domain Access. Provides access controls for the domains. Allows users to be assigned as editors of content per-domain, sets content visibility.
  • Domain Alias. Allows multiple hostnames to point to the same domain. 
  • Domain Config. Provides a means for easily changing configuration settings for domains. 
  • Domain Content. Helps editors see all the content that is assigned to each domain. 
  • Domain Source. Enables you to assign canonical links for content when writing URLs so that content on different domains links to one URL. 

 

How to setup domains for your website 

If you want to make your site support domains, here are the steps you need to take: 

  1. Enable needed modules. First, you need to understand which modules you need, install (with composer, for example) and enable them.
  2. Configure domain records. Provide the necessary info such as hostname, name, and other things. 
  3. Configure aliases for domain records. If you need multiple hostnames to point to the same domain, you will want to configure aliases. 
  4. Check $settings['trusted_host_patterns']. If you’re using the trusted host security setting, make sure to add your domains to the list.
  5. Configure cross-domain logins if necessary. Decide if you want users to stay logged in while moving across sites. If it is necessary, widen the scope of cookie_domain in services.yml from 'example.com' to '.example.com'
  6. Configure domain-sensitive caching if necessary. In other words, decide if you need content to not stay cached when moving across domains. If you need that, append url.site to required_cache_contexts.

 

Other helpful Domain-related modules 

In addition to the main Domain Access module and his submodules listed before, there are a ton of other domain-related modules out there.

Here’s a curated list of other modules we have used that you will definitely find handy when creating domains for your website. 

Let’s look at two of them more closely and see how they can be used and where they can be useful. 

 

Domain Menus for Domains

Domain Menus allow each domain to have its own main menu or footer, which can be administered by users that have admin permission of that domain. 

Here are some of the features that it offers: 

  • Menus can be assigned to individual domains.
  • You can bulk create or delete menus. 
  • You can auto-create/auto-delete domain menus when a domain is created or deleted (id of menu will looks like dm<DOMAIN_ID>-<MENU_NAME>).
  • Provides a block (which can be added to Block Layout for example) with only one setting, menu to display. It will automatically detect the domain and will render the related menu. 

 

Domain Path 

Image
Domain paths provide a more flexible way to define paths of your pages

Domain path provide a more flexible way to define paths of your pages. It allows you to generate separate path aliases for each domain. In other words, you can have different aliases for the same node in each domain. This can be useful, for example, when you want to override node paths for some of the domains.

 

Using hooks to manage access 

In addition to ready-made modules, you might need to change some of the functionality that the Domain Access modules give out of the box according to your use case. To do that, we use hooks, a very powerful Drupal feature. A little part of them:

  • hook_preprocess_block / hook_ENTITY_TYPE_access (for block, block_content, menu ...) - used to restrict access to some content, f.e. hide some specific menus, contextual links, to restrict content operations (delete, edit) for users, for some specific blocks (that was generated automatically f.e.) even if they have permission for it.
  • hook_form_alter - for preventing logging in user that are not allowed to manage current domain.
  • hook_views_query_alter - we used this hook to hide pages that are marked as ‘Send to all affiliates’ (Make this content available on all domains, f.e. search page, events or similar content listing, FAQ page etc.) from domain editor users that have permission to edit content on some domains (by default they have access to these pages, but logically that they shouldn’t have)
  • hook_node_grants_alter - overrides default Domain access logic for some pages (f.e. Search, Events listing etc) and display content from all domains (even if content not marked Send to all affiliates)

 

With these hooks, you can check whether the user has an admin or editor role in their profile. 

This, in turn, enables you to strictly control domain access and prevent any mistakes where users access things that they are not authorized to access or edit things by mistake. 

 

Conclusion 

Domains can be very helpful for managing multiple sites at once, but to prevent any mishaps, you need to make sure to finely manage your user privileges. Thankfully, there are a lot of modules and other techniques to help you manage your domains, and most of the problems can be solved with the help of those. 

Vasyl
Software Developer and technology enthusiast. Writes about Drupal, technologies, and digital innovations.

LET'S CONNECT

Get a stunning website, integrate with your tools,
measure, optimize and focus on success!