Dependency Injection on Custom Fields

Hello, I have a custom field that I've created which has a few getter/setter functions - one of which is called mergeTags(). When calling this function from my form ie: CustomFieldName::make('custom_field) ->mergeTags(....) I would like to dependancy inject the record I am currently editing, the same way you can do with default etc: ->default(function (Model $record) { return $record->....; }), Any ideas how this can be accomplished? Currently I get 'Too few arguments to function...' error Thanks!
2 Replies
Lara Zeus
Lara Zeus4mo ago
just make your setters and getters accept Closure
protected Closure | string $mergeTags = '';
protected Closure | string $mergeTags = '';
and evaluate when getting it
public function getMergeTags(): Closure | string
{
return $this->evaluate($this->mergeTags);
}
public function getMergeTags(): Closure | string
{
return $this->evaluate($this->mergeTags);
}
and remember to add the trait to your component
use \Filament\Support\Concerns\EvaluatesClosures;
use \Filament\Support\Concerns\EvaluatesClosures;
jackcbet
jackcbet4mo ago
Wow... That's awesome. Thanks Lara
Want results from more Discord servers?
Add your server