Hi Pijush,
You can hook into one of the content events to set the default values regardless of how the block was created.
The below code snippet illustrates how to do this (in the below example the default values will be set after the block is created)
[InitializableModule]
[ModuleDependency(typeof(InitializableModule))]
public class ContentEventsInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
contentEvents.SavingContent += ContentEvents_SavingContent;
}
public void Uninitialize(InitializationEngine context)
{
var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
contentEvents.SavingContent -= ContentEvents_SavingContent ;
}
private void ContentEvents_SavingContent(object sender, ContentEventArgs e)
{
var eventBlock = e.Content as EventTimeBlockType;
if(eventBlock != null)
{
//Set default values
if (eventBlock.StartDateAndTime == DateTime.MinValue)
{
eventBlock.StartDateAndTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
}
}
}
}
The value is actually set if you debug but I feel this is a bug with optimizely.
Here is how I identified and fixed such problem. When you add block from asset pane then it hides the properties which are setDefaultValue (correct)
But when the block is being created in ContentArea then it has full view display but without showing default value.
Though I still hate this solution but since the property is set as default value therefore it doesn't need to be displayed as it dont do it for asset pane.
You can hide it having a MetadataExtender (In my case the property was both required and setToDefaultValue) -
public class ContentCreateMetadataExtender : IMetadataExtender
{
public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
{
if (metadata.Model is not IContent data || data.ContentLink.ID != 0)
{
return;
}
foreach (var modelMetadata in metadata.Properties)
{
var property = modelMetadata;
if (!property.Attributes.OfType<RequiredAttribute>().Any())
{
continue;
}
//There is no default support for setting a property value when
//adding through contentArea. Therefor,if a property is required
//and initial value is set then disable show for edit as identical in asset pane.
if (property.InitialValue.IsNotNull())
{
property.ShowForEdit = false;
}
}
}
}
Register MetaDataRegistry in SiteInitializationModule -
public void Initialize(InitializationEngine context)
{
_locator = context.Locate.Advanced;
var registry = _locator.GetInstance<MetadataHandlerRegistry>();
registry.RegisterMetadataHandler(typeof(ContentData), new ContentCreateMetadataExtender());
}
Verify that the property is hidden with default set.
Has this been addressed in some capacity?
SetDefaultValues() still not working with version 12.22.3 ... I see the workaround, but I'd rather have that bug fixed ...
I will forward this question to our CMS UI team. thanks for bringing this to our attention
Hello,
Othere option to set the default value as below.
[CultureSpecific]
[Display(Name = "Page Title", GroupName = SystemTabNames.Content, Order = 10)]
public virtual string PageTitle {
get
{
var inputValue = this.GetPropertyValue(p => p.PageTitle);
return !string.IsNullOrWhiteSpace(inputValue)
? inputValue
: "Purchase Card Details";
}
set { this.SetPropertyValue(p => p.PageTitle, value); }
}
I think this might be related to bug CMS-29306 CMS-26749 and will be fixed in the next version of CMS UI
I can confirm that this is fixed in the bug https://world.optimizely.com/support/bug-list/bug/CMS-29306 which will be in our next release.
When we create a block from asset pane It that time set default value working fine. But when we create the same block from content area --> create a new block at that time set default values are not working.
PageType:---