Skip to content

Commit

Permalink
Merge pull request #45 from City-of-Helsinki/UHF-9083
Browse files Browse the repository at this point in the history
UHF-9083: prevent adding link to menu if related content entity translation is not published
  • Loading branch information
rpnykanen authored Oct 4, 2023
2 parents 5c0f648 + 38f5860 commit d625038
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
1 change: 0 additions & 1 deletion helfi_navigation.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ services:
- '@language_manager'
- '@cache_tags.invalidator'
- '@helfi_navigation.api_manager'

helfi_navigation.api_authorization:
class: Drupal\helfi_navigation\ApiAuthorization
arguments:
Expand Down
9 changes: 7 additions & 2 deletions src/Menu/MenuTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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'].
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/src/Kernel/MenuTreeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

}

}
2 changes: 1 addition & 1 deletion tests/src/Unit/MenuTreeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d625038

Please sign in to comment.