Created
November 9, 2016 01:21
-
-
Save tompipe/beaf44f29d416249dfdeaa9d1b24956a to your computer and use it in GitHub Desktop.
Umbraco - Fix Drop Down Pre Value Display In List Views Handler
This file contains hidden or 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
#region | |
using System; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
using Umbraco.Web.Models.ContentEditing; | |
#endregion | |
public class MyUmbracoApplication : UmbracoApplication | |
{ | |
protected override void OnApplicationStarted(object sender, EventArgs e) | |
{ | |
base.OnApplicationStarted(sender, e); | |
GlobalConfiguration.Configuration.MessageHandlers.Add(new FixDropDownPreValueDisplayInListViewsHandler()); | |
} | |
} | |
public class FixDropDownPreValueDisplayInListViewsHandler : DelegatingHandler | |
{ | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
// wait for the request to be processed | |
var response = await base.SendAsync(request, cancellationToken); | |
// ensure we're only processing requests to getchildren - hopefully only listviews | |
if (request?.RequestUri?.AbsolutePath.Equals("/umbraco/backoffice/UmbracoApi/Content/GetChildren") == true) | |
{ | |
// get the response value and cast to type returned by ContentController.GetChildren | |
var content = (ObjectContent)response.Content; | |
var data = content?.Value as PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>>; | |
foreach (var item in data?.Items) | |
foreach (var prop in item?.Properties.Where(p => p?.Editor == "Umbraco.DropDown")) | |
{ | |
// swap the prevalue id to the string using the datatype service | |
var propStr = prop.Value?.ToString(); | |
if (!propStr.IsNullOrWhiteSpace()) | |
prop.Value = ApplicationContext.Current.Services.DataTypeService.GetPreValueAsString(Convert.ToInt32(prop.Value)); | |
} | |
} | |
return response; | |
} | |
} |
This still works in Umbraco 7.14 but with a few amendments.
Line 41: foreach (var prop in item?.Properties.Where(p => p?.Editor == "Umbraco.DropDown.Flexible"))
Line 46: prop.Value = ApplicationContext.Current.Services.DataTypeService.GetPreValueAsString(Convert.ToInt32(((string[])prop.Value)?[0]));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Does this still work, or am I doing something wrong?
I tried to implement this to be able to see drop down prevalues in listview, but the dropdown ID's still shows. Am I missing something?
What I did was to create a ListViewPreValuesExtension.cs file in my Extensions folder and then build the solution. Do I have to do anything else to make it work?
Thanks in advance!