When creating Umbraco projects you often end up making document types for content such as office locations, categories or employees where the content type does not have a template and/or is unable to be previewed directly.
Some clients might request to be redirected to the home page (or some related page) while others might request that the preview button is removed to avoid confusion. Below is an example of doing the latter.
Create a SendingContentNotificationHandler.cs
to listen for content notifications and disable the preview for nodes without templates:
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
namespace Adventures
{
public class SendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
{
public SendingContentNotificationHandler() { }
public void Handle(SendingContentNotification notification)
{
if (notification.Content.AllowedTemplates?.Count == 0)
{
notification.Content.AllowPreview = false;
}
}
}
}
Register your handler in Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.AddNotificationHandler<SendingContentNotification, SendingContentNotificationHandler>()
.Build();
}
Top comments (0)