When using ValueConverters, pass along the Content Type - fixes #133

* Add new property HostContentType to make Properties aware of their content type
* Adjust Dummy PropertyType to include the Content Type
* Pass in Content Type from ValueConverter to Deserialization helper through new optional parameter
* Populate the property's HostContentType via RetrieveAdditionalProperties
* Make sure HostContentType isn't persisted

Notes: In some cases, the Umbraco Core looks for pt.ContentType.Alias, which didn't exist previously in our Dummy PropertyType.  This would also potentially cause an error if any PVC's do the same.

To fix we now keep track of the Content Type that the Archetype exists on (determined in the ArchetypeValueConverter), and pass this along to our Dummy PropertyType (and thus to PVCs)

Note we made the hostContentType an optional parameter as there's some cases where we won't know the content type (ie ConvertDbToEditor).  This (shouldn't?) be an issue as these cases should never have a reason to call GetValue.
This commit is contained in:
Tom Fulton
2014-05-27 14:31:23 -06:00
parent 4785c85b84
commit 485c6cb6a1
3 changed files with 11 additions and 5 deletions
@@ -4,6 +4,7 @@ using Archetype.Models;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
namespace Archetype.Extensions
{
@@ -46,7 +47,7 @@ namespace Archetype.Extensions
}
}
internal Models.ArchetypeModel DeserializeJsonToArchetype(string sourceJson, int dataTypeId)
internal Models.ArchetypeModel DeserializeJsonToArchetype(string sourceJson, int dataTypeId, PublishedContentType hostContentType = null)
{
try
{
@@ -56,7 +57,7 @@ namespace Archetype.Extensions
{
// Get list of configured properties and their types and map them to the deserialized archetype model
var preValue = GetArchetypePreValueFromDataTypeId(dataTypeId);
RetrieveAdditionalProperties(ref archetype, preValue);
RetrieveAdditionalProperties(ref archetype, preValue, hostContentType);
}
catch (Exception ex)
{
@@ -110,7 +111,7 @@ namespace Archetype.Extensions
/// </summary>
/// <param name="archetype">The Archetype to add the additional metadata to</param>
/// <param name="preValue">The configuration of the Archetype</param>
private void RetrieveAdditionalProperties(ref Models.ArchetypeModel archetype, ArchetypePreValue preValue)
private void RetrieveAdditionalProperties(ref Models.ArchetypeModel archetype, ArchetypePreValue preValue, PublishedContentType hostContentType = null)
{
foreach (var fieldset in preValue.Fieldsets)
{
@@ -125,6 +126,7 @@ namespace Archetype.Extensions
propertyInst.DataTypeGuid = property.DataTypeGuid.ToString();
propertyInst.DataTypeId = GetDataTypeByGuid(property.DataTypeGuid).Id;
propertyInst.PropertyEditorAlias = property.PropertyEditorAlias;
propertyInst.HostContentType = hostContentType;
}
}
}
@@ -25,6 +25,9 @@ namespace Archetype.Models
[JsonProperty("dataTypeGuid")]
internal string DataTypeGuid { get; set; }
[JsonProperty("hostContentType")]
internal PublishedContentType HostContentType { get; set; }
public T GetValue<T>()
{
@@ -80,7 +83,7 @@ namespace Archetype.Models
private PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias)
{
return new PublishedPropertyType(null,
return new PublishedPropertyType(this.HostContentType,
new PropertyType(new DataTypeDefinition(-1, propertyEditorAlias)
{
Id = dataTypeId
@@ -40,7 +40,8 @@ namespace Archetype.PropertyConverters
return defaultValue;
var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(source.ToString(),
(propertyType != null ? propertyType.DataTypeId : -1));
(propertyType != null ? propertyType.DataTypeId : -1),
(propertyType != null ? propertyType.ContentType : null));
return archetype;
}