Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domain Alias - enable by default and document. #397

Open
finnlewis opened this issue Aug 3, 2023 · 6 comments
Open

Domain Alias - enable by default and document. #397

finnlewis opened this issue Aug 3, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@finnlewis
Copy link
Member

finnlewis commented Aug 3, 2023

Discussing in Tech Drop-in:

Christian wanted to have both www.haringeyfeast.com and haringeyfeast.com resolve to the same microsite.

@stephen-cox showed us that domain alias module needs enabling and configuring.

@stephen-cox suggested that perhaps we should enable this by default.

@finnlewis
Copy link
Member Author

Maybe we can then add docs to https://docs.localgovdrupal.org/microsites/how-to/add-URLs.html

@rupertj
Copy link
Member

rupertj commented Mar 12, 2024

+1 to this. We've set up domain alias to add our Stage and Dev domains to microsites so we can move the DB between environments without having to edit every single microsite entity and set the URL.

@finnlewis
Copy link
Member Author

You doing any work on this @rupertj or shall I dive in a make a pull request?

@rupertj
Copy link
Member

rupertj commented Mar 12, 2024

Go for it. All I've done so far is enable it on H&Fs set up to fix the environment problem :)

@finnlewis finnlewis self-assigned this Mar 12, 2024
@finnlewis finnlewis added the enhancement New feature or request label Apr 25, 2024
@finnlewis
Copy link
Member Author

Also see hot-to here: https://docs.localgovdrupal.org/microsites/how-to/add-URLs.html#how-to-enable-and-configure-domain-aliases-for-microsites

@Adnan-cds mentioned you might have some code to help automate this?

@Adnan-cds
Copy link

mentioned you might have some code to help automate this?

This is what we are using currently:

use Drupal\domain\DomainInterface;
use Drupal\domain\DomainNegotiatorInterface;

/**
 * Implements hook_domain_request_alter().
 *
 * Resolves dynamic domain aliases for local/dev/staging/np/edit URLs. Example:
 * - https://local.example.net/ resolves to https://example.net/ or
 *   https://www.example.net/
 * - https://dev.example.net/ resolves to https://example.net/ or
 *   https://www.example.net/
 * - https://np.example.net/ resolves to https://example.net/ or
 *   https://www.example.net/
 * - https://edit.example.net/ resolves to https://example.net/ or
 *   https://www.example.net/
 * - https://staging.example.net/ resolves to https://example.net/ or
 *   https://www.example.net/
 *
 * Also takes fixed domain aliases into account.  For example, the following
 * applies when example.org is a domain alias for example.net:
 * - https://local.example.org/ resolves to https://example.net/
 * - https://dev.example.org/ resolves to https://example.net/ and so on.
 * This does not work where the domain alias uses regular expression.  For
 * example, this will not work when *.example.org is a domain alias for
 * example.net.
 */
function modulename_domain_request_alter(DomainInterface &$active_domain_record) {

  if ($active_domain_record->getMatchType() !== DomainNegotiatorInterface::DOMAIN_MATCHED_NONE) {
    return;
  }

  $active_url_parts = parse_url($active_domain_record->getUrl());
  if (!$active_url_parts) {
    return;
  }
  $active_hostname = $active_url_parts['host'];
  $active_hostname_w_optional_port = $active_domain_record->getHostname();

  $matches = [];
  // Nonlive hostnames start with "local" or "devN" or "np";
  // e.g. dev5.example.net.
  $is_nonlive_env = preg_match('#^(dev\d*|edit|local|np|staging)\.(?<live_microsites_hostname>.+)#', $active_hostname, $matches);
  $is_live_env = !$is_nonlive_env;
  if ($is_live_env) {
    return;
  }

  $potential_domain = $matches['live_microsites_hostname'];
  $potential_domain_w_www_prefix = 'www.' . $matches['live_microsites_hostname'];
  $live_domain_record = Drupal::service('entity_type.manager')->getStorage('domain')->loadByHostname($potential_domain);
  $live_domain_record = $live_domain_record ?? Drupal::service('entity_type.manager')->getStorage('domain')->loadByHostname($potential_domain_w_www_prefix);
  $live_domain_record = $live_domain_record ?? _modulename_find_domain_record_from_alias($matches['live_microsites_hostname']);
  if (empty($live_domain_record)) {
    return;
  }

  // Finally, swap the active domain with the canonical one.
  $active_domain_record = $live_domain_record;
  $active_domain_record->setMatchType(DomainNegotiatorInterface::DOMAIN_MATCHED_ALIAS);
  $active_domain_record->setCanonical();
  $active_domain_record->setHostname($active_hostname_w_optional_port);
  $active_domain_record->setPath();
  $active_domain_record->setUrl();
}

/**
 * Finds domain record for given domain alias.
 *
 * Ignores domain aliases with port numbers and regular expressions due to
 * performance overhead.
 *
 * @see Drupal\domain_alias\DomainAliasStorage::loadByHostname()
 */
function _modulename_find_domain_record_from_alias(string $domain_alias): ?DomainInterface {

  $is_domain_alias_in_use = Drupal::service('entity_type.manager')->hasDefinition('domain_alias');
  if (!$is_domain_alias_in_use) {
    return NULL;
  }

  $live_domain_alias = Drupal::service('entity_type.manager')->getStorage('domain_alias')->loadByPattern($domain_alias);
  if (empty($live_domain_alias)) {
    return NULL;
  }

  // e.g. group_2.
  $live_domain_id = $live_domain_alias->getDomainId();
  $live_domain_record = Drupal::service('entity_type.manager')->getStorage('domain')->load($live_domain_id);
  return $live_domain_record;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Status: Microsites backlog
Development

No branches or pull requests

3 participants