Copied to clipboard

Flag this post as spam?

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


  • Aleksandr Šmailov 2 posts 73 karma points
    Oct 03, 2019 @ 04:43
    Aleksandr Šmailov
    1

    How to publish only non-default language variant content?

    I'm using Umbraco 8.1.3 (same thing with 8.1.5).

    Steps:

    • Create Document Type
    • Enable vary by culture
    • Add 1 invariant property
    • Add 1 variant property (vary by culture enabled)
    • Set a default language (for example: en-US - English US)
    • Add additional language (for example: da - Danish)
    • Create content using newly created document type. Fill in invariant property in English tab and fill in variant property in Danish. Save and Publish only Danish content.

    If you now query content (for example in UmbracoApiController) using UmbracoHelper: Umbraco.Content(newContendId) and retrieve values from the properties, invariant property will be empty. If you query content using Services.ContentService and retrieve properties, invariant one will have only it's EditedValue set and the PublishedValue will be empty.

    Now, this all makes sense since the Umbraco explicitly mentions when creating non-default content variants that invariant properties are inherited from default language variant. I imagine this was a design decision.

    Essentially this means that we must create and publish Default language variant otherwise Non-default language variants will have some properties missing


    The problem is that in my custom application there should be no definition of a default language. Some content should be created only in Danish, some content only in English.

    Questions:

    1. Is it possible to achieve this in Umbraco 8?
    2. If not, is there a workaround?
  • Fredric 10 posts 80 karma points
    Aug 26, 2020 @ 07:42
    Fredric
    0

    Did you find a solution to this?

  • Aleksandr Šmailov 2 posts 73 karma points
    Aug 26, 2020 @ 11:59
    Aleksandr Šmailov
    0

    No.

    Since our business requirements do not comply with Umbraco Language Variation logic we used a different approach.

    We used Nested Content to store the language variant values.

    In retrospect, it would have been better to create Child documents instead, since Nested Elements are not separate "nodes" some issues arise with the search.

  • mmaty 114 posts 288 karma points
    Nov 26, 2020 @ 10:13
    mmaty
    0

    We have exactly the same problem here. For some of our content types we store the documents with services. We first published the default language, then immediately after publishing we unpublished the default language. And voilá: The properties can be read.

    So, somehow the invariant properties are created, if the default language variant has been published once.

    What can we do for content, which will be created and stored manually?

    Currently our idea is to let an event handler do the work: If a document is published in a non default variant only, the handler publishes and unpublishes the default variant.

    I hope, that somebody has a better idea...

  • mmaty 114 posts 288 karma points
    Nov 26, 2020 @ 13:14
    mmaty
    2

    We came up with this solution. Write an event handler:

    private void ContentService_Published( IContentService sender, Umbraco.Core.Events.ContentPublishedEventArgs e )
    {
        foreach (var content in e.PublishedEntities)
        {
            var postsToCheck = new string[]{ "article", "newspost", "textPageBlock" };
            if (postsToCheck.Contains( content.ContentType.Alias ) )
            {
                CheckVariations( content );
            }
        }
    }
    
    private void CheckVariations(IContent content)
    {
        if (content.HasDefaultCultureVariation())
            return;
        var lang = content.CultureInfos.Keys.First();
        var defaultLang = LanguageHelper.DefaultLanguageCode;
        content.SetCultureName( content.GetCultureName( lang ), defaultLang );
        this.contentService.SaveAndPublish( content, defaultLang, raiseEvents: false );
        this.contentService.Unpublish( content, defaultLang );
    }
    

    If there is no default variation, we create one and save it as draft. But it must have been published once, otherwise the solution doesn't work. Since a variant needs at least a name, we set the name of the first variant, which was saved previosly.

    You need two helper properties and a method. LanguageHelper.DefaultLanguageCode:

    public static ILanguage DefaultLanguage => ( from l in AllLanguages.Values where l.IsDefault select l ).First();
    public static string DefaultLanguageCode => DefaultLanguage.IsoCode;
    

    HasDefaultCultureVersion:

    public static bool HasDefaultCultureVariation( this IContent doc )
    {
        return doc.CultureInfos.Keys.Contains( LanguageHelper.DefaultLanguageCode );
    }
    

    EDIT: Simply saving the default variant doesn't work. Changed the code to make it work.

Please Sign in or register to post replies

Write your reply to:

Draft