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 : IContentTypeOrganiseAction
10+ {
11+ private readonly IDataTypeService _dataTypeService ;
12+
13+ public ElementTypeOrganiser ( IDataTypeService dataTypeService )
14+ {
15+ _dataTypeService = dataTypeService ;
16+ }
17+
18+ public bool CanMove ( IContentType contentType , IContentTypeService contentTypeService ) => contentType . IsElement ;
19+
20+ public void Move ( IContentType contentType , IContentTypeService contentTypeService )
21+ {
22+ var nestedContentDataTypes = _dataTypeService . GetByEditorAlias ( Umbraco . Cms . Core . Constants . PropertyEditors . Aliases . NestedContent ) . Select ( x => x . ConfigurationAs < NestedContentConfiguration > ( ) ) ;
23+ var blockGridDataTypes = _dataTypeService . GetByEditorAlias ( Umbraco . Cms . Core . Constants . PropertyEditors . Aliases . BlockGrid ) . Select ( x => x . ConfigurationAs < BlockGridConfiguration > ( ) ) ;
24+ var blockListDataTypes = _dataTypeService . GetByEditorAlias ( Umbraco . Cms . Core . Constants . PropertyEditors . Aliases . BlockList ) . Select ( x => x . ConfigurationAs < BlockListConfiguration > ( ) ) ;
25+
26+ var nestedContentContentTypeAliases = new List < string > ( ) ;
27+ var gridContentTypeKeys = new List < Guid > ( ) ;
28+ var blockContentTypeKeys = new List < Guid > ( ) ;
29+ foreach ( var blockGridDataType in blockGridDataTypes )
30+ {
31+ foreach ( var blockGrid in blockGridDataType ? . Blocks ?? Array . Empty < BlockGridConfiguration . BlockGridBlockConfiguration > ( ) )
32+ {
33+ gridContentTypeKeys . Add ( blockGrid . ContentElementTypeKey ) ;
34+ if ( blockGrid . SettingsElementTypeKey . HasValue )
35+ {
36+ gridContentTypeKeys . Add ( blockGrid . SettingsElementTypeKey . Value ) ;
37+ }
38+ }
39+ }
40+
41+ foreach ( var blockListDataType in blockListDataTypes )
42+ {
43+ foreach ( var blockList in blockListDataType ? . Blocks ?? Array . Empty < BlockListConfiguration . BlockConfiguration > ( ) )
44+ {
45+ blockContentTypeKeys . Add ( blockList . ContentElementTypeKey ) ;
46+ if ( blockList . SettingsElementTypeKey . HasValue )
47+ {
48+ blockContentTypeKeys . Add ( blockList . SettingsElementTypeKey . Value ) ;
49+ }
50+ }
51+ }
52+
53+ foreach ( var nestedContentDataType in nestedContentDataTypes )
54+ {
55+ if ( nestedContentDataType == null )
56+ {
57+ continue ;
58+ }
59+
60+ var aliases = nestedContentDataType . ContentTypes ? . Select ( x => x . Alias ) . WhereNotNull ( ) ?? Array . Empty < string > ( ) ;
61+ nestedContentContentTypeAliases . AddRange ( aliases ) ;
62+ }
63+
64+ var isNestedContent = nestedContentContentTypeAliases . Contains ( contentType . Alias ) ;
65+ var isBlockGrid = gridContentTypeKeys . Contains ( contentType . Key ) ;
66+ var isBlockList = blockContentTypeKeys . Contains ( contentType . Key ) ;
67+
68+ var parent = contentTypeService . GetOrCreateFolder ( "Element Types" ) ;
69+ var folderName = string . Empty ;
70+ if ( isNestedContent && ! isBlockGrid && ! isBlockList )
71+ {
72+ folderName = "Nested Content" ;
73+ }
74+ else if ( isBlockGrid && ! isNestedContent && ! isBlockList )
75+ {
76+ folderName = "Block Grid" ;
77+ }
78+ else if ( isBlockList && ! isNestedContent && ! isBlockGrid )
79+ {
80+ folderName = "Block List" ;
81+ }
82+
83+ if ( ! string . IsNullOrWhiteSpace ( folderName ) )
84+ {
85+ parent = contentTypeService . GetOrCreateFolder ( folderName , parent . Id ) ;
86+ }
87+
88+ contentTypeService . Move ( contentType , parent . Id ) ;
89+ }
90+ }
0 commit comments