Copied to clipboard

Flag this post as spam?

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


  • McGern 13 posts 44 karma points
    Mar 15, 2024 @ 00:56
    McGern
    0

    Using Value method on IPublishedProperty with Fallback Language

    Hi,

    Using v13.2 and trying to get Fallback.ToDefaultLanguage working on IPublishedProperty element (we are looping through all Properties on an IPublishedContent to deliver a generic json api allowing for nested properties and custom handling that content delivery api won't be able to do)

    GetValue doesn't have this functionality, and the docs https://our.umbraco.com/apidocs/v8/csharp/api/Umbraco.Core.Models.PublishedContent.IPublishedValueFallback.html (for v8 couldn't find anything more recent) states down the bottom:-

    At property level, property.GetValue() does not implement fallback, and one has to get property.Value() or property.Value{T}() to trigger fallback.

    My issue is that this is the required parameters and I have no idea what IPublishedValueFallback is (I've got a grip on the rest)

    requirements for IPublishedProperty Value method When I go to the sourcelink I get this

    public interface IPublishedValueFallback
    {
        [Obsolete("Scheduled for removal in v14")]
    

    I think I am going down a rabbit-hole, surely there must be a better way to get fallback language working?

    Any help would be much appreciated please!

    Thanks, Jarrod

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Mar 15, 2024 @ 22:30
    Marc Goodson
    100

    Hi McGern

    IPublishedValueFallback is an interface that represents the current strategy for getting fallback values... It used to be hidden away, but in the dot net core reimplementation of Umbraco the notion of dependency injection was fully embraced and lots of horrible staticness was removed in the code base...

    ... Alot of the complexity is still hidden, but what you are trying to do: looping through the properties, isn't what people 'usually do', and so a bit of the DI is peeping its head out...

    And that's confusing as you say, what on earth is IPublishedValueFallback and how do I get it and what do I do?

    From what you are describing you are accessing the properties from a custom UmbracoApiController or perhaps a custom service registered with DI, if so, in the constructor for that controller or service you should be able to inject IPublishedValueFallback. Umbraco will then provide the default implementation of the strategy and you can pass it into the Value method as a parameter...

    The nearest I've found to a code sample is it's usage in modelsbuilder classes, have a look at this usage to see if that gives you the gist of how it's used...

    https://docs.umbraco.com/umbraco-cms/v/10.latest-lts/reference/templating/modelsbuilder/understand-and-extend

    Regards

    Marc

  • McGern 13 posts 44 karma points
    Mar 16, 2024 @ 01:30
    McGern
    0

    Thanks Marc. I'll take a look. Really appreciate the time you took to write the reply.

    The reason for the not usual setup is because we are outputting json data to be consumed by an i18n framework in a specific format (less verbose than content delivery api) and are just iterating all properties (including block lists and nested block lists) to include in the json.

    I was wondering if you have the space to answer another question please? I'm really struggling with the fallback language concept and the purpose that it serves in the admin UI when you seem to have to manually code for it? I had somewhat assumed that the internal content fetching mechanisms would handle it automatically - which brings me to my actual question. I'm using the content delivery api (v13.2) for a separate part of the site and the fallback language doesn't pick up at all (i.e. Other language nodes are published with no content) and there is no obvious place to code a fallback because the content fetching is abstracted. I was trying with a vanilla install with a simple setup - when requesting the default language (also set as fallback) it was fine - when requesting a second language (fallback set as the default language in the admin UI) any invariant field values pulled through. Also, variant field values that that were populated appeared in the api - any non-populated fields in the second language appeared as null, not even empty string.

    I'm sure I'm doing something wrong, but I thought I followed the documentation so not sure what I've missed?

    Any help would be much appreciated. Everything else is so close to working.

    Regards, Jarrod

  • McGern 13 posts 44 karma points
    Mar 16, 2024 @ 14:18
    McGern
    0

    Hi Marc,

    Just put the DI for IPublishedValueFallback into the controller - thanks for the excellent explanation BTW, it makes sense now, I didn't understand that was what was needed - and the fallback values are pulling through. I needed to remove static from a couple of methods, crossing fingers that it will be okay still (the deeper side of C# is still elusive, I'm predominantly a front-end developer who gets my hands dirty once in a while).

    Thanks again for the assistance, am really appreciating the community support right now.

    Kind regards, Jarrod

Please Sign in or register to post replies

Write your reply to:

Draft