Copied to clipboard

Flag this post as spam?

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


  • Mikkel.S 33 posts 167 karma points
    Dec 15, 2022 @ 06:59
    Mikkel.S
    0

    Umbraco 11 - Block Grid Editor and exposing data through an API

    Hey all!

    I have been tinkering a bit with a REST API which exposes node data. And as such I thought it would make sense to try and expose some more complex data elements such as the Block Grid Editor and its items.

    I started with installing the Umbraco Demo Grid Items, and out of those, getting a Headline, RTE and an Image is fairly straight forward. But the TwoColumn item is empty and has no properties. How does that work in terms of fetching that data through either the ContentService or the UmbracoContextAccessor?

    In terms of getting classic Umbraco nodes out, I am struggling with the culture-based queries, i.e.:

    PHome? root = ctx.Content?.GetAtRoot(culture).FirstOrDefault() as PHome;
    PContentPage? child = root?.Children(culture).FirstOrDefault(x => x.IsDocumentType(docTypeAlias) && x.UrlSegment == urlSegment) as PContentPage;
    

    GetAtRoot(string? culture) and Children(string? culture) doesn't seem to actually do anything. The culture I'm specifying is based on the Languages tab in Umbraco, which would be either "en" or "da". Based on those queries I would somehow assume getting only the culture-specific versions out. Is that wrong of me to assume? And what it actually does is: (Pseudo code)

    _nodeRepository.GetAll().Where(x => x.cultures.Contains(culture))
    

    Because if that's the case, then how do I fetch a language specific node or at least it's fields?

    Lastly, let's assume there is no "fetcher" for getting a language-specific node. The whole GetValue

    Name = child.Name(culture) ?? "",
    Title = child.Properties.FirstOrDefault(x => x.Alias == "title")?.Value<string>(null, culture) ?? "",
    Lead = child.Properties.FirstOrDefault(x => x.Alias == "lead")?.Value<string>(null, culture) ?? "",
    

    But specifically the Block Grid Items are not:

    switch (block.Content.ContentType.Alias)
                        {
                            case UmbBlockGridDemoHeadlineBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoHeadlineBlock.ModelTypeAlias;
                                blok.Properties.Add("Headline", block.Content.Properties.FirstOrDefault(x => x.Alias == "headline")?.Value<string>(null, culture) ?? "");
                                break;
                            case UmbBlockGridDemoImageBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoImageBlock.ModelTypeAlias;
                                var image = block.Content.Value<MediaWithCrops>("image");
                                blok.Properties.Add("Image", image != null ? image.Url(null, UrlMode.Absolute) : "");
                                break;
                            case UmbBlockGridDemoRichTextBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoRichTextBlock.ModelTypeAlias;
                                blok.Properties.Add("Richtext", block.Content.Properties.FirstOrDefault(x => x.Alias == "richText")?.Value<string>(null, culture) ?? "");
                                break;
                            case UmbBlockGridDemoTwoColumnLayoutBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoTwoColumnLayoutBlock.ModelTypeAlias;
                                blok.Properties.Add("Richtext", block.Content.Value<dynamic>("richText", culture) ?? "");
                                break;
                            default:
                                break;
                        }
    

    I know the Block Grid Editor is fairly new, and this might now have been a priority. But I would like to know, if I'm doing something wrong?

    For reference, here's my super-not-structured controller-method for a very small POC:

    public IActionResult ReturnNode(string docTypeAlias, string urlSegment)
        {
            if (string.IsNullOrWhiteSpace(docTypeAlias) || string.IsNullOrWhiteSpace(urlSegment))
                return NoContent();
            if (_umbracoContextAccessor.TryGetUmbracoContext(out var ctx))
            {
                var culture = Request.Headers["cultureCode"].Any() ? Request.Headers["cultureCode"].ToString() : string.Empty;
    
    
    
                PHome? root = ctx.Content?.GetAtRoot(culture).FirstOrDefault() as PHome;
                PContentPage? child = root?.Children(culture).FirstOrDefault(x => x.IsDocumentType(docTypeAlias) && x.UrlSegment == urlSegment) as PContentPage;
    
                var blocks = child?.MyBlockGrid;
                var blocksResponseList = new List<Block>();
                var blocksResponse = new Dictionary<string, dynamic>();
                if (blocks != null)
                    foreach (var block in blocks)
                    {
                        var blok = new Block();
                        switch (block.Content.ContentType.Alias)
                        {
                            case UmbBlockGridDemoHeadlineBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoHeadlineBlock.ModelTypeAlias;
                                blok.Properties.Add("Headline", block.Content.Properties.FirstOrDefault(x => x.Alias == "headline")?.Value<string>(null, culture) ?? "");
                                break;
                            case UmbBlockGridDemoImageBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoImageBlock.ModelTypeAlias;
                                var image = block.Content.Value<MediaWithCrops>("image");
                                blok.Properties.Add("Image", image != null ? image.Url(null, UrlMode.Absolute) : "");
                                break;
                            case UmbBlockGridDemoRichTextBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoRichTextBlock.ModelTypeAlias;
                                blok.Properties.Add("Richtext", block.Content.Properties.FirstOrDefault(x => x.Alias == "richText")?.Value<string>(null, culture) ?? "");
                                break;
                            case UmbBlockGridDemoTwoColumnLayoutBlock.ModelTypeAlias:
                                blok.BlockTitle = UmbBlockGridDemoTwoColumnLayoutBlock.ModelTypeAlias;
                                blok.Properties.Add("Richtext", block.Content.Value<dynamic>("richText", culture) ?? "");
                                break;
                            default:
                                break;
                        }
                        if (!string.IsNullOrEmpty(blok.BlockTitle))
                            blocksResponseList.Add(blok);
                    }
    
                return Ok(new PageResponse
                {
                    Name = child.Name(culture) ?? "",
                    Title = child.Properties.FirstOrDefault(x => x.Alias == "title")?.Value<string>(null, culture) ?? "",
                    Lead = child.Properties.FirstOrDefault(x => x.Alias == "lead")?.Value<string>(null, culture) ?? "",
                    Blocks = blocksResponseList
                });
            }
    
            return NoContent();
        }
    

    Thank you very much in advance! /Mikkel.

Please Sign in or register to post replies

Write your reply to:

Draft