Skip to content

Commit

Permalink
Merge pull request #122 from localgovdrupal/2.x
Browse files Browse the repository at this point in the history
Release 2.3.1
  • Loading branch information
andybroomfield authored Jul 12, 2023
2 parents db261f2 + e78d206 commit f0304cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/Plugin/Block/SubsitesNavigationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Node\NodeInterface;

/**
* Class SubsiteNavigationBlock.
Expand Down Expand Up @@ -56,8 +57,25 @@ public function build() {
$mapper = \Drupal::service('entity_hierarchy.entity_tree_node_mapper');
$entities = $mapper->loadAndAccessCheckEntitysForTreeNodes('node', $tree, $cache);
$items = $this->nestTree($tree, $ancestors, $entities);
$subsite_id = $entities[$ancestors[0]]->id();
$overview_entity = $entities[$ancestors[0]];

// Sometimes the subsite is missing if the overview is unpublished and
// the user is anonymous, in which case set it to NULL.
if (!empty($entities[$ancestors[0]]) && $entities[$ancestors[0]] instanceof NodeInterface) {
$subsite_id = $entities[$ancestors[0]]->id();
$overview_entity = $entities[$ancestors[0]];
$cache->addCacheableDependency($overview_entity);
}
else {
$subsite_id = NULL;
$overview_entity = NULL;

// But we still need the parent subsite for cache purposes.
$entities = $mapper->loadEntitiesForTreeNodesWithoutAccessChecks('node', $tree, $cache);
if (!empty($entities[$ancestors[0]]) && $entities[$ancestors[0]] instanceof NodeInterface) {
$overview_entity_for_cache = $entities[$ancestors[0]];
$cache->addCacheableDependency($overview_entity_for_cache);
}
}
}
elseif ($subsite_entity->bundle('localgov_subsites_overview')) {
// Subsite overview page with no children.
Expand Down
4 changes: 3 additions & 1 deletion templates/subsite-navigation.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
{% if item.in_active_trail %}
{{ item.title }}
{% else %}
{{ link(item.title, item.url) }}
{% if item.url is not empty %}
{{ link(item.title, item.url) }}
{% endif %}
{% endif %}
{% if item.below %}
{{ subsite_navigation.menu_links(item.below, attributes, menu_level + 1) }}
Expand Down
11 changes: 11 additions & 0 deletions tests/src/Functional/SubsiteBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ public function testSubsiteNavigationBlock() {
$this->assertSession()->responseNotContains('block-localgov-subsite-navigation');
$this->drupalGet($subsite_page1->toUrl()->toString());
$this->assertSession()->responseNotContains('block-localgov-subsite-navigation');

// Test the navigation is still avalible with an unpublished overview node.
$subsite_overview->set('localgov_subsites_hide_menu', ['value' => 0]);
$subsite_overview->set('status', NodeInterface::NOT_PUBLISHED);
$subsite_overview->save();
drupal_flush_all_caches();
$this->drupalGet($subsite_page1->toUrl()->toString());
$this->assertSession()->responseContains('block-localgov-subsite-navigation');
$this->assertSession()->responseContains($subsite_page1_title);
$this->assertSession()->responseContains($subsite_page2_title);
$this->assertSession()->pageTextNotContains($subsite_overview_title);
}

}

0 comments on commit f0304cb

Please sign in to comment.