Skip to content

Instantly share code, notes, and snippets.

@jcdcdev
Last active November 21, 2023 16:10
Show Gist options
  • Save jcdcdev/8125b2605f06aeaea8dd9b4c58b0c65a to your computer and use it in GitHub Desktop.
Save jcdcdev/8125b2605f06aeaea8dd9b4c58b0c65a to your computer and use it in GitHub Desktop.
Back Office Organiser Example
using jcdcdev.Umbraco.Core.Extensions;
using StackExchange.Profiling.Internal;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;
public class ExampleContentTypeOrganiseAction : IContentTypeOrganiseAction
{
// Handle all but container types (Folders)
public bool CanMove(IContentType contentType, IContentTypeService contentTypeService) => !contentType.IsContainer;
public void Move(IContentType contentType, IContentTypeService contentTypeService)
{
var folderId = -1;
var folderName = string.Empty;
var isComposition = contentTypeService.GetComposedOf(contentType.Id).Any();
if (contentType.AllowedTemplates?.Any() ?? false)
{
folderName = "Pages";
}
else if (isComposition)
{
folderName = "Compositions";
}
else if (contentType.IsElement)
{
folderName = "Element Types";
}
if (!folderName.IsNullOrWhiteSpace())
{
folderId = contentTypeService.GetOrCreateFolder(folderName).Id;
}
contentTypeService.Move(contentType, folderId);
}
}
public class Composer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
// Make sure you register your action BEFORE the default!
builder.ContentTypeOrganiseActions().Insert<ExampleContentTypeOrganiseAction>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment