usingUmbraco.Cms.Core.Events;usingUmbraco.Cms.Core.Notifications;usingUmbraco.Cms.Core.Services;namespaceDoccers.Core.Components;publicclassContentPublishedNotificationHandler(IContentService contentService,IRelationService relationService) :INotificationHandler<ContentPublishedNotification>{publicvoidHandle(ContentPublishedNotification notification) {var home =contentService.GetRootContent().FirstOrDefault();if (home ==null) return; // Get the relation type by aliasvar relationType =relationService.GetRelationTypeByAlias("homesick");if (relationType ==null) return;foreach (var entity innotification.PublishedEntities .Where(x =>x.Id!=home.Id)) { // Check if they are already relatedif (!relationService.AreRelated(home.Id,entity.Id)) { // If not then let us relate the currenty entity to homerelationService.Relate(home.Id,entity.Id, relationType); } } }}
To have Umbraco recognize our Notification Handler we need to register it in a composer: