Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Weber 54 posts 170 karma points c-trib
    Apr 09, 2021 @ 08:37
    Jesper Weber
    0

    Dynamically hide content app

    Hi,

    I know I can hide a content app bare on the node type e.g. content or media.

    But is it possible to hide the content based on other informations? In my case I would like hide my content app if the current node has no template.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 09, 2021 @ 22:48
  • iNETZO 134 posts 497 karma points c-trib
    Apr 10, 2021 @ 12:05
    iNETZO
    1

    Hi Jesper,

    Another approach is removing interface items, like contentapps, by using EditorModel Events (https://our.umbraco.com/documentation/reference/events/EditorModel-Events/) For example:

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class HideContentAppComposer : ComponentComposer<HideContentAppEvents>
    {
    }
    
    public class HideContentAppEvents : IComponent
    {
        // Initialize: runs once when Umbraco starts
        public void Initialize()
        {
            EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
        }
    
        // Terminate: runs once when Umbraco stops
        public void Terminate()
        {
            // Unsubscribe during shutdown
            EditorModelEventManager.SendingContentModel -= EditorModelEventManager_SendingContentModel;
        }
    
        private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
        {
            // Check if the node has an template
            if (string.IsNullOrEmpty(e.Model.TemplateAlias))
            {
                // Remove the ContentApp by Alias
                e.Model.ContentApps = e.Model.ContentApps.Where(x => x.Alias != "YourContentAppAlias");
            }
        }
    }
    

    Best regards,

    iNETZO

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Apr 11, 2021 @ 11:12
    Bjarne Fyrstenborg
    1

    Hi Jesper

    Using the approach from iNETZO works, however in my test in removing allowed templates on a document type and re-publish the node, it still had the a value in TemplateAlias and TemplateId. So you might want and additional check on AllowedTemplates:

    // Check if the node has an template
    if (string.IsNullOrEmpty(e.Model.TemplateAlias) || e.Model.AllowedTemplates.Count == 0)
    {
        // Remove the ContentApp by Alias
        e.Model.ContentApps = e.Model.ContentApps.Where(x => x.Alias != "YourContentAppAlias");
    }
    

    However since you mention "I would like hide my content app" I guess you have control over how the content app is registered, so instead of removing in afterwards you could add some additional checks here:

    public class WordCounterApp : IContentAppFactory
    {
        public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups)
        {
            // Can implement some logic with userGroups if needed
            // Allowing us to display the content app with some restrictions for certain groups
            if (userGroups.All(x => x.Alias.ToLowerInvariant() != Umbraco.Core.Constants.Security.AdminGroupAlias))
                return null;
    
            // only show app on content items
            if (!(source is IContent))
            {
                return null;
            }
    
            var content = ((IContent)source);
    
            // only show app on content items with template
            if (content.TemplateId == null)
            {
                return null;
            }
    
            var myContentApp = new ContentApp
            {
                Alias = "myContentApp",
                Name = "My Content App",
                Icon = "icon-calculator",
                View = "/App_Plugins/MyContentApp/app.html",
                Weight = 0
            };
    
            return myContentApp;
        }
    }
    

    /Bjarne

  • Jesper Weber 54 posts 170 karma points c-trib
    Apr 12, 2021 @ 13:30
    Jesper Weber
    0

    Thanks for all the replies.

    I might have forgot to tell that it's a content app I'm building myself. So I have control over the registration of the content app and the answer from Bjarne works perfect :-)

Please Sign in or register to post replies

Write your reply to:

Draft