Skip to content

Commit 37609e4

Browse files
authored
[5.x] Fix create/edit CP nav descendants not properly triggering active status (#11832)
* Add failing test coverage. * Allow non-explicitly defined restful descendants when checking active state.
1 parent d4b694e commit 37609e4

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

src/CP/Navigation/NavItem.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,21 @@ public function isChild($isChild = null)
281281
->value($isChild);
282282
}
283283

284+
/**
285+
* Check if current url is a restful descendant.
286+
*/
287+
protected function currentUrlIsRestfulDescendant(): bool
288+
{
289+
return (bool) Str::endsWith(request()->url(), [
290+
'/create',
291+
'/edit',
292+
]);
293+
}
294+
284295
/**
285296
* Check if this nav item was ever a child before user preferences were applied.
286-
*
287-
* @param bool|null $isChild
288-
* @return mixed
289297
*/
290-
protected function wasOriginallyChild()
298+
protected function wasOriginallyChild(): bool
291299
{
292300
return (bool) $this->wasOriginallyChild;
293301
}
@@ -382,8 +390,12 @@ public function isActive()
382390
// If the current URL is not explicitly referenced in the CP nav,
383391
// and if this item is/was ever a child nav item,
384392
// then check against URL heirarchy conventions using regex pattern.
385-
if ($this->currentUrlIsNotExplicitlyReferencedInNav() && $this->wasOriginallyChild()) {
386-
return $this->isActiveByPattern($this->active);
393+
if ($this->currentUrlIsNotExplicitlyReferencedInNav()) {
394+
switch (true) {
395+
case $this->currentUrlIsRestfulDescendant():
396+
case $this->wasOriginallyChild():
397+
return $this->isActiveByPattern($this->active);
398+
}
387399
}
388400

389401
return request()->url() === URL::removeQueryAndFragment($this->url);

tests/CP/Navigation/ActiveNavItemTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ public function it_resolves_core_children_closure_and_can_check_when_parent_and_
196196
$this->assertTrue($this->getItemByDisplay($collections->children(), 'Articles')->isActive());
197197
}
198198

199+
#[Test]
200+
public function it_resolves_core_children_closure_and_can_check_when_parent_and_descendant_of_parent_item_is_active()
201+
{
202+
Facades\Collection::make('pages')->title('Pages')->save();
203+
Facades\Collection::make('articles')->title('Articles')->save();
204+
205+
$this
206+
->prepareNavCaches()
207+
->get('http://localhost/cp/collections/create')
208+
->assertStatus(200);
209+
210+
$collections = $this->buildAndGetItem('Content', 'Collections');
211+
212+
$this->assertTrue($collections->isActive());
213+
$this->assertInstanceOf(Collection::class, $collections->children());
214+
$this->assertFalse($collections->children()->keyBy->display()->get('Pages')->isActive());
215+
$this->assertFalse($collections->children()->keyBy->display()->get('Articles')->isActive());
216+
}
217+
199218
#[Test]
200219
public function it_resolves_core_children_closure_and_can_check_when_parent_and_descendant_of_child_item_is_active()
201220
{

0 commit comments

Comments
 (0)