November Happy Hour will be moved to Thursday December 5th.

SetDefaultValues not working properly

Vote:
 

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.

public class EventTimeBlockType : BlockData
    {
        [Required]
        [UIHint("UTC")]
        [Display(Name = "Event End Date and Time", GroupName = SystemTabNames.Content, Order = 10)]
        public virtual DateTime EndDateAndTime { get; set; }

        [Required]
        [UIHint("UTC")]
        [Display(Name = "Event Start Date and Time", GroupName = SystemTabNames.Content, Order = 5)]
        public virtual DateTime StartDateAndTime { get; set; }

        [Display(Name = "Description", Description = "For Advanced Event Schema view only", GroupName = SystemTabNames.Content, Order = 15)]
        public virtual XhtmlString Description { get; set; }

        public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);

            this.EndDateAndTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
            this.StartDateAndTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
        }
    }

PageType:---

[Display(
        GroupName = SystemTabNames.Content,
        Order = 330)]
    [CultureSpecific]
    [AllowedTypes(new[] { typeof(EventTimeBlockType) })]
    public virtual ContentArea RelatedContentArea { get; set; }
#303561
Jun 15, 2023 11:21
Vote:
 

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);
                }               
            }
        }
    }
#303738
Jun 19, 2023 5:39
Manoj Kumawat - Jul 11, 2023 13:52
I believe this event will hit after the dialog has rendered.
Vote:
 

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. 

#304993
Jul 11, 2023 13:50
Vote:
 

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 ...

#306555
Aug 10, 2023 7:30
Manoj Kumawat - Aug 10, 2023 7:40
Not that I know of.
Vote:
 

I will forward this question to our CMS UI team. thanks for bringing this to our attention 

#306561
Aug 10, 2023 10:19
Vote:
 

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); }
        }

#306885
Aug 16, 2023 13:24
Vote:
 

I think this might be related to bug CMS-29306 CMS-26749  and will be fixed in the next version of CMS UI 

#306886
Edited, Aug 16, 2023 13:32
Vote:
 

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.

#306977
Edited, Aug 17, 2023 15:23
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.