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; | |
} | |
} |
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]));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 isPagedResult<MemberBasic>
instead ofPagedResult<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?