Copied to clipboard

Flag this post as spam?

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


  • Dave Jonker 22 posts 72 karma points
    Sep 10, 2020 @ 09:33
    Dave Jonker
    0

    Problem with IPublishedElement in different cultures

    Hi all,

    I have a very strange issue in Umbraco 8.6.4. I have a class that contains an IPublishedElement and a custom enum for our culture:

    public class CultPublishedElement {
      public IPublishedElement element { get; set; }
      public enCulture culture { get; set; }
    }
    

    Then I call this in my method (in a UmbracoApiController):

    List<CultPublishedElement> cultureSpecificQuestionMarkers = new List<CultPublishedElement>();
    int cultureId = (int)cult;
    string cultureCode = cult.ToString().Replace("_", "-");
    IPublishedContent tmpGame = Umbraco.Content(game.Id);
    List<IPublishedElement> tmpElements = tmpGame.Value<IEnumerable<IPublishedElement>>("fldSpecificQuestions", culture: cultureCode).ToList();
    foreach (IPublishedElement tmpElement in tmpElements) {
      cultureSpecificQuestionMarkers.Add(
        new CultPublishedElement {
          element = tmpElement,
          culture = cult,
        }
      );
    }
    

    All content is fetched, but when I check a property called 'question' in my IPublishedElement in English first, all other 'questions' are also English. When I first check a 'question' in French, all 'questions' are in French.

    So whatever culture I use as the first culture "overrides" all other cultures.

    I also tried to fetch the records from another class to see if that helps, but no luck. It seems that the IPublishedElement uses extensive caching or have a reference to each other.

    Does anyone have a solution for this problem?

    Thanks, Dave

  • Dave Jonker 22 posts 72 karma points
    Oct 20, 2020 @ 06:54
    Dave Jonker
    0

    Really? No one? At least i'm not the only one that's confussed about this... :-)

  • Remko 118 posts 283 karma points
    Oct 20, 2020 @ 08:32
    Remko
    0

    Hi Dave,

    What do you mean bij "check a question"

    You fill cultureSpecificQuestionMarkers with IPublishedElements from 1 culture, right?

    And you get the IPublishedElement by adding culture as parameter, but when you get properties from the element you should provide cultureCode parameter as well:

    For instance:

     string question = tmpElement.Value<string>("question", culture: cultureCode);
    

    Is this what you do?

    And may I ask: what are you trying to achieve with your code? What's the purpose of the list you are generating?

  • Dave Jonker 22 posts 72 karma points
    Oct 20, 2020 @ 10:37
    Dave Jonker
    0

    Hi Remko,

    'What do you mean bij "check a question"'

    I I mean by inspecting the object in a watch in Visual Studio.

    'You fill cultureSpecificQuestionMarkers with IPublishedElements from 1 culture, right?'

    That's right... I have a multiculti IPublishedContent which contains a non-multiculti NestedContent field.

    "And you get the IPublishedElement by adding culture as parameter, but when you get properties from the element you should provide cultureCode parameter as well: For instance: string question = tmpElement.Value

    That's exactly what I do.

    "And may I ask: what are you trying to achieve with your code? What's the purpose of the list you are generating?"

    My DocType is a game with geolocations. The geolocations are non-multiculti nested content items. That's where things go wrong.

    Thanks in advance!

  • Anzal 11 posts 81 karma points
    Oct 20, 2020 @ 13:10
    Anzal
    0

    Hi Dave, Let me say this to you, actually values in nested content are independent within culture. That means if you add 5 items in french culture and 3 items in English culture and try to fetch the items by code

    tmpGame.Value<IEnumerable<IPublishedElement>>("fldSpecificQuestions", culture: cultureCode)
    

    This will only populate the results within the specified culture. So if English is passed as parameter you will only get English results for instance only English questions will be returned.

    If you want to get the results from each cultures. Just fetch the cultures from the node and perform a loop. You can fetch culture of the node by following

    var cultures = Umbraco.Content(game.Id).Cultures;
    

    And also if you didn't get any results try to pass a fall back parameter , it might pull the results.

    List<IPublishedElement> tmpElements = tmpGame.Value<IEnumerable<IPublishedElement>>("fldSpecificQuestions", culture: cultureCode,fallback: Fallback.ToDefaultValue, defaultValue: null).ToList();
    
  • Dave Jonker 22 posts 72 karma points
    Oct 20, 2020 @ 15:24
    Dave Jonker
    0

    I might have found the problem...

    This is my IPublishedContent "Game 1":

    Game 1 (NL)
    - fldTitle: (Textfield - Varying by culture)
    - fldSpecificQuestions: (Nested Content - Varying by culture)
      - fldLocation: (Geolocation)
      - fldQuestions: (Nested Content - Varying by culture)
        - fldQuestion: (Textfield - Varying by culture)
        - fldAnswers: (Nested Content - Varying by culture)
          - fldAnswer: (Textfield)
          - fldScore: (numeric)
    Game 1 (EN)
    - fldTitle: (Textfield - Varying by culture)
    - fldSpecificQuestions: (Nested Content - Varying by culture)
      - fldLocation: (Geolocation)
      - fldQuestions: (Nested Content - Varying by culture)
        - fldQuestion: (Textfield - Varying by culture)
        - fldAnswers: (Nested Content - Varying by culture)
          - fldAnswer: (Textfield)
          - fldScore: (numeric)
    

    So my DocType contains Nested content which is Varying by culture, which contains Nested content which is Varying by culture, which contains Nested content which is Varying by culture. I think something gets screwed with that. And a Nested content which is Varying by culture within a Nested content which is Varying by culture is totally unnecessary, right?

  • Bo Jacobsen 606 posts 2404 karma points
    Oct 20, 2020 @ 16:15
    Bo Jacobsen
    100

    Hi Dave.

    Yes, it should look like this for it to work properly.

    Game 1 (NL)
    - fldTitle: (Textfield - Varying by culture)
    - fldSpecificQuestions: (Nested Content - Varying by culture)
      - fldLocation: (Geolocation)
      - fldQuestions: (Nested Content)
        - fldQuestion: (Textfield)
        - fldAnswers: (Nested Content)
          - fldAnswer: (Textfield)
          - fldScore: (numeric)
    Game 1 (EN)
    - fldTitle: (Textfield - Varying by culture)
    - fldSpecificQuestions: (Nested Content - Varying by culture)
      - fldLocation: (Geolocation)
      - fldQuestions: (Nested Content)
        - fldQuestion: (Textfield)
        - fldAnswers: (Nested Content)
          - fldAnswer: (Textfield)
          - fldScore: (numeric)
    
  • Dave Jonker 22 posts 72 karma points
    Oct 20, 2020 @ 17:48
    Dave Jonker
    0

    Thank everybody! I completely overlooked the Varying by culture withing the Varying by culture withing the Varying by culture:-)

Please Sign in or register to post replies

Write your reply to:

Draft