1
+ using Newtonsoft . Json ;
2
+ using Our . Umbraco . DocTypeGridEditor . Models ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
6
+ using Umbraco . Core ;
7
+ using Umbraco . Core . Composing ;
8
+ using Umbraco . Core . Models ;
9
+ using Umbraco . Core . Models . Editors ;
10
+ using Umbraco . Core . PropertyEditors ;
11
+
12
+ namespace Our . Umbraco . DocTypeGridEditor . ValueProcessing
13
+ {
14
+ public class DocTypeGridEditorDataValueReference : IDataValueReferenceFactory , IDataValueReference
15
+ {
16
+ private readonly Lazy < Dictionary < string , IContentType > > _contentTypes ;
17
+
18
+ public IDataValueReference GetDataValueReference ( ) => this ;
19
+
20
+ public bool IsForEditor ( IDataEditor dataEditor ) => dataEditor . Alias . InvariantEquals ( Constants . PropertyEditors . Aliases . Grid ) ;
21
+
22
+
23
+ public DocTypeGridEditorDataValueReference ( )
24
+ {
25
+ _contentTypes = new Lazy < Dictionary < string , IContentType > > ( ( ) => Current . Services . ContentTypeService . GetAll ( ) . ToDictionary ( c => c . Alias ) ) ;
26
+ }
27
+
28
+ public IEnumerable < UmbracoEntityReference > GetReferences ( object value )
29
+ {
30
+ var result = new List < UmbracoEntityReference > ( ) ;
31
+ var _propertyEditors = Current . PropertyEditors ;
32
+ var rawJson = value == null ? string . Empty : value is string str ? str : value . ToString ( ) ;
33
+ DeserializeGridValue ( rawJson , out var dtgeValues ) ;
34
+
35
+ foreach ( var control in dtgeValues )
36
+ {
37
+ if ( _contentTypes . Value . TryGetValue ( control . ContentTypeAlias , out var contentType ) )
38
+ {
39
+ var propertyTypes = contentType . CompositionPropertyTypes . ToDictionary ( x => x . Alias , x => x ) ;
40
+ var properties = control . Value . Properties ( ) ;
41
+
42
+ foreach ( var property in properties )
43
+ {
44
+ if ( propertyTypes . TryGetValue ( property . Name , out var propertyType ) )
45
+ {
46
+ if ( _propertyEditors . TryGet ( propertyType . PropertyEditorAlias , out var propertyEditor ) )
47
+ {
48
+ if ( propertyEditor . GetValueEditor ( ) is IDataValueReference reference )
49
+ {
50
+ var propertyValue = property . Value . ToString ( ) ;
51
+ var refs = reference . GetReferences ( propertyValue ) ;
52
+ result . AddRange ( refs ) ;
53
+ }
54
+ }
55
+ }
56
+ }
57
+ }
58
+ }
59
+ return result ;
60
+
61
+ }
62
+
63
+ internal GridValue DeserializeGridValue ( string rawJson , out IEnumerable < DocTypeGridEditorValue > dtgeValues )
64
+ {
65
+ var grid = JsonConvert . DeserializeObject < GridValue > ( rawJson ) ;
66
+
67
+ // Find all controls that uses DTGE editor
68
+ var controls = grid . Sections . SelectMany ( x => x . Rows . SelectMany ( r => r . Areas ) . SelectMany ( a => a . Controls ) ) . ToArray ( ) ;
69
+ dtgeValues = controls . Where ( x => x . Editor . Alias . ToLowerInvariant ( ) == "doctype" ) . Select ( x => x . Value . ToObject < DocTypeGridEditorValue > ( ) ) ;
70
+
71
+ return grid ;
72
+ }
73
+ }
74
+ }
0 commit comments