how to pass a variable to importer and use it inside getColumns

Hi, I would like to pass a variable to an importer and use it inside the importer's getColumns function
11 Replies
gladjanus43
gladjanus436mo ago
there is the public static function getOptionsFormComponents(): array { return [ Select::make('dateFormat') ->options([ 'd/m/Y H:i' => 'dd/mm/yyyy min:sec', 'd-m-Y H:i' => 'dd-mm-yyyy min:sec', 'Y/m/d H:i' => 'yyyy/mm/dd min:sec', ]) ->required() ->default('d-m-Y H:i') ->label('Specificeer het datumformaat dat in je CSV wordt gebruikt.'), ]; } I have only used it in the resolveRecord function using $this->options so I cant say if it is also available in the getColumns function! This function generates a form field on the mapping dialog so the user can specifiy the variable
marco76tv
marco76tvOP6mo ago
->options(['foo'=>'pluto']) I also used it inside resolveRecord.. but I need it inside getColumns
Dan Harrin
Dan Harrin6mo ago
can you please explain the use case of what you're trying to achieve?
marco76tv
marco76tvOP6mo ago
I created the ManageContacts subpage and added the action ImportAction::make() ->importer(ContactImporter::class) ->options([ 'survey_id'=>$this->record->getKey(), ]) each survey has translations of the field labels, and I have to put getColumns inside $survey_id=Str::of(url()->previous()) ->between('/surveys/','/contacts') ->toString(); $survey=Survey::firstWhere(['id'=>$surveyid]);
$trans = $survey?->getTrans() ?? []; -------------------------------------------------------- $k='attribute
'.$i; $columns[]=ImportColumn::make($k) ->label($trans[$k] ?? $k) ->ignoreBlankState(); I think there is a better way to go and get "records" than $survey_id=Str::of(url()->previous()) ->between('/surveys/','/contacts') ->toString();
Dan Harrin
Dan Harrin6mo ago
unfortunately imports werent really made to be dynamic like this, so you're best off defining your own action that looks identical to filament's and processing them in your own job
marco76tv
marco76tvOP6mo ago
maybe a small change to importAction to pass by ImportAction::make() ->importer(ContactImporter::class) TO ImportAction::make() ->importer(ContactImporter::make(['var1'=>'value1']))
Dan Harrin
Dan Harrin6mo ago
its not that simple unfortunately there are many places where import methods need to be called from and anything you pass in would then need to be serialised into jobs etc this use case just isnt what the system was designed for, it was designed for the 99% use case where your columns correspond to columns in the database
marco76tv
marco76tvOP6mo ago
yes and I fell into that 1%
Dan Harrin
Dan Harrin6mo ago
yeah, that is unfortunate, but your use case is very specific here and is achievable with your own action
marco76tv
marco76tvOP6mo ago
thank you very much, in the meantime I use that workaround that takes the data from the url
Dan Harrin
Dan Harrin6mo ago
you can copy parts of our action and customize them :)
Want results from more Discord servers?
Add your server