-
-
Save tompipe/beaf44f29d416249dfdeaa9d1b24956a to your computer and use it in GitHub Desktop.
#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; | |
} | |
} |
I followed this suggestion and it works fine.
But I found out it will not work for member lists so I added a switch in the code.
I switch on type detected on content?.Value
because for members the result is PagedResult<MemberBasic>
instead of PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>>
Code is triggered fine and conversion is performed. I can verify that prop.Value now has the desired display text.
BUT the strange part is that for members this does NOT affect the presented value!!!!
For members I still get the number instead of the text that I know I have replaced it with.
What happens here? Is there some changed event loop which means the member list in fact doesn't care of the value I have specified?
Any suggestions?
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!
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]));
Thanks for the really handy code. I discovered something you might find useful: you need to wrap the outer foreach loop with a check for null. Otherwise it throws an exception when viewing the recycle bin.