Andrew Fletcher published: 4 July 2022 1 minute read
My goal was to get the base URL and compare it with the URL of the link. Was it an internal or external link? You can get the hostname, "{your_domain}", directly from the getHost() request:
$host = \Drupal::request()->getHost();
However, if you need the schema as well, such as https://{your_domain}
$host = \Drupal::request()->getSchemeAndHttpHost();
In my situation, I needed to redirect the page to an external site and count the page. So
$cache = \Drupal::cache();
$cache->deleteAll();
$url = $this->helper->getField(
$this->node, 'field_document_link'
);
$redirect = new TrustedRedirectResponse($url);
return $redirect->send();
Finally, the redirect would only kick in if the link was external. Which I wrote separately.
Related articles
Andrew Fletcher
•
26 Apr 2024
TypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string
I'm getting the following errorTypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string in Drupal\views\Plugin\views\argument\ArgumentPluginBase->unpackArgumentValue() (line 1302 of...
Andrew Fletcher
•
22 Apr 2024
Streamlined Drupal 10 Deployment: Best Practices for Stability and Security
Have you ever walked into a situation and marveled at how a site is still operational? Deployments that are riddled with fail-safes piled atop a shaky foundation. The real challenge lies in gradually persuading the team to embrace change to build a stable, robust deployment process aligned with...
Andrew Fletcher
•
21 Apr 2024
This command will help you spot any unusually large directories
The red flag was when I saw the server disk space is showing a site is taking up 57992.5 MB, where locally the site size is showing 957MB. There is something serious happening here and I need to establish the problem quickly. When dealing with a significant discrepancy in site size...