Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: put current page at top of ClassName dropdown (was broken) #3004

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -2744,8 +2744,6 @@ public function isNew()
protected function getClassDropdown()
{
$classes = SiteTree::page_type_classes();
$currentClass = null;

$result = [];
foreach ($classes as $class) {
$instance = singleton($class);
Expand All @@ -2771,20 +2769,15 @@ protected function getClassDropdown()
}
}

$pageTypeName = $instance->i18n_singular_name();

$currentClass = $class;
$result[$class] = $pageTypeName;
$result[$class] = $instance->i18n_singular_name();
}

// sort alphabetically, and put current on top
// Sort alphabetically, and put current on top
asort($result);
if ($currentClass) {
$currentPageTypeName = $result[$currentClass];
unset($result[$currentClass]);
$result = array_reverse($result ?? []);
$result[$currentClass] = $currentPageTypeName;
$result = array_reverse($result ?? []);
if (isset($result[$this->ClassName])) {
$currentPageTypeName = $result[$this->ClassName];
unset($result[$this->ClassName]);
$result = [$this->ClassName => $currentPageTypeName] + $result;
}

return $result;
Expand Down
18 changes: 16 additions & 2 deletions tests/php/Model/SiteTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ public function testAllowedChildrenContainsCoreSubclassesButNotHiddenClass()
*/
public function testAllowedChildren($className, $expected, $assertionMessage)
{
$class = new $className;
$class = new $className();
$this->assertEquals($expected, $class->allowedChildren(), $assertionMessage);
}

Expand Down Expand Up @@ -1359,6 +1359,9 @@ public function testAllowedChildrenValidation()
);
}

/**
* @return void
*/
Comment on lines +1362 to +1364
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @return void
*/

We don't need this typehint, but I won't delay merging just for this.

public function testClassDropdown()
{
$sitetree = new SiteTree();
Expand All @@ -1380,7 +1383,18 @@ public function testClassDropdown()

$this->assertArrayNotHasKey(SiteTreeTest_NotRoot::class, $method->invoke($rootPage));
$this->assertArrayHasKey(SiteTreeTest_NotRoot::class, $method->invoke($nonRootPage));

foreach ([SiteTreeTest_ClassA::class, SiteTreeTest_ClassB::class] as $className) {
$otherPage = new $className();
$otherPage->write();
$result = $method->invoke(object: $otherPage);
$this->assertEquals(array_key_first($result), $className);
// remove the first element as this is not alphabetical
array_shift($result);
// create a sorted array
$resultSorted = $result;
asort($resultSorted);
$this->assertEquals($result, $resultSorted);
}
Security::setCurrentUser(null);
}

Expand Down
Loading