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

UHF-9824: Allow relative links #52

Merged
merged 11 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ jobs:
- 3306:3306

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- uses: actions/checkout@v4

- name: Parse $MODULE_NAME from composer.json
run: echo "MODULE_NAME=$(cat composer.json | jq -r .name | awk -F/ '{print $NF}')" >> $GITHUB_ENV
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Only main-navigation has syncing option. Other navigations are created in Etusiv
## Local development

- Setup local Etusivu-instance.
- run `drush upwd helfi-admin 123` to update admin password. it is used as api key in other instances.
- Run `drush upwd helfi-admin 123` to update admin password. It is used as the API key in other instances.
- Setup any other instance with helfi_navigation module enabled.
- Add following line to local.settings.php. Otherwise syncing global navigation won't work
- Add the following line to local.settings.php. Otherwise, syncing global navigation won't work
```php
$config['helfi_api_base.api_accounts']['vault'][] = [
'id' => 'helfi_navigation',
Expand All @@ -49,17 +49,24 @@ Only main-navigation has syncing option. Other navigations are created in Etusiv
];
```

Local environment forces synced links always to be absolute URLs, whereas other environments allow relative URLs.

If you wish to replicate the production setup (when testing through one domain, like `helfi-proxy.docker.so` for example). Add the following line to `local.services.yml`:
```yaml
parameters:
helfi_navigation.absolute_url_always: false
```

### Steps after both instances are up and running.
1. Edit and save menu on any instance with helfi_navigation module enabled.
- When adding new items, make sure both the menu link and the node are enabled. disabled content won't be synced.
2. Run `drush cron`.
- After that you can run `docker compose logs app` to see possible exceptions or if menu sync cron succeeded.
3. Go to Etusivu and run drush cr. The navigations should have been updated
3. Go to Etusivu and run drush cr. The navigation should have been updated
based on the changes you made
4. Instances should fetch the menus from Etusivu and update the related blocks after `drush cr` and page refresh.

## Changes not updating to the global mobile menu?
The global mobile navigation API can be found from `/api/v1/global-mobile-menu` path so check if your changes are
visible there. If they are, the problem is probably caused by caches. The global mobile menu is cached for 24 hours by
the browser. You can clear this cache on Chrome by opening developer tools and on the `Network` tab select the
visible there. If they are, the problem is probably caused by caches. The browser caches the global mobile menu for 24 hours. You can clear this cache on Chrome by opening developer tools and on the `Network` tab select the
`Disable cache` checkbox and reload the page.
11 changes: 10 additions & 1 deletion helfi_navigation.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Contains installation hooks for 'helfi_news_feed' module.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\block\Entity\Block;
use Drupal\Core\Field\BaseFieldDefinition;
Expand Down Expand Up @@ -305,3 +305,12 @@ function helfi_navigation_update_9003() : void {
function helfi_navigation_update_9004() : void {
module_set_weight('helfi_navigation', 1);
}

/**
* Update global "main" navigation links.
*/
function helfi_navigation_update_9005() : void {
foreach (['fi', 'en', 'sv'] as $language) {
_helfi_navigation_queue_item('main', $language, 'update');
}
}
2 changes: 1 addition & 1 deletion helfi_navigation.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Contains alterations for the Hel.fi navigation.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityTypeInterface;
Expand Down
15 changes: 8 additions & 7 deletions helfi_navigation.services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
helfi_navigation.request_timeout: 15
helfi_navigation.absolute_url_always: null
services:
logger.channel.helfi_navigation:
parent: logger.channel_base
Expand Down Expand Up @@ -36,15 +37,15 @@ services:
- '@menu.link_tree'
- '@plugin.manager.menu.link'
- '@event_dispatcher'
helfi_navigation.cache_warmer:
class: Drupal\helfi_navigation\CacheWarmer
arguments:
- '@tempstore.shared'
- '@language_manager'
- '@cache_tags.invalidator'
- '@helfi_navigation.api_manager'
helfi_navigation.api_authorization:
class: Drupal\helfi_navigation\ApiAuthorization
arguments:
- '@config.factory'
- '@helfi_api_base.vault_manager'
helfi_api_base.absolute_url_menu_tree_builder_subscriber:
class: Drupal\helfi_navigation\EventSubscriber\AbsoluteUrlMenuTreeBuilderLinkSubscriber
arguments:
- '@helfi_api_base.environment_resolver'
- '%helfi_navigation.absolute_url_always%'
tags:
- { name: event_subscriber }
2 changes: 1 addition & 1 deletion src/ApiAuthorization.php
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;

Expand Down
2 changes: 1 addition & 1 deletion src/ApiManager.php
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;

Expand Down
90 changes: 0 additions & 90 deletions src/CacheWarmer.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Event/MenuTreeBuilderLink.php
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;

Expand Down
80 changes: 80 additions & 0 deletions src/EventSubscriber/AbsoluteUrlMenuTreeBuilderLinkSubscriber.php
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'],
];
}

}
2 changes: 1 addition & 1 deletion src/EventSubscriber/RedirectEventSubscriber.php
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;

Expand Down
2 changes: 1 addition & 1 deletion src/ExternalMenuBlockInterface.php
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;

Expand Down
6 changes: 3 additions & 3 deletions src/ExternalMenuTreeBuilder.php
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;

Expand Down Expand Up @@ -139,7 +139,7 @@ private function createLink(
'provider' => 'helfi_navigation',
];

// Parse the URL.
$item->link = $item->url;
$item->url = !empty($item->url) ? UrlHelper::parse($item->url) : new Url('<nolink>');
$item->external = $this->domainResolver->isExternal($item->url);

Expand Down Expand Up @@ -190,7 +190,7 @@ private function inActiveTrail(object $item): bool {
throw new \LogicException('Request is not set.');
}
$currentPath = parse_url($request->getUri(), PHP_URL_PATH);
$linkPath = parse_url($item->url->getUri(), PHP_URL_PATH);
$linkPath = parse_url($item->link, PHP_URL_PATH);

// We don't care about the domain when comparing URLs because the
// site might be served from multiple different domains.
Expand Down
2 changes: 1 addition & 1 deletion src/HelfiNavigationServiceProvider.php
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;

Expand Down
2 changes: 1 addition & 1 deletion src/MainMenuManager.php
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;

Expand Down
4 changes: 2 additions & 2 deletions src/Menu/MenuTreeBuilder.php
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\Menu;

Expand Down Expand Up @@ -218,7 +218,7 @@ private function processItem(MenuTreeBuilderLink $link) : array {
->dispatch($link);

return array_merge([
'url' => $menuTreeBuilderLink->url->setAbsolute()->toString(TRUE)->getGeneratedUrl(),
'url' => $menuTreeBuilderLink->url->toString(TRUE)->getGeneratedUrl(),
], $menuTreeBuilderLink->item);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Block/ExternalMenuBlock.php
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;

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Block/ExternalMenuBlockBase.php
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;

Expand Down
Loading
Loading