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

Commit 5abed06

Browse files
committed
formatting, and making ModelsBuilder work 🎉
1 parent f3d063b commit 5abed06

File tree

1 file changed

+64
-57
lines changed

1 file changed

+64
-57
lines changed

‎src/Our.Umbraco.DocTypeGridEditor/Helpers/DocTypeGridEditorHelper.cs

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,78 +39,85 @@ public static IPublishedElement ConvertValueToContent(string id, string contentT
3939

4040
private static IPublishedElement ConvertValue(string id, string contentTypeAlias, string dataJson)
4141
{
42-
var contentTypes = GetContentTypesByAlias(contentTypeAlias);
43-
var properties = new List<IPublishedProperty>();
42+
var contentTypes = GetContentTypesByAlias(contentTypeAlias);
43+
var properties = new List<IPublishedProperty>();
4444

45-
// Convert all the properties
46-
var data = JsonConvert.DeserializeObject(dataJson);
47-
var propValues = ((JObject)data).ToObject<Dictionary<string, object>>();
48-
foreach (var jProp in propValues)
49-
{
50-
var propType = contentTypes.PublishedContentType.GetPropertyType(jProp.Key);
51-
if (propType == null)
52-
continue;
45+
// Convert all the properties
46+
var data = JsonConvert.DeserializeObject(dataJson);
47+
var propValues = ((JObject)data).ToObject<Dictionary<string, object>>();
48+
foreach (var jProp in propValues)
49+
{
50+
var propType = contentTypes.PublishedContentType.GetPropertyType(jProp.Key);
51+
if (propType == null)
52+
continue;
5353

5454

55-
/* Because we never store the value in the database, we never run the property editors
56-
* "ConvertEditorToDb" method however the property editors will expect their value to
57-
* be in a "DB" state so to get round this, we run the "ConvertEditorToDb" here before
58-
* we go on to convert the value for the view.
59-
*/
60-
Current.PropertyEditors.TryGet(propType.EditorAlias, out var propEditor);
61-
var propPreValues = GetPreValuesCollectionByDataTypeId(propType.DataType.Id);
55+
/* Because we never store the value in the database, we never run the property editors
56+
* "ConvertEditorToDb" method however the property editors will expect their value to
57+
* be in a "DB" state so to get round this, we run the "ConvertEditorToDb" here before
58+
* we go on to convert the value for the view.
59+
*/
60+
Current.PropertyEditors.TryGet(propType.EditorAlias, out var propEditor);
61+
var propPreValues = GetPreValuesCollectionByDataTypeId(propType.DataType.Id);
6262

63-
var contentPropData = new ContentPropertyData(jProp.Value, propPreValues);
63+
var contentPropData = new ContentPropertyData(jProp.Value, propPreValues);
6464

65-
var newValue = propEditor.GetValueEditor().FromEditor(contentPropData, jProp.Value);
65+
var newValue = propEditor.GetValueEditor().FromEditor(contentPropData, jProp.Value);
6666

67-
/* Now that we have the DB stored value, we actually need to then convert it into its
68-
* XML serialized state as expected by the published property by calling ConvertDbToString
69-
*/
70-
var propType2 = contentTypes.ContentType.CompositionPropertyTypes.First(x => x.PropertyEditorAlias.InvariantEquals(propType.DataType.EditorAlias));
71-
72-
// TODO: Do we still need this weird tag conversion?
73-
//Property prop2 = null;
74-
//try
75-
//{
76-
// /* HACK: [LK:2016-04-01] When using the "Umbraco.Tags" property-editor, the converted DB value does
77-
// * not match the datatypes underlying db-column type. So it throws a "Type validation failed" exception.
78-
// * We feel that the Umbraco core isn't handling the Tags value correctly, as it should be the responsiblity
79-
// * of the "Umbraco.Tags" property-editor to handle the value conversion into the correct type.
80-
// * See: http://issues.umbraco.org/issue/U4-8279
81-
// */
82-
// prop2 = new Property(propType2.Id, newValue);
83-
//}
84-
//catch (Exception ex)
85-
//{
86-
// Current.Logger.Error<DocTypeGridEditorHelper>("[DocTypeGridEditor] Error creating Property object.", ex);
87-
//}
88-
89-
//if (prop2 != null)
90-
//{
67+
/* Now that we have the DB stored value, we actually need to then convert it into its
68+
* XML serialized state as expected by the published property by calling ConvertDbToString
69+
*/
70+
var propType2 = contentTypes.ContentType.CompositionPropertyTypes.First(x => x.PropertyEditorAlias.InvariantEquals(propType.DataType.EditorAlias));
71+
72+
Property prop2 = null;
73+
try
74+
{
75+
/* HACK: [LK:2016-04-01] When using the "Umbraco.Tags" property-editor, the converted DB value does
76+
* not match the datatypes underlying db-column type. So it throws a "Type validation failed" exception.
77+
* We feel that the Umbraco core isn't handling the Tags value correctly, as it should be the responsiblity
78+
* of the "Umbraco.Tags" property-editor to handle the value conversion into the correct type.
79+
* See: http://issues.umbraco.org/issue/U4-8279
80+
*/
81+
prop2 = new Property(propType2);
82+
prop2.SetValue(newValue);
83+
}
84+
catch (Exception ex)
85+
{
86+
Current.Logger.Error(typeof(DocTypeGridEditorHelper), ex, "[DocTypeGridEditor] Error creating Property object.");
87+
}
88+
89+
if (prop2 != null)
90+
{
9191
var newValue2 = propEditor.GetValueEditor().ConvertDbToString(propType2, newValue, Current.Services.DataTypeService);
9292

9393
properties.Add(new DetachedPublishedProperty(propType, newValue2));
94-
//}
9594
}
95+
}
96+
97+
// Manually parse out the special properties
98+
propValues.TryGetValue("name", out object nameObj);
99+
Guid.TryParse(id, out Guid key);
96100

97-
// Manually parse out the special properties
98-
propValues.TryGetValue("name", out object nameObj);
99-
Guid.TryParse(id, out Guid key);
101+
// Get the current request node we are embedded in
100102

101-
// Get the current request node we are embedded in
103+
var pcr = Current.UmbracoContext.PublishedRequest;
104+
var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;
102105

103-
var pcr = Current.UmbracoContext.PublishedRequest;
104-
var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;
106+
// Create the model based on our implementation of IPublishedElement
107+
IPublishedElement content = new DetachedPublishedElement(
108+
key,
109+
contentTypes.PublishedContentType,
110+
properties.ToArray());
105111

106-
// Create the model based on our implementation of IPublishedContent
107-
// TODO: FIXME!
108-
var content = new DetachedPublishedElement(
109-
key,
110-
contentTypes.PublishedContentType,
111-
properties.ToArray());
112+
var publishedModelFactory = Current.Factory.GetInstance<IPublishedModelFactory>();
113+
114+
if (publishedModelFactory != null)
115+
{
116+
// Let the current model factory create a typed model to wrap our model
117+
content = publishedModelFactory.CreateModel(content);
118+
}
112119

113-
return content;
120+
return content;
114121

115122
}
116123

0 commit comments

Comments
 (0)