Skip to content

Commit

Permalink
NEW ability to configure content field replacement
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dylan Wagstaff committed Sep 20, 2019
1 parent cd7dd78 commit 5d1df63
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
20 changes: 20 additions & 0 deletions docs/en/content_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions src/Extensions/ElementalAreasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions tests/Extensions/ElementalAreasExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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],
];
}
}

0 comments on commit 5d1df63

Please sign in to comment.