Skip to content

Instantly share code, notes, and snippets.

@YodasMyDad
Created January 18, 2022 11:08
Show Gist options
  • Save YodasMyDad/5ea8c610d457905ea9821d2901da01e0 to your computer and use it in GitHub Desktop.
Save YodasMyDad/5ea8c610d457905ea9821d2901da01e0 to your computer and use it in GitHub Desktop.
Hides properties on a node if it has children
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