Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Concerns/CanMapDynamicFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backstage\Fields\Concerns;

use Backstage\Fields\Contracts\FieldInspector;
use Backstage\Fields\Enums\Field;
use Backstage\Fields\Fields;
use Backstage\Fields\Fields\Checkbox;
use Backstage\Fields\Fields\CheckboxList;
Expand Down
2 changes: 1 addition & 1 deletion src/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Fields
{
private static array $fields = [];
protected static array $fields = [];

public static function registerField(string $className): void
{
Expand Down
16 changes: 0 additions & 16 deletions src/Fields/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Backstage\Fields\Contracts\FieldContract;
use Backstage\Fields\Enums\Field as FieldEnum;
use Backstage\Fields\Facades\Fields;
use Backstage\Fields\Fields\Select as FieldsSelect;
use Backstage\Fields\Models\Field;
use Filament\Forms;
use Filament\Forms\Components\Hidden;
Expand All @@ -28,21 +27,6 @@ class Repeater extends Base implements FieldContract
use HasFieldTypeResolver;
use HasOptions;

private const FIELD_TYPE_MAP = [
'text' => Text::class,
'textarea' => Textarea::class,
'rich-editor' => RichEditor::class,
'repeater' => Repeater::class,
'select' => FieldsSelect::class,
'checkbox' => Checkbox::class,
'checkbox-list' => CheckboxList::class,
'key-value' => KeyValue::class,
'radio' => Radio::class,
'toggle' => Toggle::class,
'color' => Color::class,
'date-time' => DateTime::class,
];

public static function getDefaultConfig(): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/RichEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Backstage\Fields\Fields;

use Backstage\Enums\ToolbarButton;
use Backstage\Fields\Enums\ToolbarButton;
use Backstage\Fields\Contracts\FieldContract;
use Backstage\Fields\Models\Field;
use Filament\Forms;
Expand Down
59 changes: 14 additions & 45 deletions src/Filament/RelationManagers/FieldsRelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function form(Form $form): Form
return;
}

$set('config', $this->initializeConfig($state));
$set('config', $this->getFieldTypeFormSchema($state));
}),

Select::make('group')
Expand Down Expand Up @@ -118,43 +118,6 @@ public function form(Form $form): Form
]);
}

private function formatCustomFields(array $fields): array
{
return collect($fields)->mapWithKeys(function ($field, $key) {
$parts = explode('\\', $field);
$lastPart = end($parts);
$formattedName = Str::title(Str::snake($lastPart, ' '));

return [$key => $formattedName];
})->toArray();
}

private function initializeDefaultConfig(string $fieldType): array
{
$className = 'Backstage\\Fields\\Fields\\' . Str::studly($fieldType);

if (! class_exists($className)) {
return [];
}

$fieldInstance = app($className);

return $fieldInstance::getDefaultConfig();
}

private function initializeCustomConfig(string $fieldType): array
{
$className = Fields::getFields()[$fieldType] ?? null;

if (! class_exists($className)) {
return [];
}

$fieldInstance = app($className);

return $fieldInstance::getDefaultConfig();
}

public function table(Table $table): Table
{
return $table
Expand Down Expand Up @@ -184,14 +147,16 @@ public function table(Table $table): Table
Tables\Actions\CreateAction::make()
->slideOver()
->mutateFormDataUsing(function (array $data) {

$key = $this->ownerRecord->getKeyName() ?? 'id';
$key = $this->ownerRecord->getKeyName();
if ($key === '') {
$key = 'id';
}

return [
...$data,
'position' => Field::where('model_key', $key)->get()->max('position') + 1,
'model_type' => 'setting',
'model_key' => $this->ownerRecord->slug,
'model_key' => $this->ownerRecord->slug ?? $key,
];
})
->after(function (Component $livewire) {
Expand All @@ -202,8 +167,10 @@ public function table(Table $table): Table
Tables\Actions\EditAction::make()
->slideOver()
->mutateRecordDataUsing(function (array $data) {

$key = $this->ownerRecord->getKeyName() ?? 'id';
$key = $this->ownerRecord->getKeyName();
if ($key === '') {
$key = 'id';
}

return [
...$data,
Expand All @@ -221,8 +188,10 @@ public function table(Table $table): Table
->getSchemaBuilder()
->hasColumn($this->ownerRecord->getTable(), $record->valueColumn)
) {

$key = $this->ownerRecord->getKeyName() ?? 'id';
$key = $this->ownerRecord->getKeyName();
if ($key === '') {
$key = 'id';
}

$this->ownerRecord->update([
$record->valueColumn => collect($this->ownerRecord->{$record->valueColumn})->forget($record->{$key})->toArray(),
Expand Down
13 changes: 13 additions & 0 deletions src/Models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
use Illuminate\Support\Facades\Config;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

/**
* @property string $ulid
* @property string|null $parent_ulid
* @property string $model_type
* @property string $model_key
* @property string $slug
* @property string $name
* @property string $field_type
* @property array|null $config
* @property int $position
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class Field extends Model
{
use HasPackageFactory;
Expand Down
20 changes: 17 additions & 3 deletions src/Services/FieldInspectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function getMethodsDetails(ReflectionClass $reflection): array
'visibility' => $this->getVisibility($method),
'static' => $method->isStatic(),
'parameters' => $this->getParametersDetails($method),
'returnType' => $method->getReturnType() ? $method->getReturnType()->getName() : null,
'returnType' => $method->getReturnType() ? $this->getTypeName($method->getReturnType()) : null,
'docComment' => $method->getDocComment() ?: null,
];
}
Expand All @@ -78,7 +78,7 @@ private function getPropertiesDetails(ReflectionClass $reflection): array
$properties[$property->getName()] = [
'visibility' => $this->getVisibility($property),
'static' => $property->isStatic(),
'type' => $property->getType() ? $property->getType()->getName() : null,
'type' => $property->getType() ? $this->getTypeName($property->getType()) : null,
'docComment' => $property->getDocComment() ?: null,
'defaultValue' => $this->getPropertyDefaultValue($property),
];
Expand All @@ -92,7 +92,7 @@ private function getParametersDetails(ReflectionMethod $method): array
$parameters = [];
foreach ($method->getParameters() as $param) {
$parameters[$param->getName()] = [
'type' => $param->getType() ? $param->getType()->getName() : null,
'type' => $param->getType() ? $this->getTypeName($param->getType()) : null,
'hasDefaultValue' => $param->isDefaultValueAvailable(),
'defaultValue' => $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null,
'isVariadic' => $param->isVariadic(),
Expand Down Expand Up @@ -123,4 +123,18 @@ private function getPropertyDefaultValue(ReflectionProperty $property): mixed
return null;
}
}

private function getTypeName(\ReflectionType $type): string
{
if ($type instanceof \ReflectionNamedType) {
return $type->getName();
}
if ($type instanceof \ReflectionUnionType) {
return implode('|', array_map(fn($t) => $this->getTypeName($t), $type->getTypes()));
}
if ($type instanceof \ReflectionIntersectionType) {
return implode('&', array_map(fn($t) => $this->getTypeName($t), $type->getTypes()));
}
return (string) $type;
}
}
Loading