-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from City-of-Helsinki/UHF-9824
UHF-9824: Allow relative links
- Loading branch information
Showing
38 changed files
with
202 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation\Event; | ||
|
||
|
80 changes: 80 additions & 0 deletions
80
src/EventSubscriber/AbsoluteUrlMenuTreeBuilderLinkSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation\EventSubscriber; | ||
|
||
use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; | ||
use Drupal\helfi_api_base\Environment\Project; | ||
use Drupal\helfi_navigation\Event\MenuTreeBuilderLink; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Evaluates whether the global menu links can omit the domain. | ||
*/ | ||
final class AbsoluteUrlMenuTreeBuilderLinkSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface $environmentResolver | ||
* The environment resolver. | ||
* @param bool|null $mustBeAbsoluteUrl | ||
* Determines whether the link must be absolute URL. | ||
*/ | ||
public function __construct( | ||
private readonly EnvironmentResolverInterface $environmentResolver, | ||
private ?bool $mustBeAbsoluteUrl = NULL, | ||
) { | ||
} | ||
|
||
/** | ||
* Evaluates whether the links can omit the domain. | ||
* | ||
* @return bool | ||
* TRUE if the link must be absolute URL. | ||
*/ | ||
private function mustBeAbsolute() : bool { | ||
if ($this->mustBeAbsoluteUrl !== NULL) { | ||
return $this->mustBeAbsoluteUrl; | ||
} | ||
|
||
try { | ||
$activeEnvironment = $this->environmentResolver->getActiveEnvironment(); | ||
$matchingEnvironment = $this->environmentResolver->getProject(Project::ETUSIVU) | ||
->getEnvironment($activeEnvironment->getEnvironmentName()); | ||
|
||
// By default, links are either absolute (external) or relative. This | ||
// should already be evaluated by MenuTreeBuilder service, so the | ||
// only thing left for us to do here is to determine whether the | ||
// Etusivu's domain matches instance's current domain. | ||
return $this->mustBeAbsoluteUrl = $matchingEnvironment->getBaseUrl() !== $activeEnvironment->getBaseUrl(); | ||
} | ||
catch (\InvalidArgumentException) { | ||
} | ||
return FALSE; | ||
} | ||
|
||
/** | ||
* Responds to MenuTreeBuilderLink event. | ||
* | ||
* @param \Drupal\helfi_navigation\Event\MenuTreeBuilderLink $link | ||
* The event to respond to. | ||
*/ | ||
public function updateLink(MenuTreeBuilderLink $link) : void { | ||
if (!$this->mustBeAbsolute()) { | ||
return; | ||
} | ||
$link->url->setAbsolute(TRUE); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() : array { | ||
return [ | ||
MenuTreeBuilderLink::class => ['updateLink'], | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation\EventSubscriber; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation\Plugin\Block; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_navigation\Plugin\Block; | ||
|
||
|
Oops, something went wrong.