diff --git a/src/Folder.php b/src/Folder.php index 2d74e00d..5e2f06ac 100644 --- a/src/Folder.php +++ b/src/Folder.php @@ -90,6 +90,13 @@ public function onBeforeDelete() parent::onBeforeDelete(); } + public function onBeforeWrite() + { + parent::onBeforeWrite(); + + $this->Title = $this->getField('Name'); + } + /** * Return the relative URL of an icon for this file type * 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()