Last active
April 19, 2021 13:21
-
-
Save leekelleher/ba6e065044d33ac60fe5400cf5aa4cf8 to your computer and use it in GitHub Desktop.
Umbraco Forms - to populate the prevalues with a data-source from a Contentment data-editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright © 2021 Lee Kelleher. | |
* This Source Code Form is subject to the terms of the Mozilla Public | |
* License, v. 2.0. If a copy of the MPL was not distributed with this | |
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
using Umbraco.Community.Contentment.DataEditors; | |
using Umbraco.Core.Services; | |
using Umbraco.Forms.Core.Models; | |
using Umbraco.Forms.Core.Providers.PreValues; | |
namespace Umbraco.Community.Contentment.Forms.PreValueSources | |
{ | |
internal sealed class ContentmentDataSourcePreValueSource : UmbracoPreValuesReadOnly | |
{ | |
private readonly ConfigurationEditorUtility _configurationEditorUtility; | |
private readonly IDataTypeService _dataTypeService; | |
private readonly Dictionary<string, string> _dataSourceKeys; | |
public ContentmentDataSourcePreValueSource(ConfigurationEditorUtility configurationEditorUtility, IDataTypeService dataTypeService) | |
{ | |
_configurationEditorUtility = configurationEditorUtility; | |
_dataTypeService = dataTypeService; | |
_dataSourceKeys = new Dictionary<string, string>() | |
{ | |
{ "DataList", "dataSource" }, | |
{ "TextInput", "items" }, | |
}; | |
Id = new Guid("44578A69-9F9D-443F-8DC7-2512B0877F98"); | |
Name = "[Contentment] Data Source"; | |
Description = "Populates the prevalues with a data-source from a Contentment data-editor."; | |
} | |
public override List<PreValue> GetPreValues(Field field, Form form) | |
{ | |
if (int.TryParse(DataTypeId, out var dataTypeId) == true && dataTypeId > 0) | |
{ | |
var dataType = _dataTypeService.GetDataType(dataTypeId); | |
if (dataType != null && _dataSourceKeys.TryGetValue(dataType.EditorAlias, out var dataSourceKey) == true) | |
{ | |
if (dataType.Configuration is Dictionary<string, object> config && | |
config.TryGetValue(dataSourceKey, out var obj1) == true && | |
obj1 is JArray array1 && | |
array1.Count > 0 && | |
array1[0] is JObject item1) | |
{ | |
var source = _configurationEditorUtility.GetConfigurationEditor<IDataListSource>(item1.Value<string>("key")); | |
if (source != null) | |
{ | |
var sourceConfig = item1["value"].ToObject<Dictionary<string, object>>(); | |
var items = source?.GetItems(sourceConfig); | |
if (items?.Any() == true) | |
{ | |
return items.Select(x => new PreValue { Id = x.Value, Value = x.Name, }).ToList(); | |
} | |
} | |
} | |
} | |
} | |
// Fallback on the base method from `UmbracoPreValuesReadOnly`. ¯\_(ツ)_/¯ | |
return base.GetPreValues(field, form); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment