From 5d1df63122f42639eeae38d323fe0139f17c42ee Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Fri, 20 Sep 2019 14:19:54 +1200 Subject: [PATCH] NEW ability to configure content field replacement Globally or per class the ElementalAreasExtension is applied to. This allows for flexibility where a developer may want Elemental to detour from its default functionality that removes the Content field on whatever object it is applied to (for whatever reason, e.g. migration from another module that did not replace the field). This change defines that settings applied to the class the extension is applied to will override the global setting. This allows for both new page types to retain elemental's default functionality, while providing the flexibility to retain e.g. migrated content on older page types. --- docs/en/content_migration.md | 20 ++++++++++ src/Extensions/ElementalAreasExtension.php | 11 ++++-- .../ElementalAreasExtensionTest.php | 38 +++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/docs/en/content_migration.md b/docs/en/content_migration.md index 0475dd7e..fbba2cef 100644 --- a/docs/en/content_migration.md +++ b/docs/en/content_migration.md @@ -12,6 +12,26 @@ content for you. It will: There are several configuration options and extension hooks to allow customising the functionality of this class. +### Choosing not to replace the standard content editor + +If you wish to apply elemental to pages but still retain the default `Content` field, use the config setting + +```yml +DNADesign\Elemental\Extensions\ElementalAreasExtension: + keep_content_fields: true +``` + +**Note** This setting is globally applied. If you wish to replace the default Content area for all but a few +select page types, you can instead add a config option to that class + +```yml +YourVendor\YourProject\Pages\SpecialBlockAndContentPage: + elemental_keep_content_field: true +``` + +The owner class config setting will always take precedence over the global setting (on the extension). +This makes it possible to e.g. keep content fields globally, except for select page types. + ### Configuring the element that is created You may configure which element content is migrated to by using the following configuration: diff --git a/src/Extensions/ElementalAreasExtension.php b/src/Extensions/ElementalAreasExtension.php index b7dd4177..ed0384c4 100644 --- a/src/Extensions/ElementalAreasExtension.php +++ b/src/Extensions/ElementalAreasExtension.php @@ -73,11 +73,13 @@ class ElementalAreasExtension extends DataExtension /** * Whether or not to replace the default SiteTree content field + * Applies globally, across all page types; unless a page type overrides this with its own config setting of + * `elemental_keep_content_field` * * @var boolean * @config */ - private static $replace_content_field = true; + private static $keep_content_fields = false; /** * Get the available element types for this page type, @@ -176,9 +178,10 @@ public function updateCMSFields(FieldList $fields) return; } - // add an empty holder for content as some module explicitly use insert - // after content. - if (Config::inst()->get(ElementalAreasExtension::class, 'replace_content_field')) { + // add an empty holder for content as some module explicitly use insert after content + $globalReplace = !Config::inst()->get(self::class, 'keep_content_fields'); + $classOverride = Config::inst()->get(get_class($this->owner), 'elemental_keep_content_field'); + if ($globalReplace && !$classOverride || $classOverride === false) { $fields->replaceField('Content', new LiteralField('Content', '')); } $elementalAreaRelations = $this->owner->getElementalRelations(); diff --git a/tests/Extensions/ElementalAreasExtensionTest.php b/tests/Extensions/ElementalAreasExtensionTest.php index f96066ef..97752fc7 100644 --- a/tests/Extensions/ElementalAreasExtensionTest.php +++ b/tests/Extensions/ElementalAreasExtensionTest.php @@ -6,8 +6,11 @@ use DNADesign\Elemental\Models\ElementContent; use DNADesign\Elemental\Tests\Src\TestElement; use DNADesign\Elemental\Tests\Src\TestUnusedElement; +use SilverStripe\Core\Config\Config; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Dev\SapphireTest; +use SilverStripe\Forms\HTMLEditor\HTMLEditorField; +use SilverStripe\Forms\LiteralField; class ElementalAreasExtensionTest extends SapphireTest { @@ -72,4 +75,39 @@ private function assertContainsInOrder(array $expected, array $actual) $this->assertSame($expected, $matches); } + + /** + * @dataProvider provideContentFieldPreservationSettings + */ + public function testContentFieldsAreRemovedByDefault($keepGlobal, $keepClass, $expectedType) + { + Config::inst()->set(ElementalAreasExtension::class, 'keep_content_fields', $keepGlobal); + Config::inst()->set(SiteTree::class, 'elemental_keep_content_field', $keepClass); + $page = SiteTree::create(); + $fields = $page->getCMSFields(); + $this->assertInstanceOf($expectedType, $fields->fieldByName('Root.Main.Content')); + } + + /** + * Provide data for testing both settings and override precedence of Content field replacement + * Settings provided as: + * - ElementalAreasExtension.keep_content_fields (the global setting) + * - SiteTree.elemental_keep_content_field (the class level setting - should take precedence) + * - The expected class of the Field in the FieldList (LiteralField OR HTMLEditorField) + * + * @return array + */ + public function provideContentFieldPreservationSettings() + { + // Test both unset (null) and explicitly declined (false) where applicable. + return [ + [null, null, LiteralField::class], + [false, false, LiteralField::class], + [true, null, HTMLEditorField::class], + [true, false, LiteralField::class], + [null, true, HTMLEditorField::class], + [false, true, HTMLEditorField::class], + [true, true, HTMLEditorField::class], + ]; + } }