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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityFinally 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.
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();