Domains and Drupal 8

Vitalii Zinenko               vzinenko@adyax.com

 

Andrey Postnikov      apostnikov@gmail.com

Domains

Capabilities

  • use one Drupal installation for many sites
  • share content to any sites during installation
  • permit accesses for:
  1. domain
  2. blocks, entity types or node bundles
  3. domain languages
  4. users and user roles

Domains

Possible issues:

  • SEO issues
  • Single Point of Failure
  • ​Greater configuration overhead
  • Higher server load

Domain access vs Multi-site:

Multi-site

Domain Access

Domains

Installing module. You should know:

  • simple server configurations
<VirtualHost *:80>
    DocumentRoot /path/to/drupal/install
    ServerName drupal8.test
    ServerAlias *.drupal8.test drupal8.test
</VirtualHost>
  • how to manage virtual hosts
sudo nano /etc/hosts

127.0.0.1      domain1.drupal8.test
127.0.0.1      domain2.drupal8.test
127.0.0.1      domain3.drupal8.test
127.0.0.1      another-domain.test

Domains

Installing on shared hosting with Cpanel:

Domains

Installing module:

  • place module into /path_to_drupal/modules/contrib/ or install in via Drush or Composer
  • no need to edit settings.php as in Drupal 7
  • change 'cookie_domain' at sites/default/services.yml for cross-domain login
  • set trusted host settings, if using:
$settings['trusted_host_patterns'] = array(
  '^*\.drupal8\.test$',
  '^another-domain\.test$',
);

Domains

View permissions:

Domains

Provided blocks:

Domains

Manage domains in BO:

Domains

Set domain for Entity:

Domains

Available hooks:

/**
 * Notifies other modules that we are loading a domain record from the database.
 *
 * When using this hook, you should invoke the namespace with:
 *
 * use Drupal\domain\DomainInterface;
 *
 * @param array $domains
 *   An array of $domain record objects.
 */

function hook_domain_load(array $domains) {
  // Add a variable to the $domain.
  foreach ($domains as $domain) {
    $domain->addProperty('myvar', 'mydomainvar');
  }
}
/**
 * Allows modules to modify the inbound domain request.
 *
 * When using this hook, first check $domain->getMatchType(), which returns a
 * numeric constant indicating the type of match derived by the caller or by
 * earlier returns of this hook (such as domain_alias_request_alter()).
 * Use this value to determine if the request needs to be overridden. Valid
 * types are DomainNegotiator::DOMAIN_MATCH_NONE,
 * DomainNegotiator::DOMAIN_MATCH_EXACT, DomainNegotiator::DOMAIN_MATCH_ALIAS.
 */
function hook_domain_request_alter(\Drupal\domain\DomainInterface &$domain) {
  // Add a special case to the example domain.
  if ($domain->getMatchType() == \Drupal\domain\DomainNegotiator::DOMAIN_MATCH_EXACT && $domain->id() == 'example_com') {
    // Do something here.
    $domain->addProperty('foo', 'Bar');
  }
}
/**
 * Adds administrative operations for the domain overview form.
 *
 * These operations are only available to users who can administer the domain.
 * That access check happens prior to this hook being called. If your use-case
 * requires additional permission checking, you should provide it before
 * returning any values.
 */
function hook_domain_operations(\Drupal\domain\DomainInterface $domain, \Drupal\Core\Session\AccountInterface $account) {
  $operations = [];
  // Check permissions.
  if ($account->hasPermission('view domain aliases') || $account->hasPermission('administer domain aliases')) {
    // Add aliases to the list.
    $id = $domain->id();
    $operations['domain_alias'] = array(
      'title' => t('Aliases'),
      'url' => Url::fromRoute('domain_alias.admin', array('domain' => $id)),
      'weight' => 60,
    );
  }
  return $operations;
}
/**
 * Alter the list of domains that may be referenced.
 *
 * Note that this hook does not fire for users with the 'administer domains'
 * permission.
 *
 */
function hook_domain_references_alter($query, $account, $context) {
  // Remove the default domain from non-admins when editing nodes.
  if ($context['entity_type'] == 'node' && $context['field_type'] == 'editor' && !$account->hasPermission('edit assigned domains')) {
    $default = \Drupal::service('domain.loader')->loadDefaultId();
    $query->condition('id', $default, '<>');
  }
}
/**
 * Alter the validation step of a domain record.
 *
 * This hook allows modules to change or extend how domain validation
 * happens. Most useful for international domains or other special cases
 * where a site wants to restrict domain creation is some manner.
 *
 */
function hook_domain_validate_alter(&$error_list, $hostname) {
  // Only allow TLDs to be .org for our site.
  if (substr($hostname, -4) != '.org') {
    $error_list[] = t('Only .org domains may be registered.');
  }
}

Domains

Submodules:

  • domain_access
  • domain_allias
  • domain_config
  • domain_content
  • domain_source

Domains

Created modules:

 

  • https://github.com/skilld-labs/domain config-ui branch
  • https://github.com/skilld-labs/domain_language
  • https://github.com/skilld-labs/domain_menu_access
  • https://github.com/skilld-labs/domain_block_content
  • https://github.com/skilld-labs/domain_entity

 

Domains

Domain Access

Vitalii Zinenko               vzinenko@adyax.com

Andrey Postnikov      apostnikov@gmail.com

Kharkiv Drupal Talks #4. Domain module

By Vitali Zinenko

Kharkiv Drupal Talks #4. Domain module

  • 1,039