Skip to content

Commit e01edf9

Browse files
committed
feature: element type organiser
1 parent 6e1da6b commit e01edf9

16 files changed

+159
-174
lines changed

src/Umbraco.Community.BackOfficeOrganiser/Composing/BackofficeOrganiserNotificationHandler.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,19 @@
99

1010
namespace Umbraco.Community.BackOfficeOrganiser.Composing;
1111

12-
public class BackofficeOrganiserNotificationHandler :
13-
INotificationHandler<DataTypeSavedNotification>,
14-
INotificationHandler<ContentTypeSavedNotification>,
15-
INotificationHandler<MemberTypeSavedNotification>,
16-
INotificationHandler<MediaTypeSavedNotification>
12+
public class BackofficeOrganiserNotificationHandler(
13+
DataTypeOrganiser dataTypeOrganiser,
14+
ContentTypeOrganiser contentTypeOrganiser,
15+
MediaTypeOrganiser mediaTypeOrganiser,
16+
MemberTypeOrganiser memberTypeOrganiser,
17+
IOptions<BackOfficeOrganiserOptions> options)
18+
:
19+
INotificationHandler<DataTypeSavedNotification>,
20+
INotificationHandler<ContentTypeSavedNotification>,
21+
INotificationHandler<MemberTypeSavedNotification>,
22+
INotificationHandler<MediaTypeSavedNotification>
1723
{
18-
private readonly ContentTypeOrganiser _contentTypeOrganiser;
19-
private readonly DataTypeOrganiser _dataTypeOrganiser;
20-
private readonly MediaTypeOrganiser _mediaTypeOrganiser;
21-
private readonly MemberTypeOrganiser _memberTypeOrganiser;
22-
private readonly BackOfficeOrganiserOptions _options;
23-
24-
public BackofficeOrganiserNotificationHandler(
25-
DataTypeOrganiser dataTypeOrganiser,
26-
ContentTypeOrganiser contentTypeOrganiser,
27-
MediaTypeOrganiser mediaTypeOrganiser,
28-
MemberTypeOrganiser memberTypeOrganiser,
29-
IOptions<BackOfficeOrganiserOptions> options)
30-
{
31-
_dataTypeOrganiser = dataTypeOrganiser;
32-
_contentTypeOrganiser = contentTypeOrganiser;
33-
_mediaTypeOrganiser = mediaTypeOrganiser;
34-
_memberTypeOrganiser = memberTypeOrganiser;
35-
_options = options.Value;
36-
}
24+
private readonly BackOfficeOrganiserOptions _options = options.Value;
3725

3826
public void Handle(ContentTypeSavedNotification notification)
3927
{
@@ -44,7 +32,7 @@ public void Handle(ContentTypeSavedNotification notification)
4432

4533
foreach (var item in notification.SavedEntities)
4634
{
47-
_contentTypeOrganiser.Organise(item);
35+
contentTypeOrganiser.Organise(item);
4836
}
4937
}
5038

@@ -57,7 +45,7 @@ public void Handle(DataTypeSavedNotification notification)
5745

5846
foreach (var dataType in notification.SavedEntities)
5947
{
60-
_dataTypeOrganiser.Organise(dataType);
48+
dataTypeOrganiser.Organise(dataType);
6149
}
6250
}
6351

@@ -70,7 +58,7 @@ public void Handle(MediaTypeSavedNotification notification)
7058

7159
foreach (var item in notification.SavedEntities)
7260
{
73-
_mediaTypeOrganiser.Organise(item);
61+
mediaTypeOrganiser.Organise(item);
7462
}
7563
}
7664

@@ -83,7 +71,7 @@ public void Handle(MemberTypeSavedNotification notification)
8371

8472
foreach (var item in notification.SavedEntities)
8573
{
86-
_memberTypeOrganiser.Organise(item);
74+
memberTypeOrganiser.Organise(item);
8775
}
8876
}
8977
}

src/Umbraco.Community.BackOfficeOrganiser/Composing/Composer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void Compose(IUmbracoBuilder builder)
2828
builder.ManifestFilters().Append<ManifestFilter>();
2929

