Merge pull request #2902 from creative-commoners/pulls/5/hide-pagetypes

API Add new SiteTree.hide_pagetypes configuration
This commit is contained in:
Sabina Talipova 2023-11-06 11:24:36 +13:00 committed by GitHub
commit cef8c0f179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -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 * 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. * to make ImprovedProduct, then you would set $hide_ancestor to Product.
* *
* @deprecated 5.2.0 Use hide_pagetypes instead
*
* @config * @config
* @var string * @var string
*/ */
private static $hide_ancestor = null; 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 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 * you don't want to rely on the magic of appending Controller to the Classname
@ -538,7 +547,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
} }
/** /**
* 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 * @return array
*/ */
@ -551,7 +560,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
unset($classes[$baseClassIndex]); 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 // figure out if there are any classes we don't want to appear
foreach ($classes as $class) { foreach ($classes as $class) {

View File

@ -1209,6 +1209,13 @@ class SiteTreeTest extends SapphireTest
$this->assertEquals(3, $p->Version); $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() public function testPageTypeClasses()
{ {
$classes = SiteTree::page_type_classes(); $classes = SiteTree::page_type_classes();
@ -1223,6 +1230,11 @@ class SiteTreeTest extends SapphireTest
$newClasses, $newClasses,
'Setting hide_ancestor to a boolean (incorrect) value caused a page class to be hidden' '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);
} }
/** /**