|
| 1 | +using Newtonsoft.Json; |
| 2 | +using Newtonsoft.Json.Linq; |
| 3 | +using Umbraco.Cms.Core; |
| 4 | +using Umbraco.Cms.Core.Models; |
| 5 | +using Umbraco.Cms.Core.PropertyEditors; |
| 6 | +using Umbraco.Cms.Core.Serialization; |
| 7 | +using Umbraco.Cms.Core.Services; |
| 8 | +using Umbraco.Cms.Core.Strings; |
| 9 | +using Umbraco.Cms.Tests.Common.Builders; |
| 10 | +using Umbraco.Cms.Tests.Common.Builders.Extensions; |
| 11 | +using Umbraco.Cms.Tests.Integration.TestServerTest; |
| 12 | +using Umbraco.Cms.Web.BackOffice.Controllers; |
| 13 | +using Umbraco.Cms.Web.Common.Formatters; |
| 14 | +using Umbraco.Extensions; |
| 15 | + |
| 16 | +namespace Our.Umbraco.Test.Examples.DataEditors; |
| 17 | + |
| 18 | +public class DataEditor_Via_Hosted_Controller : UmbracoTestServerTestBase |
| 19 | +{ |
| 20 | + [Test] |
| 21 | + public async Task Creates_Json_Model_For_New_Document() |
| 22 | + { |
| 23 | + var url = PrepareApiControllerUrl<ContentController>(x => x.GetEmpty("myDoctype", -1)); |
| 24 | + |
| 25 | + var response = await Client.GetAsync(url); |
| 26 | + |
| 27 | + var json = await response.Content.ReadAsStringAsync(); |
| 28 | + json = json.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix); |
| 29 | + var model = JObject.Parse(json); |
| 30 | + |
| 31 | + var property = model.SelectToken("variants[0].tabs[0].properties[0]")!; |
| 32 | + |
| 33 | + await VerifyJson(property.ToString(Formatting.Indented)); |
| 34 | + } |
| 35 | + |
| 36 | + [SetUp] |
| 37 | + public void SetupSchema() |
| 38 | + { |
| 39 | + var dataType = new DataTypeBuilder() |
| 40 | + .WithoutIdentity() |
| 41 | + .WithKey(new Guid("87c4db0b-4cee-4c49-9668-7607bf93fdb2")) |
| 42 | + .WithDatabaseType(ValueStorageType.Ntext) |
| 43 | + .WithName("My editor") |
| 44 | + .AddEditor() |
| 45 | + .WithAlias("my.editor") |
| 46 | + .AddExplicitValueEditorBuilder() |
| 47 | + .WithValueType(ValueTypes.Json) |
| 48 | + .Done() |
| 49 | + .Done() |
| 50 | + .Build(); |
| 51 | + |
| 52 | + GetRequiredService<IDataTypeService>().Save(dataType); |
| 53 | + |
| 54 | + var contentType = new ContentTypeBuilder() |
| 55 | + .WithoutIdentity() |
| 56 | + .WithName("My doctype") |
| 57 | + .WithAlias("myDoctype") |
| 58 | + .AddPropertyType() |
| 59 | + .WithAlias("my.property") |
| 60 | + .WithName("My property") |
| 61 | + .WithPropertyEditorAlias("my.editor") |
| 62 | + .WithDataTypeId(dataType.Id) |
| 63 | + .Done() |
| 64 | + .Build(); |
| 65 | + |
| 66 | + GetRequiredService<IContentTypeService>().Save(contentType); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +public class MyEditor : IDataEditor |
| 71 | +{ |
| 72 | + public const string PreviewPath = "/App_Plugins/MyEditor/editor.html"; |
| 73 | + |
| 74 | + private readonly ILocalizedTextService localizedTextService; |
| 75 | + private readonly IShortStringHelper shortStringHelper; |
| 76 | + private readonly IJsonSerializer jsonSerializer; |
| 77 | + |
| 78 | + public MyEditor( |
| 79 | + ILocalizedTextService localizedTextService, |
| 80 | + IShortStringHelper shortStringHelper, |
| 81 | + IJsonSerializer jsonSerializer |
| 82 | + ) |
| 83 | + { |
| 84 | + this.localizedTextService = localizedTextService; |
| 85 | + this.shortStringHelper = shortStringHelper; |
| 86 | + this.jsonSerializer = jsonSerializer; |
| 87 | + } |
| 88 | + |
| 89 | + public IDataValueEditor GetValueEditor() |
| 90 | + { |
| 91 | + return GetValueEditor(null); |
| 92 | + } |
| 93 | + |
| 94 | + public IDataValueEditor GetValueEditor(object? configuration) |
| 95 | + { |
| 96 | + return new MyDataValueEditor( |
| 97 | + localizedTextService, |
| 98 | + shortStringHelper, |
| 99 | + jsonSerializer |
| 100 | + ) |
| 101 | + { |
| 102 | + ValueType = ValueTypes.Json, |
| 103 | + View = PreviewPath |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + public IConfigurationEditor GetConfigurationEditor() |
| 108 | + { |
| 109 | + return new ConfigurationEditor(); |
| 110 | + } |
| 111 | + |
| 112 | + public string Alias => "my.editor"; |
| 113 | + public string Name => "My editor"; |
| 114 | + public EditorType Type => EditorType.PropertyValue; |
| 115 | + public string Icon => "icon-user"; |
| 116 | + public string Group => Constants.PropertyEditors.Groups.Common; |
| 117 | + public bool IsDeprecated => false; |
| 118 | + public IDictionary<string, object>? DefaultConfiguration => new Dictionary<string, object>(); |
| 119 | + public IPropertyIndexValueFactory PropertyIndexValueFactory => new DefaultPropertyIndexValueFactory(); |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +public class MyDataValueEditor : DataValueEditor |
| 124 | +{ |
| 125 | + public MyDataValueEditor(ILocalizedTextService localizedTextService, IShortStringHelper shortStringHelper, IJsonSerializer jsonSerializer) |
| 126 | + : base(localizedTextService, shortStringHelper, jsonSerializer) |
| 127 | + { |
| 128 | + } |
| 129 | + |
| 130 | + public override object? ToEditor(IProperty property, string? culture = null, string? segment = null) |
| 131 | + { |
| 132 | + var value = property.GetValue(culture, segment) as string; |
| 133 | + var jObj = JObject.Parse(value ?? "{}"); |
| 134 | + if (!jObj.ContainsKey("dataForBackoffice")) |
| 135 | + { |
| 136 | + jObj["dataForBackoffice"] = "Some extra data for the view"; |
| 137 | + } |
| 138 | + |
| 139 | + return jObj; |
| 140 | + } |
| 141 | +} |
| 142 | + |
0 commit comments