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.
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.
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.
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.
How to publish only non-default language variant content?
I'm using Umbraco 8.1.3 (same thing with 8.1.5).
Steps:
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 usingServices.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:
Did you find a solution to this?
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.
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...
We came up with this solution. Write an event handler:
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:
HasDefaultCultureVersion:
EDIT: Simply saving the default variant doesn't work. Changed the code to make it work.
is working on a reply...