Skip to content

Instantly share code, notes, and snippets.

@tompipe
Created November 9, 2016 01:21
Show Gist options
  • Save tompipe/beaf44f29d416249dfdeaa9d1b24956a to your computer and use it in GitHub Desktop.
Save tompipe/beaf44f29d416249dfdeaa9d1b24956a to your computer and use it in GitHub Desktop.
Umbraco - Fix Drop Down Pre Value Display In List Views Handler
#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;
}
}
@mokonta
Copy link

mokonta commented Oct 18, 2019

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