Skip to content

Commit 493eda9

Browse files
Add DocTypeGridEditorDataTypeConfigurationConnector
1 parent 54ad642 commit 493eda9

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Linq;
7+
using Umbraco.Core;
8+
using Umbraco.Core.Configuration.Grid;
9+
using Umbraco.Core.Deploy;
10+
using Umbraco.Core.Models;
11+
using Umbraco.Core.Services;
12+
using Umbraco.Deploy.Connectors.DataTypeConfigurationConnectors;
13+
using Umbraco.Deploy.Core;
14+
using Umbraco.Web.PropertyEditors;
15+
16+
namespace Umbraco.Deploy.Contrib.DataTypeConfigurationConnectors
17+
{
18+
/// <summary>
19+
/// Implements a Grid layout data type configuration connector supporting DocTypeGridEditor.
20+
/// </summary>
21+
public class DocTypeGridEditorDataTypeConfigurationConnector : DataTypeConfigurationConnectorBase2
22+
{
23+
private readonly IGridConfig _gridConfig;
24+
private readonly IContentTypeService _contentTypeService;
25+
26+
/// <inheritdoc />
27+
public override IEnumerable<string> PropertyEditorAliases { get; } = new[]
28+
{
29+
Constants.PropertyEditors.Aliases.Grid
30+
};
31+
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="DocTypeGridEditorDataTypeConfigurationConnector" /> class.
34+
/// </summary>
35+
/// <param name="gridConfig">The grid configuration.</param>
36+
/// <param name="contentTypeService">The content type service.</param>
37+
public DocTypeGridEditorDataTypeConfigurationConnector(IGridConfig gridConfig, IContentTypeService contentTypeService)
38+
{
39+
_gridConfig = gridConfig;
40+
_contentTypeService = contentTypeService;
41+
}
42+
43+
/// <inheritdoc />
44+
public override string ToArtifact(IDataType dataType, ICollection<ArtifactDependency> dependencies, IContextCache contextCache)
45+
{
46+
if (dataType.ConfigurationAs<GridConfiguration>() is GridConfiguration gridConfiguration &&
47+
gridConfiguration.Items?.ToObject<GridConfigurationItems>() is GridConfigurationItems gridConfigurationItems)
48+
{
49+
// Get all element types (when needed)
50+
var allElementTypes = new Lazy<IEnumerable<IContentType>>(() => _contentTypeService.GetAll().Where(x => x.IsElement).ToList());
51+
52+
// Process DTGE editors
53+
foreach (var gridEditor in GetGridEditors(gridConfigurationItems).Where(IsDocTypeGridEditor))
54+
{
55+
if (gridEditor.Config.TryGetValue("allowedDocTypes", out var allowedDocTypesConfig) &&
56+
allowedDocTypesConfig is JArray allowedDocTypes &&
57+
allowedDocTypes.Count > 0)
58+
{
59+
string[] docTypes = allowedDocTypes.Values<string>().WhereNotNull().ToArray();
60+
61+
// Use regex matching
62+
AddDependencies(dependencies, allElementTypes.Value.Where(x => docTypes.Any(y => Regex.IsMatch(x.Alias, y))));
63+
}
64+
else
65+
{
66+
// Add all element types as dependencies and stop processing
67+
AddDependencies(dependencies, allElementTypes.Value);
68+
break;
69+
}
70+
}
71+
}
72+
73+
return base.ToArtifact(dataType, dependencies, contextCache);
74+
}
75+
76+
private static void AddDependencies(ICollection<ArtifactDependency> dependencies, IEnumerable<IContentType> contentTypes)
77+
{
78+
foreach (var contentType in contentTypes)
79+
{
80+
dependencies.Add(new ArtifactDependency(contentType.GetUdi(), true, ArtifactDependencyMode.Exist));
81+
}
82+
}
83+
84+
/// <summary>
85+
/// Gets the grid editors used within the grid configuration.
86+
/// </summary>
87+
/// <param name="gridConfigurationItems">The grid configuration items.</param>
88+
/// <returns>
89+
/// The used grid editors (returns all editors if any of the areas allows all editors).
90+
/// </returns>
91+
protected virtual IEnumerable<IGridEditorConfig> GetGridEditors(GridConfigurationItems gridConfigurationItems)
92+
{
93+
foreach (var gridEditor in _gridConfig.EditorsConfig.Editors)
94+
{
95+
if (IsAllowedGridEditor(gridConfigurationItems, gridEditor.Alias))
96+
{
97+
yield return gridEditor;
98+
}
99+
}
100+
}
101+
102+
/// <summary>
103+
/// Determines whether the grid editor alias is allowed in the specified grid configuration.
104+
/// </summary>
105+
/// <param name="gridConfigurationItems">The grid configuration items.</param>
106+
/// <param name="alias">The alias.</param>
107+
/// <returns>
108+
/// <c>true</c> if the grid editor alias is allowed in the specified grid configuration; otherwise, <c>false</c>.
109+
/// </returns>
110+
protected static bool IsAllowedGridEditor(GridConfigurationItems gridConfigurationItems, string alias)
111+
=> gridConfigurationItems.Layouts.Any(x => x.Areas.Any(y => y.AllowAll || y.Allowed.Contains(alias)));
112+
113+
/// <summary>
114+
/// Determines whether the grid editor is the DTGE.
115+
/// </summary>
116+
/// <param name="gridEditor">The grid editor.</param>
117+
/// <returns>
118+
/// <c>true</c> if the grid editor is the DTGE; otherwise, <c>false</c>.
119+
/// </returns>
120+
protected static bool IsDocTypeGridEditor(IGridEditorConfig gridEditor)
121+
=> !string.IsNullOrEmpty(gridEditor.View) && gridEditor.View.Contains("doctypegrideditor");
122+
123+
/// <summary>
124+
/// The grid configuration items.
125+
/// </summary>
126+
protected sealed class GridConfigurationItems
127+
{
128+
/// <summary>
129+
/// Gets or sets the row configurations.
130+
/// </summary>
131+
/// <value>
132+
/// The row configurations.
133+
/// </value>
134+
[JsonProperty("layouts")]
135+
public GridConfigurationLayout[] Layouts { get; set; } = Array.Empty<GridConfigurationLayout>();
136+
}
137+
138+
/// <summary>
139+
/// The grid row configuration.
140+
/// </summary>
141+
protected sealed class GridConfigurationLayout
142+
{
143+
/// <summary>
144+
/// Gets or sets the areas.
145+
/// </summary>
146+
/// <value>
147+
/// The areas.
148+
/// </value>
149+
[JsonProperty("areas")]
150+
public GridConfigurationLayoutArea[] Areas { get; set; } = Array.Empty<GridConfigurationLayoutArea>();
151+
}
152+
153+
/// <summary>
154+
/// The grid row configuration area.
155+
/// </summary>
156+
protected sealed class GridConfigurationLayoutArea
157+
{
158+
/// <summary>
159+
/// Gets or sets a value indicating whether all grid editors are allowed.
160+
/// </summary>
161+
/// <value>
162+
/// <c>true</c> if all grid editors are allowed; otherwise, <c>false</c>.
163+
/// </value>
164+
/// <remarks>
165+
/// Defaults to <c>true</c>.
166+
/// </remarks>
167+
[JsonProperty("allowAll")]
168+
public bool AllowAll { get; set; } = true;
169+
170+
/// <summary>
171+
/// Gets or sets the allowed grid editor aliases.
172+
/// </summary>
173+
/// <value>
174+
/// The allowed grid editor aliases.
175+
/// </value>
176+
[JsonProperty("allowed")]
177+
public string[] Allowed { get; set; } = Array.Empty<string>();
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)