spatie / statamic-responsive-images

Responsive images for Statamic 3
MIT License
99 stars 29 forks source link

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

Closed ncla closed 1 year ago

ncla commented 1 year ago

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.