London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Custom editor for PromotionData Description property

Vote:
 

Is it possible to switch the editor for PromotionData.Description to a custom one instead of "textarea"?

I have tried this to see if I can modify the metadata, but no success.


[InitializableModule]
[ModuleDependency(typeof(InitializableModule))]
public class PromotionDataMetaDataExtenderInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var registry = context.Locate.Advanced.GetInstance<MetadataHandlerRegistry>();
        registry.RegisterMetadataHandler(typeof(PromotionData), new PromotionDataMetaDataExtender());
    }

    public void Preload(string[] parameters) { }

    public void Uninitialize(InitializationEngine context) { }
}

public class PromotionDataMetaDataExtender : IMetadataExtender
{
    public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
    {
        foreach (var metadataProperty in metadata.Properties)
        {
            if (metadataProperty.PropertyName == nameof(PromotionData.Description))
            {
                metadataProperty.ShowForEdit = false; // Try and hide to see if the extender has had an effect
            }
        }
    }
}

Decompiled PromotionData class


namespace EPiServer.Commerce.Marketing
{
    // Represents a content based promotion item.
    [AvailableContentTypes(Availability.None)]
    [AdministrationSettings(CodeOnly = true, GroupName = "marketing")]
    [CLSCompliant(false)]
    public abstract class PromotionData : BasicContent
    {
        // Gets or sets the description of the promotion.
        [UIHint("textarea")]
        [Display(Order = 10, GroupName = "EPiServerCMS_SettingsPanel", Prompt = "/commerce/properties/description/prompt")]
        public virtual string Description { get; set; }
    }
}
#337850
Edited, Apr 16, 2025 9:41
Vote:
 

Found a way by implementing a custom IMetaDataProvider
Seems to work fine.


 public class CustomEpiserverMetadataProvider : IMetadataProvider
 {
     private IMetadataProvider _defaultMetadataProvider;

     public CustomEpiserverMetadataProvider (IMetadataProvider metadataProvider)
     {
         _defaultMetadataProvider = metadataProvider;
     }

     public ExtendedMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
     {
         return _defaultMetadataProvider.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
     }

     public IEnumerable GetMetadataForProperties(object container, Type containerType)
     {
         var metadataForProperties = _defaultMetadataProvider.GetMetadataForProperties(container, containerType).ToList();

         if (container is PromotionData)
         {
             foreach (var propertyMetadata in metadataForProperties)
             {
                 if (propertyMetadata.PropertyName == nameof(PromotionData.Description))
                 {
                     propertyMetadata.UIHint = "my-custom-ui-hint"; // Replace the default UIHint "textarea" with our custom editor
                 }
             }
         }

         return metadataForProperties;
     }
 }

Registering it using a InitializationModule


  [InitializableModule]
  [ModuleDependency(typeof(InitializableModule))]
  public class CustomEpiserverMetadataProviderRegistration : IInitializableModule
  {
      public void Initialize(InitializationEngine context)
      {
          var registry = context.Locate.Advanced.GetInstance();
          var metadataHandlers = registry.GetMetadataHandlers(typeof(ContentData));
          IMetadataProvider defaultMetadataProvider = (IMetadataProvider)metadataHandlers.FirstOrDefault(i => i is IMetadataProvider);

          registry.RegisterMetadataHandler(typeof(ContentData),new CustomEpiserverMetadataProvider(defaultMetadataProvider));
      }

      public void Preload(string[] parameters) { }

      public void Uninitialize(InitializationEngine context) { }

  }
#337868
Edited, Apr 17, 2025 8:02
* 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.