diff --git a/src/Folder.php b/src/Folder.php index 925962d1..5e2f06ac 100644 --- a/src/Folder.php +++ b/src/Folder.php @@ -92,16 +92,9 @@ public function onBeforeDelete() public function onBeforeWrite() { - // Make sure Title is updated if Name is chaged (as we keep these in sync for folders) - if ($this->isChanged('Name')) { - $name = $this->Name; - $this->setField( - 'Title', - str_replace(array('-','_'), ' ', preg_replace('/\.[^.]+$/', '', $name)) - ); - } - parent::onBeforeWrite(); + + $this->Title = $this->getField('Name'); } /** diff --git a/tests/php/FolderTest.php b/tests/php/FolderTest.php index b9623c8f..6f26e1d7 100644 --- a/tests/php/FolderTest.php +++ b/tests/php/FolderTest.php @@ -293,6 +293,19 @@ public function testTitleTiedToName() $newFolder->Title = 'TestTitleWithIllegalCharactersCopiedToName '; $this->assertEquals($newFolder->Name, $newFolder->Title); $this->assertEquals($newFolder->Title, 'TestTitleWithIllegalCharactersCopiedToName '); + + // Title should be populated from name on first write + $writeFolder = new Folder(); + $writeFolder->Name = 'TestNameWrittenToTitle'; + $writeFolder->write(); + $newFolderWritten = Folder::get_one(Folder::class, "\"Title\" = 'TestNameWrittenToTitle'"); + $this->assertNotNull($newFolderWritten); + + // Title should be populated from name on subsequent writes + $writeFolder->Name = 'TestNameWrittenToTitleOnUpdate'; + $writeFolder->write(); + $newFolderWritten = Folder::get_one(Folder::class, "\"Title\" = 'TestNameWrittenToTitleOnUpdate'"); + $this->assertNotNull($newFolderWritten); } public function testRootFolder()