Skip to content

Fix src saving as an array and not as string#173

Merged
ncla merged 1 commit intospatie:mainfrom
ncla:fix/issue-172
Oct 16, 2022
Merged

Fix src saving as an array and not as string#173
ncla merged 1 commit intospatie:mainfrom
ncla:fix/issue-172

Conversation

@ncla
Copy link
Copy Markdown
Collaborator

@ncla ncla commented Oct 14, 2022

Fixes #172.

As far as I understood, process method on fieldtypes is used for when field is being saved, and preProcess is for displaying the field. In ResponsiveFieldtype@process we are calling preProcess for all fields, when it should be just process. We are saving data there, not displaying.

Code from Assets fieldtype.

public function preProcess($values)
{
    if (is_null($values)) {
        return [];
    }

    return collect($values)->map(function ($value) {
        return $this->valueToId($value);
    })->filter()->values()->all();
}
public function process($data)
{
    $max_files = (int) $this->config('max_files');

    $values = collect($data)->map(function ($id) {
        return Asset::find($id)->path();
    });

    return $this->config('max_files') === 1 ? $values->first() : $values->all();
}

As you can see, core Assets fieldtype handles max_files being 1 only when saving. When preProcessing or displaying the data, it uses collection/array.

I am guessing you are not suppose to mix and match process and preProcess together in either one.

@ncla ncla merged commit 03aaddf into spatie:main Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

src values should be saved as string instead of an array in content files

1 participant