Quadrubo / filament-model-settings

Model specific settings for your filament project.
MIT License
15 stars 3 forks source link

Allow customization of settings persistence #19

Open belzaaron opened 2 months ago

belzaaron commented 2 months ago

Summary

This aligns the package to the standard persistence functionality abstraction allowance akin to the EditRecord page within Filament Panel src, see ref: https://github.com/filamentphp/filament/blob/5a8b9994e9356a0d0702b7c307d811496440ef12/packages/panels/src/Resources/Pages/EditRecord.php#L151

Why

This came as I would like an override to provide update-like behavior instead of the included ->apply((array) behavior. (so basically instead- I would use $settings->setMultiple(Arr::dot($data)); within the handleRecordUpdate I define)

Edit: (This ~workaround~ produces the desired "updating"-like functionality)

<?php

namespace App\Filament\Pages;

use Quadrubo\FilamentModelSettings\Pages\Contracts\HasModelSettings;
use Quadrubo\FilamentModelSettings\Pages\ModelSettingsPage as Page;

abstract class ModelSettingsPage extends Page implements HasModelSettings
{
    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeSave(array $data): array
    {
        $settings = $this->getSettingRecord()->settings();

        // I want to merge the changes ($data) with the existing settings $settings->allFlattened(), allowing this to act as an "update" operation instead of "apply" from the package.
        return array_merge($settings->allFlattened(), $settings::dotFlatten($data));
    }
}