Skip to content

StringTagField: array to string conversion on saving #218

@micschk

Description

@micschk

Array to string conversion on line 296 in .../silverstripe/tagfield/src/StringTagField.php:

289     /**
290      * Ensure that arrays are imploded before being saved
291      *
292      * @return mixed|string
293      */
294     public function dataValue()
295     {
296         return implode(',', $this->value);
297     }

It appears $this->value is either a plain array, or an associative array of Title => Value pairs (when altered) at this point:
print_r($this->value) -> Array ( [0] => Array ( [Title] => one [Value] => one ) )

Seems looping over the items and using the 'Value' if defined, string item itself otherwise, fixes this:

public function dataValue()
{
//        return implode(',', $this->value);
        $values = [];
        foreach($this->value as $string_or_assoc_array_value){
            $values[] = is_array($string_or_assoc_array_value) ? $string_or_assoc_array_value['Value'] : $string_or_assoc_array_value;
        }
        return implode(',', $values);
}

If anyone can reproduce and this quick-fix seems acceptable, I'd be happy to create a PR for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions