From 8a7fe273a625b4471f4eb1c3ca3661bea642887d Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 6 Jun 2017 11:16:44 +1200 Subject: [PATCH] NEW Allow existing document sets to be linked to pages --- code/extensions/DMSSiteTreeExtension.php | 7 ++++++- tests/dmstest.yml | 2 ++ tests/extensions/DMSSiteTreeExtensionTest.php | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/code/extensions/DMSSiteTreeExtension.php b/code/extensions/DMSSiteTreeExtension.php index 71a2eb3..7f32914 100644 --- a/code/extensions/DMSSiteTreeExtension.php +++ b/code/extensions/DMSSiteTreeExtension.php @@ -20,10 +20,15 @@ class DMSSiteTreeExtension extends DataExtension 'Document Sets', false, $this->owner->DocumentSets(), //->Sort('DocumentSort'), - new GridFieldConfig_RecordEditor + $config = new GridFieldConfig_RelationEditor ); $gridField->addExtraClass('documentsets'); + // Only show document sets in the autocompleter that have not been assigned to a page already + $config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchList( + DMSDocumentSet::get()->filter(array('PageID' => 0)) + ); + $fields->addFieldToTab( 'Root.Document Sets (' . $this->owner->DocumentSets()->count() . ')', $gridField diff --git a/tests/dmstest.yml b/tests/dmstest.yml index dab68b0..686c5df 100644 --- a/tests/dmstest.yml +++ b/tests/dmstest.yml @@ -62,6 +62,8 @@ DMSDocumentSet: dsSaveLinkedDocuments: Title: Test Set 6 Page: =>SiteTree.s9 + unlinked_set: + Title: Document Set not linked DMSDocument: d1: Filename: test-file-file-doesnt-exist-1 diff --git a/tests/extensions/DMSSiteTreeExtensionTest.php b/tests/extensions/DMSSiteTreeExtensionTest.php index 288bee4..db7d952 100644 --- a/tests/extensions/DMSSiteTreeExtensionTest.php +++ b/tests/extensions/DMSSiteTreeExtensionTest.php @@ -82,4 +82,24 @@ class DMSSiteTreeExtensionTest extends SapphireTest } $this->assertCount(0, $siteTree->getAllDocuments()->filter('EmbargoedUntilPublished', true)); } + + /** + * Ensure that document sets that are assigned to pages to not show up in "link existing" autocomplete + * search results + */ + public function testGetRelatedDocumentsForAutocompleter() + { + $page = $this->objFromFixture('SiteTree', 's1'); + $gridField = $page->getCMSFields()->fieldByName('Root.Document Sets (2).Document Sets'); + $this->assertInstanceOf('GridField', $gridField); + + $autocompleter = $gridField->getConfig()->getComponentByType('GridFieldAddExistingAutocompleter'); + $jsonResult = $autocompleter->doSearch( + $gridField, + new SS_HTTPRequest('GET', '/', array('gridfield_relationsearch' => 'Document Set')) + ); + + $this->assertContains('Document Set not linked', $jsonResult); + $this->assertNotContains('Document Set 1', $jsonResult); + } }