From e826152fd1d316d9aa010764e3ca1ca6f99efa1d Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 2 Nov 2023 15:51:51 +1300 Subject: [PATCH] API Add new SiteTree.hide_pagetypes configuration Allows clearly defining all pagetypes that should be hidden in a single place, instead of having to spread them out across different pagetypes with MyPage.hide_ancestor --- code/Model/SiteTree.php | 13 +++++++++++-- tests/php/Model/SiteTreeTest.php | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index aefd7001c1..a26fd010c6 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -203,11 +203,20 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi * in the cms, set this to the old class name. Eg, if you extended Product * to make ImprovedProduct, then you would set $hide_ancestor to Product. * + * @deprecated 5.2.0 Use hide_pagetypes instead + * * @config * @var string */ private static $hide_ancestor = null; + /** + * Any fully qualified class names added to this array will be hidden in the CMS + * when selecting page types, e.g. for creating a new page or changing the type + * of an existing page. + */ + private static array $hide_pagetypes = []; + /** * You can define the class of the controller that maps to your SiteTree object here if * you don't want to rely on the magic of appending Controller to the Classname @@ -538,7 +547,7 @@ public static function get_by_link($link, $cache = true) } /** - * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_ancestor} + * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_pagetypes} * * @return array */ @@ -551,7 +560,7 @@ public static function page_type_classes() unset($classes[$baseClassIndex]); } - $kill_ancestors = []; + $kill_ancestors = self::config()->get('hide_pagetypes', Config::UNINHERITED) ?? []; // figure out if there are any classes we don't want to appear foreach ($classes as $class) { diff --git a/tests/php/Model/SiteTreeTest.php b/tests/php/Model/SiteTreeTest.php index a0afb4cc63..f35d6c3137 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -1209,6 +1209,13 @@ public function testVersionsAreCreated() $this->assertEquals(3, $p->Version); } + public function testHidePagetypes() + { + SiteTree::config()->set('hide_pagetypes', ['Page']); + $classes = SiteTree::page_type_classes(); + $this->assertNotContains('Page', $classes); + } + public function testPageTypeClasses() { $classes = SiteTree::page_type_classes(); @@ -1223,6 +1230,11 @@ public function testPageTypeClasses() $newClasses, 'Setting hide_ancestor to a boolean (incorrect) value caused a page class to be hidden' ); + + // Testing what happens if a valid config value is set + Config::modify()->set(SiteTreeTest_ClassA::class, 'hide_ancestor', 'Page'); + $classes = SiteTree::page_type_classes(); + $this->assertNotContains('Page', $classes); } /**