Enrich datatypes with default prevalues

Datatypes can have default prevalues, that are not saved as part of the
config and thus need to be added when loading the datatype - e.g. the
"focalPoint" prevalue for the image cropper datatype.
This commit is contained in:
kjac
2014-05-29 11:14:45 +02:00
parent 9fe756bacb
commit 84acd43ccf
@@ -8,6 +8,9 @@ using Umbraco.Core.Models;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Mvc;
using Umbraco.Web.Editors;
using Umbraco.Core.PropertyEditors;
using Archetype.Extensions;
using Newtonsoft.Json;
namespace Archetype.Api
{
@@ -35,8 +38,32 @@ namespace Archetype.Api
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
var dataTypeDisplay = Mapper.Map<IDataTypeDefinition, DataTypeDisplay>(dataType);
return new { selectedEditor = dataTypeDisplay.SelectedEditor, preValues = dataTypeDisplay.PreValues };
// enrich the prevalues with any missing default prevalues for the datatype (e.g. image cropper "focalPoint# prevalue)
var preValues = dataTypeDisplay.PreValues.ToList();
if(string.IsNullOrEmpty(dataType.PropertyEditorAlias) == false)
{
// fetch the default prevalues for the datatype
var propEditor = PropertyEditorResolver.Current.GetByAlias(dataType.PropertyEditorAlias);
if(propEditor != null && propEditor.DefaultPreValues != null && propEditor.DefaultPreValues.Any())
{
// add any missing prevalues
foreach(var missingPreValue in propEditor.DefaultPreValues.Where(p => preValues.Any(pre => pre.Key == p.Key) == false))
{
var value = missingPreValue.Value;
// if the prevalue is a JSON object, deserialize it
if(value != null && value.ToString().DetectIsJson())
{
value = JsonConvert.DeserializeObject(value.ToString());
}
preValues.Add(new PreValueFieldDisplay { Key = missingPreValue.Key, Value = value });
}
}
}
return new { selectedEditor = dataTypeDisplay.SelectedEditor, preValues = preValues };
}
}
}