diff --git a/code/TagField.php b/code/TagField.php index fd6ca27..365d4c4 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -332,13 +332,11 @@ class TagField extends DropdownField { */ $source = $this->getSource(); - $dataClass = $source->dataClass(); - $titleField = $this->getTitleField(); $term = Convert::raw2sql($term); - $query = $dataClass::get() + $query = $source ->filter($titleField . ':PartialMatch:nocase', $term) ->sort($titleField) ->limit($this->getLazyLoadItemLimit()); diff --git a/tests/TagFieldTest.php b/tests/TagFieldTest.php index ecc9620..0012906 100755 --- a/tests/TagFieldTest.php +++ b/tests/TagFieldTest.php @@ -147,6 +147,44 @@ class TagFieldTest extends SapphireTest { ); } + /** + * Tests that TagField supports pre-filtered data sources + */ + public function testRestrictedSuggestions() { + $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); + $field = new TagField('Tags', '', $source); + + /** + * Partial tag title match. + */ + $request = $this->getNewRequest(array('term' => 'Tag')); + + $this->assertEquals( + '{"items":[{"id":1,"text":"Tag1"}]}', + $field->suggest($request)->getBody() + ); + + /** + * Exact tag title match. + */ + $request = $this->getNewRequest(array('term' => 'Tag1')); + + $this->assertEquals( + '{"items":[{"id":1,"text":"Tag1"}]}', + $field->suggest($request)->getBody() + ); + + /** + * Excluded item doesn't appear in matches + */ + $request = $this->getNewRequest(array('term' => 'Tag2')); + + $this->assertEquals( + '{"items":[]}', + $field->suggest($request)->getBody() + ); + } + /** * @param array $parameters *