Jeavon / Umbraco-Core-Property-Value-Converters

19 stars 13 forks source link

MNTP returns IPublishedContent #24

Open bjarnef opened 6 years ago

bjarnef commented 6 years ago

In the docs it says that MNTP (Multinode Tree Picker) returns IEnumerable<IPublishedContent> https://github.com/Jeavon/Umbraco-Core-Property-Value-Converters/blob/v3/Docs/MultiNodeTreePicker.md

However I expirience it returns IPublishedContent, but it might be because the datatype instance is limited to select one item? image

If I used one of the following, it sometimes returned null even a node was picked.

var pageTest1 = item.GetValue<IEnumerable<IPublishedContent>>("linkToPage");
var pageTest2 = (item.GetValue<IEnumerable<IPublishedContent>>("linkToPage") ?? Enumerable.Empty<IPublishedContent>()).FirstOrDefault();

But it is a bit strange, because in some of the items (LeBlender), it works with cast to IEnumerable<IPublishedContent>

var pageTest1 = item.GetValue<IEnumerable<IPublishedContent>>("linkToPage");
var pageTest2 = item.GetValue<IPublishedContent>("linkToPage");

However for a country node, if I instead cast to IPublishedContent it does work. image

We have also ModelsBuilder enabled and use Our.Umbraco.CoreValueConverters v3.1.3 on Umbraco v7.5.8

bjarnef commented 6 years ago

My original code used

var page = (item.GetValue<IEnumerable<IPublishedContent>>("linkToPage") ?? Enumerable.Empty<IPublishedContent>()).FirstOrDefault();

It did work for some items, but was sometimes returning null.

If I change my code to the following it seems to work.

var linkToPage1 = item.GetValue<IEnumerable<IPublishedContent>>("linkToPage");
var linkToPage2 = item.GetValue<IPublishedContent>("linkToPage");

var page = (linkToPage1 ?? linkToPage2.AsEnumerableOfOne()).FirstOrDefault()
Jeavon commented 6 years ago

Hi @bjarnef

That's really odd (and wrong), I would guess it has something to do what's going on inside GetValue, I'm guessing this method is from LeBlender?

Jeavon

Jeavon commented 6 years ago

I found it and I'm not sure why it does this and I'm guessing that's what's causing your issues....

https://github.com/Lecoati/LeBlender/blob/e11905f192a1cb733ec012404998ff74f0b56ca5/Src/Lecoati.LeBlender.Extension/Models/LeBlenderPropertyModel.cs#L49

bjarnef commented 6 years ago

Hi @Jeavon

Yes it is a bit strange, it does seems to work for some of the LeBlender items. For now I solved it by using the following:

var linkToPage1 = item.GetValue<IEnumerable<IPublishedContent>>("linkToPage");
var linkToPage2 = item.GetValue<IPublishedContent>("linkToPage");

//var page = (item.GetValue<IEnumerable<IPublishedContent>>("linkToPage") ?? Enumerable.Empty<IPublishedContent>()).FirstOrDefault();
 // fix issue with property value converter sometimes returns IPublishedContent, sometimes IEnumerable<IPublishedContent>
var page = (linkToPage1 ?? linkToPage2.AsEnumerableOfOne()).FirstOrDefault();

I have also had some issues in some of the (LeBlender) grid editors, when casting an json object to a string or the object/model directly, e.g. for LinkPicker package. https://our.umbraco.org/projects/backoffice-extensions/link-picker/

//var link = selector.GetValue<string>("link");
//var link = selector.GetValue<LinkPickerModel>("link");
var link = selector.GetRawValue("link");

LinkPickerModel linkPage = null;

if (link != null)
{
    linkPage = JsonConvert.DeserializeObject<LinkPickerModel>(link);
}

but I don't have these issues in projects without property value converter package. The properties linkToPage and link are both using LinkPicker as datatype.

Jeavon commented 6 years ago

@bjarnef it's because of that code within the GetValue method of LeBlender, it is sometimes returning "Source" and sometimes returning "Object", it should always return "Object". I can't think of reason why this would be like this perhaps @agiraud might know...

bjarnef commented 6 years ago

@Jeavon maybe @soreng know more about this?

Jeavon commented 6 years ago

@bjarnef did you create this issue on LeBlender somewhere?

bjarnef commented 6 years ago

@Jeavon I think it is related to this issue https://github.com/Lecoati/LeBlender/issues/80 although not exact same functionally, but an issue how the value is returned from LeBlender when using core property value converters mainly an issue with pickers.