Created
January 18, 2022 11:08
-
-
Save YodasMyDad/5ea8c610d457905ea9821d2901da01e0 to your computer and use it in GitHub Desktop.
Hides properties on a node if it has children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HidePropertiesIfChildren : INotificationHandler<SendingContentNotification> | |
{ | |
private readonly IServiceProvider _container; | |
public HidePropertiesIfChildren(IServiceProvider container) | |
{ | |
_container = container; | |
} | |
public void Handle(SendingContentNotification notification) | |
{ | |
if (notification.Content.ContentTypeAlias.Equals("myDocumentTypeAlias")) | |
{ | |
using (var scope = _container.CreateScope()) | |
{ | |
var umbracoContextAccessor = scope.ServiceProvider.GetService<IUmbracoContextAccessor>(); | |
umbracoContextAccessor!.TryGetUmbracoContext(out var umbracoContext); | |
var node = umbracoContext.Content.GetById(notification.Content.Id); | |
if (node.Children.Any()) | |
{ | |
foreach (var variant in notification.Content.Variants) | |
{ | |
foreach (var variantTab in variant.Tabs) | |
{ | |
var editiedProperties = variantTab.Properties.ToList(); | |
var aliasesToRemove = new List<string> { "propertyAliasOne", "propertyAliasTwo" }; | |
editiedProperties.RemoveAll(x => aliasesToRemove.Contains(x.Alias)); | |
variantTab.Properties = editiedProperties; | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment