Skip to content
Open
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
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/ar/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'قواعد التحقق',
'helper' => 'القواعد الخاصة بحقل السمة، مثال: min:1|max:10|...',
],
'default_value' => [
'label' => 'القيمة الافتراضية',
'helper' => 'يُطبَّق كقيمة ابتدائية عند إنشاء سجل جديد بهذه السمة.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/bg/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Правила за валидация',
'helper' => 'Правила за поле на атрибут, пример: min:1|max:10|...',
],
'default_value' => [
'label' => 'Стойност по подразбиране',
'helper' => 'Прилага се като начална стойност при създаване на нов запис с този атрибут.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/de/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@
'validation_rules' => [
'label' => 'Validierungsregeln',
],
'default_value' => [
'label' => 'Standardwert',
'helper' => 'Wird als Anfangswert beim Erstellen eines neuen Eintrags mit diesem Attribut verwendet.',
],
],
];
];
4 changes: 4 additions & 0 deletions packages/admin/resources/lang/en/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Validation Rules',
'helper' => 'Rules for attribute field, example: min:1|max:10|...',
],
'default_value' => [
'label' => 'Default Value',
'helper' => 'Applied as the initial value when creating a new record with this attribute.',
],
],
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/es/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Reglas de validación',
'helper' => 'Reglas para el campo de atributo, ejemplo: min:1|max:10|...',
],
'default_value' => [
'label' => 'Valor predeterminado',
'helper' => 'Se aplica como valor inicial al crear un nuevo registro con este atributo.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/fa/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Validation Rules',
'helper' => 'Rules for attribute field, example: min:1|max:10|...',
],
'default_value' => [
'label' => 'مقدار پیش‌فرض',
'helper' => 'به عنوان مقدار اولیه هنگام ایجاد یک رکورد جدید با این ویژگی اعمال می‌شود.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/fr/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Règles de validation',
'helper' => 'Règles pour le champ attribut, exemple : min:1|max:10|...',
],
'default_value' => [
'label' => 'Valeur par défaut',
'helper' => 'Appliquée comme valeur initiale lors de la création d\'un nouvel enregistrement avec cet attribut.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/hr/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Pravila validacije',
'helper' => 'Pravila za polje atributa, primjer: min:1|max:10|...',
],
'default_value' => [
'label' => 'Zadana vrijednost',
'helper' => 'Primjenjuje se kao početna vrijednost pri stvaranju novog zapisa s ovim atributom.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/hu/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Érvényesítési szabályok',
'helper' => 'Szabályok az attribútum mezőhöz, pl.: min:1|max:10|...',
],
'default_value' => [
'label' => 'Alapértelmezett érték',
'helper' => 'Kezdeti értékként kerül alkalmazásra új rekord létrehozásakor ezzel az attribútummal.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/mn/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Баталгаажуулах дүрэм',
'helper' => 'Атрибутын талбарын дүрэм, жишээ: min:1|max:10|...',
],
'default_value' => [
'label' => 'Өгөгдмөл утга',
'helper' => 'Энэ атрибуттай шинэ бичлэг үүсгэхэд анхны утга болгон хэрэглэгдэнэ.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/nl/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Validatieregels',
'helper' => 'Regels voor attribuutveld, voorbeeld: min:1|max:10|...',
],
'default_value' => [
'label' => 'Standaardwaarde',
'helper' => 'Toegepast als beginwaarde bij het aanmaken van een nieuw record met dit attribuut.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/pl/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@
'validation_rules' => [
'label' => 'Reguły walidacji',
],
'default_value' => [
'label' => 'Wartość domyślna',
'helper' => 'Stosowana jako wartość początkowa podczas tworzenia nowego rekordu z tym atrybutem.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/pt_BR/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Regras de validação',
'helper' => 'Regras para o campo do atributo, exemplo: min:1|max:10|...',
],
'default_value' => [
'label' => 'Valor padrão',
'helper' => 'Aplicado como valor inicial ao criar um novo registro com este atributo.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/ro/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Reguli de validare',
'helper' => 'Reguli pentru câmpul atribut, de ex.: min:1|max:10|...',
],
'default_value' => [
'label' => 'Valoare implicită',
'helper' => 'Aplicată ca valoare inițială la crearea unui nou înregistrări cu acest atribut.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/tr/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Doğrulama Kuralları',
'helper' => 'Özellik alanı için kurallar, örnek: min:1|max:10|...',
],
'default_value' => [
'label' => 'Varsayılan değer',
'helper' => 'Bu özellikle yeni bir kayıt oluşturulurken başlangıç değeri olarak uygulanır.',
],
],
];
];
6 changes: 5 additions & 1 deletion packages/admin/resources/lang/vi/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
'label' => 'Quy tắc xác thực',
'helper' => 'Quy tắc cho trường thuộc tính, ví dụ: min:1|max:10|...',
],
'default_value' => [
'label' => 'Giá trị mặc định',
'helper' => 'Được áp dụng làm giá trị ban đầu khi tạo bản ghi mới với thuộc tính này.',
],
],
];
];
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ class_basename($fieldType)
->helperText(
__('lunarpanel::attribute.form.validation_rules.helper')
),
TextInput::make('default_value')->label(
__('lunarpanel::attribute.form.default_value.label')
)
->string()
->nullable()
->helperText(
__('lunarpanel::attribute.form.default_value.helper')
),
Grid::make(1)
->schema(function (Get $get) {
return AttributeData::getConfigurationFields($get('type'));
Expand Down
6 changes: 5 additions & 1 deletion packages/admin/src/Support/Forms/AttributeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public function getFilamentComponent(Attribute $attribute): Component
$value = $this->unwrapFieldValue($state);

if ($value === null) {
$value = $this->unwrapFieldValue((new $attribute->type)->getValue());
if (filled($attribute->default_value)) {
$value = $attribute->default_value;
} else {
$value = $this->unwrapFieldValue((new $attribute->type)->getValue());
}
}

if ($attribute->type === TranslatedTextFieldType::class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,63 @@
])->assertHasNoTableActionErrors();
});

it('can create an attribute with a default value', function () {
$lang = Language::factory()->create([
'default' => true,
'code' => 'en',
]);

$this->asStaff();

$attributeGroup = AttributeGroup::factory()->create();

Livewire::test(AttributesRelationManager::class, [
'ownerRecord' => $attributeGroup,
'pageClass' => EditAttributeGroup::class,
])->callTableAction(CreateAction::class, data: [
'name.'.$lang->code => 'Price',
'type' => Number::class,
'handle' => 'price',
'default_value' => '99',
'configuration' => [],
])->assertHasNoTableActionErrors();

$this->assertDatabaseHas((new Attribute)->getTable(), [
'attribute_group_id' => $attributeGroup->id,
'handle' => 'price',
'default_value' => '99',
]);
});

it('can edit an attribute default value', function () {
Language::factory()->create([
'default' => true,
'code' => 'en',
]);

$this->asStaff();

$attributeGroup = AttributeGroup::factory()->create();

$attribute = Attribute::factory()->create([
'attribute_group_id' => $attributeGroup->id,
'type' => Number::class,
'default_value' => null,
]);

Livewire::test(AttributesRelationManager::class, [
'ownerRecord' => $attributeGroup,
'pageClass' => EditAttributeGroup::class,
])->callTableAction(EditAction::class, $attribute, data: [
'default_value' => '42',
])->assertHasNoTableActionErrors();

$this->assertDatabaseHas((new Attribute)->getTable(), [
'id' => $attribute->id,
'default_value' => '42',
]);
});

it('hydrates dropdown lookups when editing attributes', function () {
Language::factory()->create([
'default' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,51 @@ function createTranslatedRichTextProductAttribute(Product $product, string $hand
->assertDontSee(__('lunarpanel::product.status.availability.hidden_from_guests'))
->assertDontSee(__('lunarpanel::product.status.availability.no_default_customer_group'));
});

it('applies attribute default_value when product has no value for that attribute', function () {
Language::factory()->create(['default' => true]);

TaxClass::factory()->create(['default' => true]);

$product = Product::factory()->create();
ProductVariant::factory()->create(['product_id' => $product->id]);

$group = AttributeGroup::factory()->create([
'attributable_type' => 'product',
'name' => ['en' => 'Details'],
'handle' => 'details',
'position' => 1,
]);

$attribute = Attribute::factory()->create([
'attribute_type' => 'product',
'attribute_group_id' => $group->id,
'position' => 1,
'name' => ['en' => 'Quantity'],
'handle' => 'quantity',
'type' => Number::class,
'default_value' => '99',
'required' => false,
'system' => false,
'searchable' => false,
]);

DB::table('lunar_attributables')->insert([
'attribute_id' => $attribute->id,
'attributable_type' => 'product_type',
'attributable_id' => $product->productType->id,
]);

// The product has no value for 'quantity' yet
expect($product->attr('quantity'))->toBeNull();

$this->asStaff(admin: true);

// Saving without explicitly setting quantity should persist the default value
Livewire::test(EditProduct::class, [
'record' => $product->getRouteKey(),
'pageClass' => 'productEdit',
])->call('save')->assertHasNoFormErrors();

expect($product->refresh()->attr('quantity'))->toBe(99);
});