Feature: Adding Custom Fields to Products
This commit is contained in:
@@ -2,9 +2,31 @@
|
||||
|
||||
namespace Modules\Core\Review\Filament\Extensions;
|
||||
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
use Lunar\Admin\Support\Extending\ResourceExtension;
|
||||
use Modules\Core\Review\Filament\Pages\ManageProductReviews;
|
||||
|
||||
/**
|
||||
* CorePlugin allows exactly one extension class per Lunar resource — this
|
||||
* one already owned ProductResource (adding the Reviews sub-page) before
|
||||
* Catalog needed its own product-form addition, so extendForm() lives
|
||||
* here too rather than competing for the same resource slot.
|
||||
*
|
||||
* extendForm() adds a "Custom Fields" repeater authoring Product::
|
||||
* $custom_fields (see the migration adding that column, and Modules\Core\
|
||||
* Catalog\Models\Product's own docblock on why this needed a first-party
|
||||
* Product subclass rather than being addable to the base Lunar model) —
|
||||
* per-product, customer-authored input (a reference photo upload, an
|
||||
* optional engraving textarea) rendered on the storefront product page,
|
||||
* NOT a Lunar ProductOption: an option's values are a fixed, admin-
|
||||
* authored list that define variants, which doesn't fit "the customer
|
||||
* uploads their own unique photo."
|
||||
*/
|
||||
class ProductResourceExtension extends ResourceExtension
|
||||
{
|
||||
public function extendPages(array $pages): array
|
||||
@@ -18,4 +40,56 @@ class ProductResourceExtension extends ResourceExtension
|
||||
{
|
||||
return [...$pages, ManageProductReviews::class];
|
||||
}
|
||||
|
||||
public function extendForm(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
...$schema->getComponents(),
|
||||
$this->customFieldsSection(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function customFieldsSection(): Section
|
||||
{
|
||||
return Section::make('Custom Fields')
|
||||
->description('Extra input the shopper fills in on this product\'s page before adding it to their cart — a reference photo, personalization text, etc.')
|
||||
->collapsible()
|
||||
->collapsed(fn ($record) => blank($record?->custom_fields))
|
||||
->schema([
|
||||
Repeater::make('custom_fields')
|
||||
->hiddenLabel()
|
||||
->schema([
|
||||
TextInput::make('label')
|
||||
->label('Label')
|
||||
->helperText('Shown to the shopper above the field.')
|
||||
->required(),
|
||||
Select::make('type')
|
||||
->label('Field type')
|
||||
->options([
|
||||
'text' => 'Short text',
|
||||
'textarea' => 'Long text',
|
||||
'file' => 'File upload',
|
||||
])
|
||||
->default('text')
|
||||
->native(false)
|
||||
->live()
|
||||
->required(),
|
||||
TextInput::make('key')
|
||||
->label('Key')
|
||||
->helperText('Machine-facing identifier — stored on the order/cart line, used to look up this answer elsewhere. Cannot be changed once orders reference it.')
|
||||
->required()
|
||||
->alphaDash()
|
||||
->maxLength(64),
|
||||
Toggle::make('required')
|
||||
->label('Required')
|
||||
->helperText('Shopper cannot add this product to their cart without answering.')
|
||||
->default(false),
|
||||
])
|
||||
->columns(2)
|
||||
->addActionLabel('Add a custom field')
|
||||
->reorderable()
|
||||
->collapsible()
|
||||
->itemLabel(fn (array $state): ?string => $state['label'] ?? null),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user