3030
builder.DataTypeOrganiseActions().Append<DefaultDataTypeOrganiseAction>();
31+
builder.ContentTypeOrganiseActions().Append<ElementTypeOrganiser>();
3132
builder.ContentTypeOrganiseActions().Append<DefaultContentTypeOrganiseAction>();
3233
builder.MediaTypeOrganiseActions().Append<DefaultMediaTypeOrganiseAction>();
3334
builder.MemberTypeOrganiseActions().Append<DefaultMemberTypeOrganiseAction>();

src/Umbraco.Community.BackOfficeOrganiser/Controllers/BackOfficeOrganiserController.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,15 @@ namespace Umbraco.Community.BackOfficeOrganiser.Controllers;
1515
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
1616
[DisableBrowserCache]
1717
[UmbracoRequireHttps]
18-
public class BackOfficeOrganiserController : UmbracoApiController
18+
public class BackOfficeOrganiserController(IBackOfficeOrganiserService service) : UmbracoApiController
1919
{
20-
private readonly IBackOfficeOrganiserService _service;
21-
22-
public BackOfficeOrganiserController(IBackOfficeOrganiserService service)
23-
{
24-
_service = service;
25-
}
26-
2720
[HttpPost]
2821
public IActionResult Organise(OrganiseRequest model)
2922
{
3023
var success = true;
3124
foreach (var type in model.Types.Select(DetermineOrganiseType).Distinct())
3225
{
33-
var attempt = _service.Organise(type);
26+
var attempt = service.Organise(type);
3427
if (!attempt.Success)
3528
{
3629
success = false;

src/Umbraco.Community.BackOfficeOrganiser/Organisers/BackOfficeOrganiserBase.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
namespace Umbraco.Community.BackOfficeOrganiser.Organisers;
44

5-
public abstract class BackOfficeOrganiserBase<T> : IBackOfficeOrganiser<T>
5+
public abstract class BackOfficeOrganiserBase<T>(ILogger logger) : IBackOfficeOrganiser<T>
66
{
7-
public readonly ILogger Logger;
8-
9-
protected BackOfficeOrganiserBase(ILogger logger)
10-
{
11-
Logger = logger;
12-
}
7+
public readonly ILogger Logger = logger;
138

149
protected virtual void PostOrganiseAll()
1510
{

src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ContentTypeOrganiseActionCollection.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,4 @@
22

33
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;
44

5-
public class ContentTypeOrganiseActionCollection : BuilderCollectionBase<IContentTypeOrganiseAction>
6-
{
7-
public ContentTypeOrganiseActionCollection(Func<IEnumerable<IContentTypeOrganiseAction>> items)
8-
: base(items)
9-
{
10-
}
11-
}
5+
public class ContentTypeOrganiseActionCollection(Func<IEnumerable<IContentTypeOrganiseAction>> items) : BuilderCollectionBase<IContentTypeOrganiseAction>(items);

src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ContentTypeOrganiser.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,22 @@
55

66
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;
77

8-
public class ContentTypeOrganiser : BackOfficeOrganiserBase<IContentType>
8+
public class ContentTypeOrganiser(
9+
ILogger<ContentTypeOrganiser> logger,
10+
IContentTypeService contentTypeService,
11+
ContentTypeOrganiseActionCollection organiseActions)
12+
: BackOfficeOrganiserBase<IContentType>(logger)
913
{
10-
private readonly IContentTypeService _contentTypeService;
11-
private readonly ContentTypeOrganiseActionCollection _organiseActions;
12-
13-
public ContentTypeOrganiser(
14-
ILogger<ContentTypeOrganiser> logger,
15-
IContentTypeService contentTypeService,
16-
ContentTypeOrganiseActionCollection organiseActions) : base(logger)
17-
{
18-
_contentTypeService = contentTypeService;
19-
_organiseActions = organiseActions;
20-
}
21-
22-
protected override List<IContentType> GetAll() => _contentTypeService.GetAll().ToList();
14+
protected override List<IContentType> GetAll() => contentTypeService.GetAll().ToList();
2315

2416
public override void Organise(IContentType contentType)
2517
{
26-
var organiser = _organiseActions.FirstOrDefault(x => x.CanMove(contentType, _contentTypeService));
27-
organiser?.Move(contentType, _contentTypeService);
18+
var organiser = organiseActions.FirstOrDefault(x => x.CanMove(contentType, contentTypeService));
19+
organiser?.Move(contentType, contentTypeService);
2820
}
2921

3022
protected override void PostOrganiseAll()
3123
{
32-
_contentTypeService.DeleteAllEmptyContainers();
24+
contentTypeService.DeleteAllEmptyContainers();
3325
}
3426
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using jcdcdev.Umbraco.Core.Extensions;
2+
using Umbraco.Cms.Core.Models;
3+
using Umbraco.Cms.Core.PropertyEditors;
4+
using Umbraco.Cms.Core.Services;
5+
using Umbraco.Extensions;
6+
7+
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;
8+
9+
public class ElementTypeOrganiser(IDataTypeService dataTypeService) : IContentTypeOrganiseAction
10+
{
11+
public bool CanMove(IContentType contentType, IContentTypeService contentTypeService) => contentType.IsElement;
12+
13+
public void Move(IContentType contentType, IContentTypeService contentTypeService)
14+
{
15+
var nestedContentDataTypes = dataTypeService.GetByEditorAlias(Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.NestedContent).Select(x => x.ConfigurationAs<NestedContentConfiguration>());
16+
var blockGridDataTypes = dataTypeService.GetByEditorAlias(Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid).Select(x => x.ConfigurationAs<BlockGridConfiguration>());
17+
var blockListDataTypes = dataTypeService.GetByEditorAlias(Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockList).Select(x => x.ConfigurationAs<BlockListConfiguration>());
18+
19+
var nestedContentContentTypeAliases = new List<string>();
20+
var gridContentTypeKeys = new List<Guid>();
21+
var blockContentTypeKeys = new List<Guid>();
22+
foreach (var blockGridDataType in blockGridDataTypes)
23+
{
24+
foreach (var blockGrid in blockGridDataType?.Blocks ?? [])
25+
{
26+
gridContentTypeKeys.Add(blockGrid.ContentElementTypeKey);
27+
if (blockGrid.SettingsElementTypeKey.HasValue)
28+
{
29+
gridContentTypeKeys.Add(blockGrid.SettingsElementTypeKey.Value);
30+
}
31+
}
32+
}
33+
34+
foreach (var blockListDataType in blockListDataTypes)
35+
{
36+
foreach (var blockList in blockListDataType?.Blocks ?? [])
37+
{
38+
blockContentTypeKeys.Add(blockList.ContentElementTypeKey);
39+
if (blockList.SettingsElementTypeKey.HasValue)
40+
{
41+
blockContentTypeKeys.Add(blockList.SettingsElementTypeKey.Value);
42+
}
43+
}
44+
}
45+
46+
foreach (var nestedContentDataType in nestedContentDataTypes)
47+
{
48+
if (nestedContentDataType == null)
49+
{
50+
continue;
51+
}
52+
53+
var aliases = nestedContentDataType.ContentTypes?.Select(x => x.Alias).WhereNotNull() ?? [];
54+
nestedContentContentTypeAliases.AddRange(aliases);
55+
}
56+
57+
var isNestedContent = nestedContentContentTypeAliases.Contains(contentType.Alias);
58+
var isBlockGrid = gridContentTypeKeys.Contains(contentType.Key);
59+
var isBlockList = blockContentTypeKeys.Contains(contentType.Key);
60+
61+
var parent = contentTypeService.GetOrCreateFolder("Element Types");
62+
var folderName = string.Empty;
63+
if (isNestedContent && !isBlockGrid && !isBlockList)
64+
{
65+
folderName = "Nested Content";
66+
}
67+
else if (isBlockGrid && !isNestedContent && !isBlockList)
68+
{
69+
folderName = "Block Grid";
70+
}
71+
else if (isBlockList && !isNestedContent && !isBlockGrid)
72+
{
73+
folderName = "Block List";
74+
}
75+
if (!string.IsNullOrWhiteSpace(folderName))
76+
{
77+
parent = contentTypeService.GetOrCreateFolder(folderName, parent.Id);
78+
}
79+
80+
contentTypeService.Move(contentType, parent.Id);
81+
}
82+
}

src/Umbraco.Community.BackOfficeOrganiser/Organisers/DataTypes/DataTypeOrganiseActionCollection.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,4 @@
22

33
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.DataTypes;
44

5-
public class DataTypeOrganiseActionCollection : BuilderCollectionBase<IDataTypeOrganiseAction>
6-
{
7-
public DataTypeOrganiseActionCollection(Func<IEnumerable<IDataTypeOrganiseAction>> items)
8-
: base(items)
9-
{
10-
}
11-
}
5+
public class DataTypeOrganiseActionCollection(Func<IEnumerable<IDataTypeOrganiseAction>> items) : BuilderCollectionBase<IDataTypeOrganiseAction>(items);

src/Umbraco.Community.BackOfficeOrganiser/Organisers/DataTypes/DataTypeOrganiser.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,22 @@
55

66
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.DataTypes;
77

8-
public class DataTypeOrganiser : BackOfficeOrganiserBase<IDataType>
8+
public class DataTypeOrganiser(
9+
ILogger<DataTypeOrganiser> logger,
10+
IDataTypeService dataTypeService,
11+
DataTypeOrganiseActionCollection organiseActions)
12+
: BackOfficeOrganiserBase<IDataType>(logger)
913
{
10-
private readonly IDataTypeService _dataTypeService;
11-
private readonly DataTypeOrganiseActionCollection _organiseActions;
12-
13-
public DataTypeOrganiser(
14-
ILogger<DataTypeOrganiser> logger,
15-
IDataTypeService dataTypeService,
16-
DataTypeOrganiseActionCollection organiseActions) : base(logger)
17-
{
18-
_dataTypeService = dataTypeService;
19-
_organiseActions = organiseActions;
20-
}
21-
2214
public override void Organise(IDataType dataType)
2315
{
24-
var organiser = _organiseActions.FirstOrDefault(x => x.CanMove(dataType, _dataTypeService));
25-
organiser?.Move(dataType, _dataTypeService);
16+
var organiser = organiseActions.FirstOrDefault(x => x.CanMove(dataType, dataTypeService));
17+
organiser?.Move(dataType, dataTypeService);
2618
}
2719

28-
protected override List<IDataType> GetAll() => _dataTypeService.GetAll().ToList();
20+
protected override List<IDataType> GetAll() => dataTypeService.GetAll().ToList();
2921

3022
protected override void PostOrganiseAll()
3123
{
32-
_dataTypeService.DeleteAllEmptyContainers();
24+
dataTypeService.DeleteAllEmptyContainers();
3325
}
3426
}

src/Umbraco.Community.BackOfficeOrganiser/Organisers/DataTypes/DefaultDataTypeOrganiseAction.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@
99

1010
namespace Umbraco.Community.BackOfficeOrganiser.Organisers.DataTypes;
1111

12-
public class DefaultDataTypeOrganiseAction : IDataTypeOrganiseAction
12+
public class DefaultDataTypeOrganiseAction(IOptions<BackOfficeOrganiserOptions> options, ILogger<DefaultDataTypeOrganiseAction> logger) : IDataTypeOrganiseAction
1313
{
14-
private readonly ILogger _logger;
15-
private readonly BackOfficeOrganiserOptions _options;
16-
17-
public DefaultDataTypeOrganiseAction(IOptions<BackOfficeOrganiserOptions> options, ILogger<DefaultDataTypeOrganiseAction> logger)
18-
{
19-
_logger = logger;
20-
_options = options.Value;
21-
}
14+
private readonly ILogger _logger = logger;
15+
private readonly BackOfficeOrganiserOptions _options = options.Value;
2216

2317
public bool CanMove(IDataType dataType, IDataTypeService dataTypeService) => true;
2418

0 commit comments

Comments
 (0)