Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit b439fc4

Browse files
committed
Enables media tracking in DTGE
1 parent 71b3889 commit b439fc4

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

src/Our.Umbraco.DocTypeGridEditor/Composing/DocTypeGridEditorComposer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Our.Umbraco.DocTypeGridEditor.Extensions;
22
using Our.Umbraco.DocTypeGridEditor.ValueProcessing;
3+
using Umbraco.Core;
34
using Umbraco.Core.Composing;
45

56
namespace Our.Umbraco.DocTypeGridEditor.Composing
@@ -12,6 +13,7 @@ public class DocTypeGridEditorComposer : IUserComposer
1213
public void Compose(Composition composition)
1314
{
1415
composition.DocTypeGridEditorValueProcessors().Append<UmbracoTagsValueProcessor>();
16+
composition.DataValueReferenceFactories().Append<DocTypeGridEditorDataValueReference>();
1517
}
1618
}
1719
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Our.Umbraco.DocTypeGridEditor.Models
10+
{
11+
public class DocTypeGridEditorValue
12+
{
13+
[JsonProperty("value")]
14+
public JObject Value { get; set; }
15+
[JsonProperty("dtgeContentTypeAlias")]
16+
public string ContentTypeAlias { get; set; }
17+
[JsonProperty("id")]
18+
public Guid Id { get; set; }
19+
}
20+
}

src/Our.Umbraco.DocTypeGridEditor/Our.Umbraco.DocTypeGridEditor.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@
230230
<ItemGroup>
231231
<Compile Include="Bootstrap.cs" />
232232
<Compile Include="Composing\Current.cs" />
233+
<Compile Include="Models\DocTypeGridEditorValue.cs" />
234+
<Compile Include="ValueProcessing\DocTypeGridEditorDataValueReference.cs" />
233235
<Compile Include="Extensions\JsonExtensions.cs" />
234236
<Compile Include="Helpers\DocTypeGridEditorHelper.cs" />
235237
<Compile Include="Helpers\XmlHelper.cs" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)