All model properties annotated with Required attribute will be displayed on the Content Create view. Sometimes we would like show some additional properties when creating new content. For example we have a boolean property “Show article on Start Page”. It’s not mandatory to mark it, but it’s could be a useful feature for editor if he could select it when adding new article. I tried to extend EPiServer functionality and add this feature.
Extending metadata information
First I introduced DisplayOnContentCreateAttribute annotation attribute. If model property is marked with this attribute it should appear on the view.
1 2 3 4 |
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class DisplayOnContentCreateAttribute : Attribute { } |
The atribute will be used by custom MetadataExtender. Inside ModifyMetadata method the extender will check if property has DisplayOnContentCreate attribute. Based on this information it will set displayOnContentCreate client property.
1 2 3 4 5 6 7 8 9 10 11 |
public class ContentCreateMetadataExtender : IMetadataExtender { public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes) { foreach (ExtendedMetadata property in metadata.Properties) { property.EditorConfiguration["displayOnContentCreate"] = property.Attributes.OfType<DisplayOnContentCreateAttribute>().Any(); } } } |
And finally I had to register our MetadataExtender in the InitializableModule.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[InitializableModule] [ModuleDependency(typeof (EPiServer.Cms.Shell.InitializableModule))] public class SiteMetadataExtenderInitialization : IInitializableModule { public void Initialize(InitializationEngine context) { if (context.HostType == HostType.WebApplication) { var registry = context.Locate.Advanced.GetInstance<MetadataHandlerRegistry>(); registry.RegisterMetadataHandler(typeof (ContentData), new ContentCreateMetadataExtender()); } } public void Preload(string[] parameters) { } public void Uninitialize(InitializationEngine context) { } } |
Extending Create View
To use new attribute on the client side I replaced CreateContentViewModel EPiServer script file with custom implementation. I did it using modules.config file …
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <module> <clientResources> <add name="epi-cms.widgets.base" path="~/modules/createContent/CreateContentViewModel.js" resourceType="Script" /> </clientResources> </module> |
…then I prepared _hasDisplayOnContentCreate method responsible for checking if content should be displayed on the Additional properties panel.
1 2 3 |
_hasDisplayOnContentCreate: function (property) { return property.settings && property.settings.displayOnContentCreate; }, |
It is used by two internal methods:
- _propertyFilter – return the collection of properties that should be displayed on CreateContent view
- _hasRequiredProperties – checks if content has at least one required property. If no, then panel wan’t be displayed. So displayOnContentCreate property value should be also included in this condition.
After running the solution all properties marked with DisplayOnContentCreateAttribute will be displayed on Additional properties tab.
Code is available on Gist