From bc7bf5265b1607c9af466c8092f9d4ef05096afd Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Tue, 28 Jun 2022 09:29:32 +0200 Subject: [PATCH 1/5] fix: Deal with DataObjects or Int values, so that the correct value is shown with has_one --- src/TagField.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/TagField.php b/src/TagField.php index 177ab23..7e45d46 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -402,6 +402,14 @@ class TagField extends MultiSelectField return $values->column($this->getTitleField()); } + if ($values instanceof DataObject && $values->exists()) { + return [$values->ID]; + } + + if (is_int($values)) { + return [$values]; + } + return [trim((string) $values)]; } From e34d6e4898ff162b76e612eb39bf33a3f2e248ed Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Mon, 4 Jul 2022 16:10:02 +0200 Subject: [PATCH 2/5] fix: Use the title-field for has_one DataObject relations --- src/TagField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TagField.php b/src/TagField.php index 7e45d46..c8df287 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -403,7 +403,7 @@ class TagField extends MultiSelectField } if ($values instanceof DataObject && $values->exists()) { - return [$values->ID]; + return [$values->getField($this->getTitleField())]; } if (is_int($values)) { From 560a1398ab9b9a947d962afc63fb37421ff92bd5 Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Mon, 25 Jul 2022 16:49:23 +0200 Subject: [PATCH 3/5] fix: Do not use `getField` to allow for field fallback values --- src/TagField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TagField.php b/src/TagField.php index c8df287..80df94e 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -403,7 +403,7 @@ class TagField extends MultiSelectField } if ($values instanceof DataObject && $values->exists()) { - return [$values->getField($this->getTitleField())]; + return [$values->{$this->getTitleField()}]; } if (is_int($values)) { From 8da9f83cde9976d5acb5155a03fcd7b0b6f0848b Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Tue, 26 Jul 2022 10:47:13 +0200 Subject: [PATCH 4/5] fix: Fall back to the `ID` if no title is given --- src/TagField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TagField.php b/src/TagField.php index 80df94e..f280634 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -403,7 +403,7 @@ class TagField extends MultiSelectField } if ($values instanceof DataObject && $values->exists()) { - return [$values->{$this->getTitleField()}]; + return [$values->{$this->getTitleField()} ?? $values->ID]; } if (is_int($values)) { From 3f1d65e88e6d8ceb2bc2e1e80050f5f720cd7bd9 Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Thu, 28 Jul 2022 10:48:33 +0200 Subject: [PATCH 5/5] docs: Update readme with an example for `has_one` relations. --- readme.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/readme.md b/readme.md index fccac8a..8ad0e31 100644 --- a/readme.md +++ b/readme.md @@ -73,6 +73,45 @@ $field = TagField::create( **Note:** This assumes you have imported the namespaces class, e.g. use SilverStripe\TagField\TagField; + +#### Has-One Relations + +You can also use the TagField to select values for `has_one` relations. +Let's assume, that a `BlogPost` *has one* `BlogCategory`. + +```php +class BlogCategory extends DataObject +{ + private static $db = [ + 'Title' => 'Varchar(200)', + ]; +} +``` + +```php +use SilverStripe\ORM\DataObject; + +class BlogPost extends DataObject +{ + private static $has_one = [ + 'BlogCategory' => BlogCategory::class + ]; +} +``` + +```php +$field = TagField::create( + 'BlogCategoryID', + $this->fieldLabel('BlogCategory'), + BlogCategory::get() +) + ->setIsMultiple(false) + ->setCanCreate(true); +``` + +**Note:** We're using the `ID` suffix for the field-name (eg. `BlogCategoryID` instead of `BlogCategory`) and +only allow one value by setting `->setIsMultiple(false)` + ### String Tags ```php