Skip to content

Commit

Permalink
Merge pull request #83 from bcgov/Add-data-bindings-to-Groups-ADO-2322
Browse files Browse the repository at this point in the history
Add (customizable) data bindings to Groups
  • Loading branch information
saranyaviswam authored Jan 20, 2025
2 parents b2d1ba0 + 4c3353d commit 5312157
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/Filament/Forms/Resources/FieldGroupResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Filament\Forms\Resources\FieldGroupResource\Pages;
use App\Models\FieldGroup;
use App\Models\FormDataSource;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
Expand All @@ -29,6 +30,10 @@ public static function form(Form $form): Form
->unique(ignoreRecord: true)
->required(),
TextInput::make('label'),
Select::make('data_binding_path')
->label('Field data source')
->options(FormDataSource::pluck('name', 'name')),
Textarea::make('data_binding'),
Textarea::make('description')
->columnSpanFull(),
Textarea::make('internal_description')
Expand Down
30 changes: 29 additions & 1 deletion app/Filament/Forms/Resources/FormVersionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ public static function form(Form $form): Form
}
return $options;
})

->searchable()
->required()
->reactive()
Expand Down Expand Up @@ -351,6 +350,35 @@ public static function form(Form $form): Form
->distinct(),
Toggle::make('repeater')
->label('Repeater'),
Fieldset::make('Data Binding')
->columns(1)
->schema([
Placeholder::make('data_binding')
->label("Default")
->content(fn($get) => FieldGroup::find($get('field_group_id'))->data_binding ?? 'null'),
Checkbox::make('customize_data_binding')
->label('Customize Data Binding')
->inline()
->live(),
TextInput::make('custom_data_binding')
->label(false)
->visible(fn($get) => $get('customize_data_binding')),
]),
Fieldset::make('Data Source')
->columns(1)
->schema([
Placeholder::make('data_binding_path')
->label("Default")
->content(fn($get) => FieldGroup::find($get('field_group_id'))->data_binding_path ?? 'null'),
Checkbox::make('customize_data_binding_path')
->label('Customize Data Source')
->inline()
->live(),
Select::make('custom_data_binding_path')
->label(false)
->options(FormDataSource::pluck('name', 'name'))
->visible(fn($get) => $get('customize_data_binding_path')),
]),
Repeater::make('form_fields')
->label('Form Fields in Group')
->reorderable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ protected function afterCreate(): void
'order' => $order,
'label' => $component['group_label'] ?? null,
'repeater' => $component['repeater'] ?? false,
'data_binding_path' => $component['data_binding_path'] ?? null,
'custom_data_binding_path' => $component['custom_data_binding_path'] ?? null,
'data_binding' => $component['data_binding'] ?? null,
'custom_data_binding' => $component['custom_data_binding'] ?? null,
'instance_id' => $component['instance_id'] ?? null,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Forms\Resources\FormVersionResource\Pages;

use App\Filament\Forms\Resources\FormVersionResource;
use App\Models\FieldGroup;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -92,6 +93,8 @@ protected function afterSave(): void
'order' => $order,
'label' => $component['group_label'] ?? null,
'repeater' => $component['repeater'] ?? false,
'custom_data_binding_path' => $component['customize_data_binding_path'] ? $component['custom_data_binding_path'] : null,
'custom_data_binding' => $component['customize_data_binding'] ? $component['custom_data_binding'] : null,
'instance_id' => $component['instance_id'] ?? null,
]);

Expand Down Expand Up @@ -228,11 +231,16 @@ protected function mutateFormDataBeforeFill(array $data): array
];
}

$fieldGroup = FieldGroup::find($group['field_group_id']) ?? 'null';
$components[] = [
'component_type' => 'field_group',
'field_group_id' => $group->field_group_id,
'group_label' => $group->label,
'repeater' => $group->repeater,
'custom_data_binding_path' => $group->custom_data_binding_path ?? $fieldGroup->data_binding_path,
'customize_data_binding_path' => $group->custom_data_binding_path ?? null,
'custom_data_binding' => $group->custom_data_binding ?? $fieldGroup->data_binding,
'customize_data_binding' => $group->custom_data_binding ?? null,
'form_fields' => $formFieldsData,
'order' => $group->order,
'instance_id' => $group->instance_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Forms\Resources\FormVersionResource\Pages;

use App\Filament\Forms\Resources\FormVersionResource;
use App\Models\FieldGroup;
use App\Models\FormField;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
Expand Down Expand Up @@ -120,11 +121,16 @@ protected function mutateFormDataBeforeFill(array $data): array
];
}

$fieldGroup = FieldGroup::find($group['field_group_id']) ?? 'null';
$components[] = [
'component_type' => 'field_group',
'field_group_id' => $group->field_group_id,
'group_label' => $group->label,
'repeater' => $group->repeater,
'custom_data_binding_path' => $group->custom_data_binding_path ?? $fieldGroup->data_binding_path,
'customize_data_binding_path' => $group->custom_data_binding_path ?? null,
'custom_data_binding' => $group->custom_data_binding ?? $fieldGroup->data_binding,
'customize_data_binding' => $group->custom_data_binding ?? null,
'form_fields' => $formFieldsData,
'order' => $group->order,
'instance_id' => $group->instance_id,
Expand Down
10 changes: 10 additions & 0 deletions app/Helpers/FormTemplateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ protected static function formatField($fieldInstance, $index)
} elseif ($fieldInstance->customize_label == 'hide') {
$label = null;
}

$base = [
"type" => $field->dataType->name,
"id" => $fieldInstance->custom_instance_id ?? $fieldInstance->instance_id,
Expand Down Expand Up @@ -186,6 +187,11 @@ protected static function formatGroup($groupInstance, $index)
return self::formatField($fieldInstance, $fieldIndex + 1);
})->values()->all();

$databindings = [
"source" => $groupInstance->custom_data_binding_path ?? $group->data_binding_path,
"path" => $groupInstance->custom_data_binding ?? $group->data_binding,
];

$base = [
"type" => "group",
"label" => $groupInstance->label ?? $group->label,
Expand All @@ -197,6 +203,10 @@ protected static function formatGroup($groupInstance, $index)
],
];

if (!is_null($databindings["source"]) && !is_null($databindings["path"])) {
$base = array_merge($base, ["databindings" => $databindings]);
}

return array_merge($base, [
"groupItems" => [
[
Expand Down
2 changes: 2 additions & 0 deletions app/Models/FieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class FieldGroup extends Model
'label',
'description',
'internal_description',
'data_binding_path',
'data_binding',
'repeater',
];

Expand Down
2 changes: 2 additions & 0 deletions app/Models/FieldGroupInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class FieldGroupInstance extends Model
'field_group_id',
'label',
'repeater',
'custom_data_binding_path',
'custom_data_binding',
'order',
'instance_id',

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('field_groups', function (Blueprint $table) {
$table->text('data_binding')->nullable();
$table->string('data_binding_path')->nullable();
});

Schema::table('field_group_instances', function (Blueprint $table) {
$table->text('custom_data_binding')->nullable();
$table->string('custom_data_binding_path')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('field_groups', function (Blueprint $table) {
$table->dropColumn('data_binding');
$table->dropColumn('data_binding_path');
});

Schema::table('field_group_instances', function (Blueprint $table) {
$table->dropColumn('custom_data_binding');
$table->dropColumn('custom_data_binding_path');
});
}
};

0 comments on commit 5312157

Please sign in to comment.