Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jake williamson 207 posts 873 karma points
    Sep 16, 2019 @ 03:49
    jake williamson
    0

    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:

    {{
      "dtgeContentTypeAlias": "blockMarginCommonItem",
      "value": {
        "name": "Common Item",
        "margin": [
          "Small"
        ],
        "hide": "0"
      },
      "id": "68378fe0-1854-2623-9faa-19b2537c28cb"
    }}
    

    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 ;)

  • jake williamson 207 posts 873 karma points
    Sep 16, 2019 @ 03:50
    jake williamson
    0

    is it possible that it's something to do with DetachedPublishedContent?

    enter image description here

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 16, 2019 @ 07:04
    Shaishav Karnani from digitallymedia.com
    1

    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

  • jake williamson 207 posts 873 karma points
    Sep 16, 2019 @ 07:26
    jake williamson
    0

    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.?

    var dtgeContent = JsonConvert.DeserializeObject<IPublishedElement>(control.value.ToString());
    

    i'll give it a go!

    cheers,

    jake

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 16, 2019 @ 09:09
    Shaishav Karnani from digitallymedia.com
    0

    Yes please - give a try

  • jake williamson 207 posts 873 karma points
    Sep 16, 2019 @ 23:26
    jake williamson
    0

    hi shaishav,

    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 bool IsConverter(IPublishedPropertyType propertyType) { return propertyType.EditorAlias == "Umbraco.Grid"; }

    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;
    }
    

    }

    i'm clearly missing something here!

  • Chris Norwood 131 posts 642 karma points
    Oct 17, 2019 @ 21:14
    Chris Norwood
    0

    I think the issue may be this line:

    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.

  • Morten Bork 5 posts 75 karma points
    Apr 13, 2020 @ 21:56
    Morten Bork
    0

    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

  • Tim 67 posts 90 karma points
    Jun 25, 2021 @ 16:01
    Tim
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft