Field definitions
Field definitions are used to set up fields.
Field definitions are specific to the different Areas in Litium. For example, there can be fields called Name in several platform areas. One for Products and another for Websites.
A number of settings can be made for all fields:
- Translatable
- Column in lists
- Filter in list views
- Read only
- Visible in Storefront
- Searchable
How to use the field definition in code:
using System;
using Litium.FieldFramework;
using Litium.Products;
public class FieldDefinitionExample
{
private readonly FieldDefinitionService _fieldDefinitionService;
private readonly FieldTemplateService _fieldTemplateService;
private readonly BaseProductService _baseProductService;
public FieldDefinitionExample(FieldDefinitionService fieldDefinitionService, BaseProductService baseProductService, FieldTemplateService fieldTemplateService)
{
_fieldDefinitionService = fieldDefinitionService;
_baseProductService = baseProductService;
_fieldTemplateService = fieldTemplateService;
}
public void CreateFieldDefinition()
{
var fieldDefinition = new FieldDefinition<ProductArea>("MyField", "text")
{
Localizations =
{
["sv-SE"] = {Name = "Mitt fält", Description = "Mitt fält för att spara information"},
["en-UK"] = {Name = "My field", Description = "My field to save information"},
},
CanBeGridColumn = false,
CanBeGridFilter = true,
};
_fieldDefinitionService.Create(fieldDefinition);
}
public void CreateField()
{
var fieldTemplate = _fieldTemplateService.Get<ProductFieldTemplate>("MyFieldTemplate");
var baseproduct = new BaseProduct("My_baseproduct", fieldTemplate.SystemId);
baseproduct.Fields.AddOrUpdateValue("MyField", "value");
_baseProductService.Create(baseproduct);
var myFieldValue = baseproduct.Fields.GetValue<string>("MyField");
}
public void UpdateField()
{
var baseproduct = _baseProductService.Get("My_baseproduct");
var changableBaseProduct = baseproduct.MakeWritableClone();
changableBaseProduct.Fields.AddOrUpdateValue("MyField", "value2");
_baseProductService.Update(changableBaseProduct);
}
}