Hi Dariusz
#1
Is property X the one decorated with SelectMany attribute? If yes, you can manually update the value in this property. It just holds comma delimited string values. To make it clear, I'll use the following code snippet to explain further more
I have 3 languages, and if I select first two languages, the value stored in MultipleLanguage will be "EN,SW". In your publish event hook, you can override this value to "PF", and check your CMS, the UI should be updated.
#2 I don't have much background of your business. As far as the value of category is not changed, everything will be working fine. When categories get deleted, that's where the tricky bits happened. Let's continue with the sample below.
I have the first two languages selected, and add each language triggers adding a block in a content area. Now first language gets deleted (simulate to your category delete scenario), the UI in CMS won't break but English option will disappear, and value stored in MultipleLanguage remains the same. How would you remove the block added to the content area? So technically you'll need to maintain the state.
I hope above helps
[SelectMany(SelectionFactoryType = typeof(LanguageSelectionFactory))]
public virtual string MultipleLanguage { get; set; }
public class LanguageSelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
return new ISelectItem[] { new SelectItem() { Text = "English", Value = "EN" }, new SelectItem() { Text = "Swahili", Value = "SW" }, new SelectItem() { Text = "French Polonesia", Value = "PF" }
}; } }
Hi Dariusz!
If I understand correcty you want to set the value of a property when the content is loaded "manually check/uncheck checkboxes on property X loading in CMS UI:"? If thats the case this pseduo code may help (the example I use simply adds a "A" to the page name on any page but you can change the checkbox values here):
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
namespace YourProjectNamespace
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ChangePropertyOnLoadInit : IInitializableModule
{
private IContentLoader _contentLoader;
private IHttpContextAccessor _httpContextAccessor;
public void Initialize(InitializationEngine context)
{
_contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();
_httpContextAccessor = context.Locate.Advanced.GetInstance<IHttpContextAccessor>();
var events = context.Locate.Advanced.GetInstance<IContentEvents>();
events.LoadedContent += Events_LoadedContent;
}
private void Events_LoadedContent(object sender, ContentEventArgs e)
{
// Only apply for the page type needed
if (e.Content is not PageData pageData)
{
return;
}
// Prevent the more than one execution within the same request
if (_httpContextAccessor.HttpContext?.Items["Skip" + e.ContentLink.ID] != null)
{
return;
}
_httpContextAccessor.HttpContext?.Items.Add("Skip" + e.ContentLink.ID, true);
var updateContent = pageData.CreateWritableClone();
updateContent.PageName += "A";
e.Content = updateContent;
}
public void Uninitialize(InitializationEngine context)
{
var events = context.Locate.Advanced.GetInstance<IContentEvents>();
events.LoadedContent -= Events_LoadedContent;
}
}
}
Bear in mind this code runs every time the content is loaded so will called when the page is displayed on the front end. So keep performance in mind with any code you write!
Let me know how you get on.
Thanks for the answers.
Finally, the solution is similar to the answers, and looks like:
Given the CMS page, there is a need to have a checkbox property (let's call it X) whose state is detached from the original object.
Just to make it more clear, here is an example:
The solution I think would be:
All the above are clear to me and I have an implementation of that. However, I'm not quite sure how to manually check/uncheck checkboxes on property X loading in CMS UI:
Using CMS 12.17.x
Thank you :)