Hi!
How do I set name for fields or field groups generated with code?
I managed to add a new field on a website template using Litium.Accelerator.Definitions.Websites.AcceleratorWebsiteFieldDefinitionSetup
but the name of the field in backoffice is “fielddefinition.websitearea.cookieconsentpage.name”
How do i set this name? I tried with this
new FieldDefinition<WebsiteArea>(AcceleratorWebsiteFieldNameConstants.CookieConsentPage, SystemFieldTypeConstants.Pointer)
{
Option = new PointerOption { EntityType = PointerTypeConstants.WebsitesPage },
Localizations = {
["en-US"] = { Name = "Cookie Consent Page", Description = "" },
["sv-SE"] = { Name = "Sida om cookies", Description = "" }
}
}
Same problem with field group on template using Litium.Accelerator.Definitions.Websites.AcceleratorWebsiteTemplateSetup
where the name in backoffice is “fieldtemplate.websitearea.acceleratorwebsite.fieldgroup.pages.name” for this code:
new FieldTemplateFieldGroup()
{
Id = "Pages",
Collapsed = false,
Localizations =
{
["en-US"] = { Name = "Pages" },
["sv-SE"] = { Name = "Sidor" }
},
Fields =
{
AcceleratorWebsiteFieldNameConstants.CookieConsentPage
}
}
Litium version: 7.1
Litium.Accelerator.MVC/Site/Resources/Administration/Administration.resx
Add this:
<data name=“fieldtemplate.websitearea.acceleratorwebsite.fieldgroup.pages.name”>
<value>Pages</value>
</data>
Thanks, I tried to do as your link says but it still don’t work, can you see anything wrong in my code example above? Or do I need to add names both in code with Localizations and in Administration.resx?
Note that this one works first time code is run after you added field to the code if you use the accelerator.
The accelerator doesn’t update fields already created.
We change InitFields to update the field names at startup
private void InitFields(IEnumerable<FieldDefinition> fields)
{
// NOTE: Change to update field definitions from resource files.
foreach (var item in fields)
{
var currentField = _fieldDefinitionService.Get(item.AreaType, item.Id);
if (IsAlreadyExecuted<FieldDefinition>(item.Id, item.AreaType.Name))
{
if (currentField != null)
{
currentField = currentField.MakeWritableClone();
_fieldFrameworkSetupLocalizationService.Localize(currentField);
_fieldDefinitionService.Update(currentField);
SetAlreadyExecuted<FieldDefinition>(item.Id, item.AreaType.Name);
}
continue;
}
if (currentField == null)
{
_fieldFrameworkSetupLocalizationService.Localize(item);
_fieldDefinitionService.Create(item);
SetAlreadyExecuted<FieldDefinition>(item.Id, item.AreaType.Name);
}
SetAlreadyExecuted<FieldDefinition>(item.Id, item.AreaType.Name);
}
}
Ok, thanks. Yes, that’s what I assumed, I already tried deleting field in backoffice, then it was created again but with wrong name again. Will have a look at your example with InitFields.
the change is only to always apply localization. it doesnt sound like that is the issue for you when removed field is created with wrong name.
The
_fieldFrameworkSetupLocalizationService` is using the translations from the resource files to localize the strings. If the string is missing from the resource file it will put the key as the value instead. It’s the missing strings that will make the string-keys to be written out in the above example.
As @Spotonkastebo pointing out the InitFIelds
in the Litium.Accelerator.Definitions.DefinitionSetup
is the method that is executed in the startup of application and adding the fields if they not already exists. If you want the definition always to be executed (even if they are changed or removed) you should comment out/remove the IsAlreadyExecuted
checks.
I can’t get it to work, tried with modification of code from @Spotonkastebo to always add or update fields (see below) but still get name like “fielddefinition.websitearea.cookieconsentpage.name”
private void InitFields(IEnumerable<FieldDefinition> fields)
{
foreach (var item in fields)
{
var currentField = _fieldDefinitionService.Get(item.AreaType, item.Id);
if (currentField != null)
{
currentField = currentField.MakeWritableClone();
_fieldFrameworkSetupLocalizationService.Localize(currentField);
_fieldDefinitionService.Update(currentField);
}
else
{
_fieldFrameworkSetupLocalizationService.Localize(item);
_fieldDefinitionService.Create(item);
}
SetAlreadyExecuted<FieldDefinition>(item.Id, item.AreaType.Name);
}
}
I also tried with another id of page “cookiepage” instead of “cookieconsentpage” to make sure the field was not added before or cached somewhere, but my new field gets name “fielddefinition.websitearea.cookiepage.name”
new FieldDefinition<WebsiteArea>(AcceleratorWebsiteFieldNameConstants.CookiePage, SystemFieldTypeConstants.Pointer)
{
Option = new PointerOption { EntityType = PointerTypeConstants.WebsitesPage },
Localizations = {
["en-US"] = { Name = "Cookie Page", Description = "" },
["sv-SE"] = { Name = "Sida om cookies", Description = "" }
}
}
Have you added the resource string with key fielddefinition.websitearea.cookieconsentpage.name
?
If you not want the translations from the resource file you should not call the _fieldFrameworkSetupLocalizationService.Localize(currentField);
.
- You need to add the translation to the resourcefile
- Make sure the resource file have namesuffix corresponding to the culture you use.
Mytranslations.resx
Mytranslations.sv-se.resx
- Make sure you don’t have wrong buildproperties on the resourcefiles
- Double-check that you have spelled exactly correct in the resource file. I think it is important to use lowercase letter
fielddefinition.websitearea.cookieconsentpage.name
fielddefinition.websitearea.cookieconsentpage.description
Thanks,
couldn’t get it to work with Localizations and without _fieldFrameworkSetupLocalizationService.Localize(currentField);
but now it works when using resource files. To summarize:
I added this to AcceleratorWebsiteFieldDefinitionSetup.cs
new FieldDefinition<WebsiteArea>(AcceleratorWebsiteFieldNameConstants.CookiePage, SystemFieldTypeConstants.Pointer)
{
Option = new PointerOption { EntityType = PointerTypeConstants.WebsitesPage }
},
I added these to Administration.resx and Administration.sv-se.resx
fielddefinition.websitearea.cookiepage.name
fielddefinition.websitearea.cookiepage.description
with this code in DefinitionSetup.cs
private void InitFields(IEnumerable<FieldDefinition> fields)
{
foreach (var item in fields)
{
var currentField = _fieldDefinitionService.Get(item.AreaType, item.Id);
if (currentField != null)
{
currentField = currentField.MakeWritableClone();
_fieldFrameworkSetupLocalizationService.Localize(currentField);
_fieldDefinitionService.Update(currentField);
}
else
{
_fieldFrameworkSetupLocalizationService.Localize(item);
_fieldDefinitionService.Create(item);
}
SetAlreadyExecuted<FieldDefinition>(item.Id, item.AreaType.Name);
}
}