From fb35183e497e27895467917a405206fe4bfc32e5 Mon Sep 17 00:00:00 2001 From: wakqasahmed Date: Sun, 23 Aug 2026 10:08:45 +0200 Subject: [PATCH] Fix nav:breadcrumbs including home when include_home is false (#12584) When include_home is false, array_shift() removed the leading home segment, but a root URL left a trailing empty segment that URL::tidy() normalized back to '/', which then resolved to the Home entry. Filter out empty segments after the shift so they never reach tidy()/findByUri(). --- src/Tags/Nav.php | 1 + tests/Tags/NavBreadcrumbsTest.php | 78 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/Tags/NavBreadcrumbsTest.php diff --git a/src/Tags/Nav.php b/src/Tags/Nav.php index fc1ff19ccea..ee09c0b7761 100644 --- a/src/Tags/Nav.php +++ b/src/Tags/Nav.php @@ -23,6 +23,7 @@ public function breadcrumbs() if (! $this->params->bool('include_home', true)) { array_shift($segments); + $segments = array_values(array_filter($segments, fn ($segment) => $segment !== '')); } $crumbs = collect($segments)->map(function () use (&$segments) { diff --git a/tests/Tags/NavBreadcrumbsTest.php b/tests/Tags/NavBreadcrumbsTest.php new file mode 100644 index 00000000000..d91c0877274 --- /dev/null +++ b/tests/Tags/NavBreadcrumbsTest.php @@ -0,0 +1,78 @@ +routes('{parent_uri}/{slug}')->structureContents(['root' => true]))->save(); + + EntryFactory::collection('pages')->id('home')->slug('home')->data(['title' => 'Home'])->create(); + EntryFactory::collection('pages')->id('about')->slug('about')->data(['title' => 'About'])->create(); + EntryFactory::collection('pages')->id('team')->slug('team')->data(['title' => 'Team'])->create(); + + $collection->structure()->in('en')->tree([ + ['entry' => 'home'], + ['entry' => 'about', 'children' => [ + ['entry' => 'team'], + ]], + ])->save(); + } + + private function tag($tag) + { + return (string) Parse::template($tag, [], trusted: true); + } + + #[Test] + public function it_includes_home_by_default() + { + $this->get('/about/team'); + + $titles = $this->tag('{{ nav:breadcrumbs }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('Home|About|Team|', $titles); + } + + #[Test] + public function it_excludes_home_when_include_home_is_false() + { + $this->get('/about/team'); + + $titles = $this->tag('{{ nav:breadcrumbs include_home="false" }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('About|Team|', $titles); + } + + #[Test] + public function it_excludes_home_when_include_home_is_false_on_a_top_level_page() + { + $this->get('/about'); + + $titles = $this->tag('{{ nav:breadcrumbs include_home="false" }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('About|', $titles); + } + + #[Test] + public function it_returns_no_breadcrumbs_on_the_home_page_when_include_home_is_false() + { + $this->get('/'); + + $titles = $this->tag('{{ nav:breadcrumbs include_home="false" }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('', $titles); + } +}