AI OnAI Off
If you're happy for the content type name to be saved within the "name" field, you could do this by hooking into the "SavingContent" event and prefixing the name before it saves like this:
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class BlockNamingInitialisation : IInitializableModule
{
private IContentEvents _contentEvents;
private IContentTypeRepository _contentTypeRepository;
public void Initialize(InitializationEngine context)
{
_contentEvents = context.Locate.ContentEvents();
_contentTypeRepository = context.Locate.ContentTypeRepository();
//Attach the event handler to the creating and saving events
_contentEvents.SavingContent += AddTypePrefixToBlock;
_contentEvents.CreatingContent += AddTypePrefixToBlock;
}
private void AddTypePrefixToBlock(object sender, ContentEventArgs e)
{
//Only modify blocks whose names aren't already prefixed with a [
if (e.Content is BlockData && !e.Content.Name.StartsWith('['))
{
//Fetch the content type name (you may want to cache this lookup - it'll get called a lot)
var contentTypeName = _contentTypeRepository.Load(e.Content.ContentTypeID).LocalizedName;
//Prefix the content name with the content type name
e.Content.Name = $"[{contentTypeName}] {e.Content.Name}";
}
}
public void Uninitialize(InitializationEngine context)
{
//Remove the event handlers on uninitialise
_contentEvents.SavingContent -= AddTypePrefixToBlock;
_contentEvents.CreatingContent -= AddTypePrefixToBlock;
}
}
Is it possible to control the "display name" for blocks in edit mode?
For instance, we would like to always have the block name prefixed with the block type. It has been found to be of great help when maintaining larger sites with lots of content.
In this example we have manually prefixed each block with the type name, but we'd rather have it done automatically if possible.