dtge output to IPublishedContent using a grid property value converter?
hey out there,
we've just dropped dtge into a v8 project we're working on. it works a treat so far however something we're struggling with is writing a type converter to work with our grid output.
at the moment, we've a grid editor that stores content id's in rows. we run that through a type converter that gets content based on those id's and creates a model which is IEnumerable<IPublishedContent> . nice and simple to output on the front end ;)
the trick is now that we've added the dtge we get a more traditional output from the grid i.e. it's a json blob something like this:
so, i gave it a go and got the following exception:
Could not create an instance of type Umbraco.Core.Models.PublishedContent.IPublishedElement. Type is an interface or abstract class and cannot be instantiated. Path 'dtgeContentTypeAlias', line 2, position 25.
i'm defo missing something here!
this is my type converter for the grid, it's skimmed down for brevity but the important bits are there:
public class GridTypeConverter : IPropertyValueConverter
public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
{
var grid = JsonConvert.DeserializeObject<Grid>(source as string);
var myGrid = new MyGrid();
var myRows = new List<MyGridRow>();
foreach (var section in grid.sections)
{
foreach (var row in section.rows)
{
foreach (var area in row.areas)
{
var myRow = new MyGridRow();
var myContent = new List<IPublishedContent>();
foreach (var control in area.controls)
{
int.TryParse(control.value.ToString(), out var controlValueId);
if (controlValueId <= 0)
{
var dtgeContent = JsonConvert.DeserializeObject<IPublishedElement>(control.value.ToString());
continue;
}
var umbracoHelper = Umbraco.Web.Composing.Current.UmbracoHelper;
control.Content = umbracoHelper.Content(controlValueId);
if (control.Content != null)
{
myContent.Add(control.Content);
}
}
if (!myContent.Any())
{
continue;
}
myRow.Content = myContent;
myRows.Add(myRow);
}
}
}
myGrid.Rows = myRows;
return myGrid;
}
var dtgeContent = JsonConvert.DeserializeObject<IPublishedElement>(control.value.ToString());
I would guess that NewtonSoft.JSON will not know where the required implementation of IPublishedElement is so it can't create an instance (and I'm not honestly sure how you'd tell it where the implementation is as presumably there may be multiple implementations available - e.g. in my very simple U8 project I have 4 implementations of IPublishedElement).
Typically you would use something like Ninject to resolve the dependency here, but it's not clear how you'd do that in this case - you'd probably have to set up some kind of mapping based on the document type alias.
Edit: I've just had a look at my project and all of my Element types inherit from PublishedElementModel, so you could try changing that line to create instances of that class - it seems to be the default implementation of the interface.
Jake did you ever get this solved? I am trying the exact same thing, but has ended up with manual mapping of objects. I am interested in the same solution you have requested.
The model item passed into the dictionary is of type 'Our.Umbraco.DocTypeGridEditor.Models.DetachedPublishedElement', but this dictionary requires a model item of type 'My.Custom.Model'
Worked fine when DocTypeGridEditorHelper.ConvertValueToContent returned IPublishedContent
dtge output to IPublishedContent using a grid property value converter?
hey out there,
we've just dropped dtge into a v8 project we're working on. it works a treat so far however something we're struggling with is writing a type converter to work with our grid output.
at the moment, we've a grid editor that stores content id's in rows. we run that through a type converter that gets content based on those id's and creates a model which is
IEnumerable<IPublishedContent>
. nice and simple to output on the front end ;)the trick is now that we've added the dtge we get a more traditional output from the grid i.e. it's a json blob something like this:
so my question is how do i convert the json blob into a piece of IPublishedContent and is that even possible?!
has anyone attempted this before?!
any suggestions are as ever greatly received ;)
is it possible that it's something to do with
DetachedPublishedContent
?Hi Jake,
v8 of DTGE uses IPublishedElement and not IPublishedContent. It is exactly same change as Nested Content.
Please can you try changing your doctype to IPublishedElement and all should work seamless.
We used in v8.1.4 and you say it works like a treat. I love this package as all our client finds it awesome feature.
Cheers,
Shaishav
hi shaishav,
thank you for the reply ;)
just spinning up the code but does that mean i can take the json blob and deserialise it to
IPublishedElement
e.g.?i'll give it a go!
cheers,
jake
Yes please - give a try
hi shaishav,
so, i gave it a go and got the following exception:
i'm defo missing something here!
this is my type converter for the grid, it's skimmed down for brevity but the important bits are there:
{ public bool IsConverter(IPublishedPropertyType propertyType) { return propertyType.EditorAlias == "Umbraco.Grid"; }
}
i'm clearly missing something here!
I think the issue may be this line:
I would guess that NewtonSoft.JSON will not know where the required implementation of IPublishedElement is so it can't create an instance (and I'm not honestly sure how you'd tell it where the implementation is as presumably there may be multiple implementations available - e.g. in my very simple U8 project I have 4 implementations of IPublishedElement).
Typically you would use something like Ninject to resolve the dependency here, but it's not clear how you'd do that in this case - you'd probably have to set up some kind of mapping based on the document type alias.
Edit: I've just had a look at my project and all of my Element types inherit from PublishedElementModel, so you could try changing that line to create instances of that class - it seems to be the default implementation of the interface.
Jake did you ever get this solved? I am trying the exact same thing, but has ended up with manual mapping of objects. I am interested in the same solution you have requested.
Thanks Morten
Also have the same issue...
The model item passed into the dictionary is of type 'Our.Umbraco.DocTypeGridEditor.Models.DetachedPublishedElement', but this dictionary requires a model item of type 'My.Custom.Model'
Worked fine when DocTypeGridEditorHelper.ConvertValueToContent returned IPublishedContent
is working on a reply...