mirror of
https://github.com/silverstripe/silverstripe-tagfield
synced 2024-10-22 11:05:32 +02:00
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
This commit is contained in:
parent
8477911890
commit
0a04e35825
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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'], []);
|
||||
|
Loading…
Reference in New Issue
Block a user