Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Dec 21, 2023
1 parent 03730d9 commit f2f6a9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
27 changes: 23 additions & 4 deletions src/TagField.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ public function saveInto(DataObjectInterface $record)
$fieldName = $this->getName();
$values = $this->getValueArray();

// We need to extract IDs as in some cases (Relation) we are unable to use the value field
$ids = [];

if (!$values) {
$values = [];
}
Expand All @@ -585,6 +588,7 @@ public function saveInto(DataObjectInterface $record)
continue;
}

$ids[] = $tag->ID;
$cleanValues[] = $tag->{$valueField};
}

Expand All @@ -595,7 +599,7 @@ public function saveInto(DataObjectInterface $record)

if ($relation instanceof Relation) {
// Save values into relation
$relation->setByIDList(array_filter($cleanValues ?? []));
$relation->setByIDList(array_filter($ids ?? []));
} elseif ($this->getAllowRawValue()) {
// Store raw data without serialisation
$record->{$fieldName} = $cleanValues;
Expand All @@ -607,8 +611,23 @@ public function saveInto(DataObjectInterface $record)
// ... JSON-encoded string for other fields
: $this->stringEncode(array_filter(array_values($cleanValues)));
} else {
$record->{$fieldName} = $tag && $tag->{$valueField}
? $tag->{$valueField}
// Detect has one as this case needs ID as opposed to custom value
$relations = $record->hasOne();
$hasOneDetected = false;

foreach ($relations as $relationName => $relationTarget) {
$foreignKey = $relationName . 'ID';

if ($foreignKey === $fieldName) {
$hasOneDetected = true;

break;
}
}

$targetField = $hasOneDetected ? 'ID' : $valueField;
$record->{$fieldName} = $tag && $tag->{$targetField}
? $tag->{$targetField}
: null;
}
}
Expand Down Expand Up @@ -708,7 +727,7 @@ protected function getTags($term)
// Map into a distinct list
$items = [];

foreach ($list->getGenerator() as $record) {
foreach ($list as $record) {
$value = $record->{$valueField};
$items[$value] = [
'Title' => $record->{$titleField},
Expand Down
8 changes: 7 additions & 1 deletion tests/Stub/TagFieldTestBlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\TagField\Tests\Stub\TagFieldTestBlogTag;
use SilverStripe\ORM\ManyManyList;

/**
* @property string $Content
* @property int $PrimaryTagID
* @method TagFieldTestBlogTag PrimaryTag()
* @method ManyManyList|TagFieldTestBlogTag[] Tags()
*/
class TagFieldTestBlogPost extends DataObject implements TestOnly
{
private static $table_name = 'TagFieldTestBlogPost';
Expand Down
5 changes: 4 additions & 1 deletion tests/Stub/TagFieldTestBlogTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\TagField\Tests\Stub\TagFieldTestBlogPost;
use SilverStripe\ORM\ManyManyList;

/**
* @method ManyManyList|TagFieldTestBlogPost[] BlogPosts()
*/
class TagFieldTestBlogTag extends DataObject implements TestOnly
{
private static $table_name = 'TagFieldTestBlogTag';
Expand Down

0 comments on commit f2f6a9c

Please sign in to comment.