diff --git a/README.md b/README.md index 0c346d6..37eebc2 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ The master repository for all menus is `Etusivu`-instance - Fetch aggregated main-navigation from Etusivu-instance. - Fetch global header and footer navigations from Etusivu-instance. - Render Fetched navigations with blocks. +- Custom REST-endpoint for mobile navigation: `/api/v1/global-mobile-menu` ### Main-navigation syncing @@ -51,3 +52,8 @@ Only main-navigation has syncing option. Other navigations are created in Etusiv 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 +`Disable cache` checkbox and reload the page. diff --git a/helfi_navigation.services.yml b/helfi_navigation.services.yml index 5be6fd6..ba47e3b 100644 --- a/helfi_navigation.services.yml +++ b/helfi_navigation.services.yml @@ -39,7 +39,6 @@ services: - '@language_manager' - '@cache_tags.invalidator' - '@helfi_navigation.api_manager' - helfi_navigation.api_authorization: class: Drupal\helfi_navigation\ApiAuthorization arguments: diff --git a/src/Menu/MenuTreeBuilder.php b/src/Menu/MenuTreeBuilder.php index faeb81c..a7dce4b 100644 --- a/src/Menu/MenuTreeBuilder.php +++ b/src/Menu/MenuTreeBuilder.php @@ -127,7 +127,7 @@ private function transform(array $menuItems, string $langcode, string $rootId = if (!$link = $this->getEntity($element->link, $langcode)) { continue; } - $this->evaluateEntityAccess($element); + $this->evaluateEntityAccess($element, $langcode); // Only show accessible links. if ($element->access instanceof AccessResultInterface && !$element->access->isAllowed()) { @@ -267,8 +267,10 @@ private function getEntity(MenuLinkInterface $link, string $langcode): ? MenuLin * * @param \Drupal\Core\Menu\MenuLinkTreeElement $element * The element to check entity access for. + * @param string $langcode + * Menu tree language. */ - private function evaluateEntityAccess(MenuLinkTreeElement $element) : void { + private function evaluateEntityAccess(MenuLinkTreeElement $element, string $langcode) : void { // Attempt to fetch the entity type and id from link's route parameters. // The route parameters should be an array containing entity type => id // like: ['node' => '1']. @@ -284,6 +286,9 @@ private function evaluateEntityAccess(MenuLinkTreeElement $element) : void { if (!$entity = $storage->load($routeParameters[$entityType])) { return; } + + $entity = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity; + if (!$entity->access('view')) { // Disallow access if user has no view access to the target entity. // This updates the existing access result and will be evaluated in diff --git a/tests/src/Kernel/MenuTreeBuilderTest.php b/tests/src/Kernel/MenuTreeBuilderTest.php index 464e193..425610a 100644 --- a/tests/src/Kernel/MenuTreeBuilderTest.php +++ b/tests/src/Kernel/MenuTreeBuilderTest.php @@ -143,6 +143,37 @@ public function testBuildMenuTree() : void { // Make sure no links are visible after the node was unpublished. $tree = $this->getMenuTree('fi'); $this->assertCount(0, $tree['sub_tree']); + + // Enable all english node translations. + foreach ($this->nodes as $node) { + if ($node->hasTranslation('en')) { + $translation = $node->getTranslation('en'); + $translation->setPublished()->save(); + } + } + + // Rebuild the container to empty static entity cache. + $this->container->get('kernel')->rebuildContainer(); + + // Make sure english nodes are enabled. + $tree = $this->getMenuTree('en'); + $this->assertCount(3, $tree['sub_tree']); + + // Disable all english node translations. + foreach ($this->nodes as $node) { + if ($node->hasTranslation('en')) { + $translation = $node->getTranslation('en'); + $translation->setUnpublished()->save(); + } + } + + // Rebuild the container to empty static entity cache. + $this->container->get('kernel')->rebuildContainer(); + + // Make sure english nodes disappear. + $tree = $this->getMenuTree('en'); + $this->assertCount(2, $tree['sub_tree']); + } } diff --git a/tests/src/Unit/MenuTreeBuilderTest.php b/tests/src/Unit/MenuTreeBuilderTest.php index b85ae85..f9f4b36 100644 --- a/tests/src/Unit/MenuTreeBuilderTest.php +++ b/tests/src/Unit/MenuTreeBuilderTest.php @@ -46,7 +46,7 @@ public function testRootElementException(\stdClass $rootElement) : void { new InternalDomainResolver(), $menuTree->reveal(), $this->prophesize(MenuLinkManagerInterface::class)->reveal(), - $this->prophesize(EventDispatcherInterface::class)->reveal(), + $this->prophesize(EventDispatcherInterface::class)->reveal() ); $menuTreeBuilder->build('main', 'en', $rootElement); }