1

I need the equivalent of domain_get_domain() in the Domain Access module for Drupal 8, but I can't find it.

I need to find the domain for the current request.

| improve this question | | | | |
3

Finally worked out the following ...

$domainNegotiator = \Drupal::service('domain.negotiator');
$current = $domainNegotiator->negotiateActiveHostname();

There are other methods on the DomainNegotiator service that I thought should work, particularly getActiveDomain() and getActiveId(), but they didn't return the domain I was actually on like I thought they would.

It's still a bit confusing to me, but I made it work.

EDIT: @berramou 's answer below helped as well. Originally, I was looking for the Domain object (from the Domain Access module), but it turns out the host was enough to match a Domain reference on my entity.

| improve this answer | | | | |
1

In drupal 8 there is \Drupal::request() to get request info.

For Example if your url something like https://example.com

If you want only the domain name example.com

$domain = \Drupal::request()->getHost();

if you want the domain name with schema http or https https://example.com use this:

  $domain_with_schema = \Drupal::request()->getSchemeAndHttpHost();
| improve this answer | | | | |

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.