How do I set a default value in a UI Forms field using a macro on create?
I have got a UI edit form for a custom module class with a UserID as one of the fields. This field uses a selector using a dropdown with the users in the database.
What I want is setting the default value to the current UserID, and preferably not showing the selector at all, only for administrators.
I cannot figure out how to do this. Tried a bit with Macro's but that didn't work. Of course I browsed through the docs, but couldn't find any help there.
Environment
- Xperience by Kentico version: [30.1.1]
- .NET version: [9]
- Execution environment: [Private cloud (Azure)]
- Link to relevant Xperience by Kentico documentation
Answers
In your "Create" class for your custom module you can override the SetFormData(infoObject, IFormFieldValueProvider)
method.
Something like so:
protected override Task SetFormData(ProductModelInfo infoObject, IFormFieldValueProvider fieldValueProvider)
{
// default the order to be last in the list
var productType = ProductModelInfo.Provider.Get()
.Column(nameof(ProductModelInfo.ProductModelOrder))
.OrderByDescending(nameof(ProductModelInfo.ProductModelOrder))
.FirstOrDefault();
// set the order of the new item to order + 1
if (productType != null) infoObject.ProductModelOrder = productType.ProductModelOrder + 1;
return base.SetFormData(infoObject, fieldValueProvider);
}
To answer this question, you have to login first.