From 0a04e35825764376ad29fb48613ae998b051b604 Mon Sep 17 00:00:00 2001 From: Daniel Kliemsch Date: Mon, 4 Apr 2022 10:27:49 +0200 Subject: [PATCH] ENH Make the write call of StringTagField::saveInto method configurable (#208) * Make the immediate write call of StringTagField::saveInto method configurable * Set immediate_write_enabled option in the class instead of config; add deprecation notice * Add tests for the immediate write option --- src/StringTagField.php | 12 +++++++++++- tests/StringTagFieldTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/StringTagField.php b/src/StringTagField.php index f8cd49b..27bf13a 100644 --- a/src/StringTagField.php +++ b/src/StringTagField.php @@ -33,6 +33,13 @@ class StringTagField extends DropdownField 'suggest', ]; + /** + * @var bool Triggers a write call within the saveInto function if enabled + * + * @deprecated 3.0.0 + */ + private static $immediate_write_enabled = true; + /** * @var bool */ @@ -273,7 +280,10 @@ class StringTagField extends DropdownField $name = $this->getName(); $record->$name = $this->dataValue(); - $record->write(); + + if (self::config()->get('immediate_write_enabled')) { + $record->write(); + } } /** diff --git a/tests/StringTagFieldTest.php b/tests/StringTagFieldTest.php index 74bb388..c56e69f 100755 --- a/tests/StringTagFieldTest.php +++ b/tests/StringTagFieldTest.php @@ -61,6 +61,38 @@ class StringTagFieldTest extends SapphireTest $this->assertEquals('Tag1,Tag2', $record->Tags); } + public function testImmediateWriteEnabled() + { + $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); + $record->write(); + + StringTagField::config()->update('immediate_write_enabled', true); + + $field = new StringTagField('Tags'); + $field->setValue(['Tag1', 'Tag2']); + $field->saveInto($record); + + $this->assertEquals('Tag1,Tag2', StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags); + } + + public function testImmediateWriteDisabled() + { + $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); + $record->write(); + + StringTagField::config()->update('immediate_write_enabled', false); + + $field = new StringTagField('Tags'); + $field->setValue(['Tag1', 'Tag2']); + $field->saveInto($record); + + $this->assertNull(StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags); + + $record->write(); + + $this->assertEquals('Tag1,Tag2', StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags); + } + public function testItSuggestsTags() { $field = new StringTagField('SomeField', 'Some field', ['Tag1', 'Tag2'], []);