Skip to content

Commit

Permalink
move field group component convertion to model
Browse files Browse the repository at this point in the history
  • Loading branch information
cklei-carly committed Aug 16, 2024
1 parent 25bfce8 commit efc6902
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
31 changes: 1 addition & 30 deletions src/FilamentFieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Arr;
use ReflectionClass;
use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Contracts\FieldTypeConfig;
use SolutionForest\FilamentFieldGroup\Models\FieldGroup;
use SolutionForest\FilamentFieldGroup\Supports\FieldGroupConfig;

class FilamentFieldGroup
Expand Down Expand Up @@ -91,35 +90,7 @@ public function findFieldGroup($name): ?Forms\Components\Component
return null;
}

$schema = [];

foreach ($fieldGroup->fields as $field) {

$fiFormConfig = $this->getFieldTypeConfig($field->type, $field->config);

if (! $fiFormConfig) {
continue;
}

$fiFormComponentFQCN = Arr::first(Arr::pluck($fiFormConfig->getFormComponents(), 'component'));
if (! $fiFormComponentFQCN) {
throw new \Exception("The field type config class {$fiFormConfig} does not have a FormComponent attribute.");
}

$fiFormComponent = $fiFormComponentFQCN::make($field->name);

// @todo - some components may not have these methods
$fiFormComponent->label($field->label);
$fiFormComponent->helperText($field->instructions);
$fiFormComponent->required($field->mandatory);

$fiFormConfig->applyConfig($fiFormComponent);

$schema[] = $fiFormComponent;
}

return Forms\Components\Section::make($fieldGroup->title)
->schema($schema);
return $fieldGroup->toFilamentComponent();
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Models/FieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use SolutionForest\FilamentFieldGroup\Facades\FilamentFieldGroup;
use SolutionForest\FilamentFieldGroup\Supports\FieldGroupConfig;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
Expand Down Expand Up @@ -35,6 +37,42 @@ public function fields(): HasMany
return $this->hasMany(FieldGroupConfig::getFieldModelClass(), 'group_id')->orderBy('sort');
}

/**
* @return \Filament\Forms\Components\Component
*/
public function toFilamentComponent()
{
$schema = [];

foreach ($this->fields as $field) {

$fiFormConfig = FilamentFieldGroup::getFieldTypeConfig($field->type, $field->config);

if (! $fiFormConfig) {
continue;
}

$fiFormComponentFQCN = Arr::first(Arr::pluck($fiFormConfig->getFormComponents(), 'component'));
if (! $fiFormComponentFQCN) {
throw new \Exception("The field type config class {$fiFormConfig} does not have a FormComponent attribute.");
}

$fiFormComponent = $fiFormComponentFQCN::make($field->name);

// @todo - some components may not have these methods
$fiFormComponent->label($field->label);
$fiFormComponent->helperText($field->instructions);
$fiFormComponent->required($field->mandatory);

$fiFormConfig->applyConfig($fiFormComponent);

$schema[] = $fiFormComponent;
}

return \Filament\Forms\Components\Section::make($this->title)
->schema($schema);
}

public static function boot()
{
parent::boot();
Expand Down

0 comments on commit efc6902

Please sign in to comment.