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");
}
}
}
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;
}
}
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 :-)
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.
Hi Jesper
You can do it using interceptors. Couple articles about that - https://skrift.io/issues/changing-backoffice-functionality-without-changing-core-code/
https://nathanw.com.au/blog/customising-umbraco-without-touching-core-files/
Thanks,
Alex
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:
Best regards,
iNETZO
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
andTemplateId
. So you might want and additional check onAllowedTemplates
: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:
/Bjarne
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 :-)
is working on a reply...