Changes to content loading in CMS 8
This topic describes changes to content loading in Optimizely Content Management System (CMS 8).
ILanguageSelector
was replaced with LoaderOptions
in the API in all methods where you load content. New overloads have also been added that take a CultureInfo
to make it easier to load a specific language. Methods that previously used LanguageSelector
, such as GetDefault
or CreateLanguageBranch
, now only accept CultureInfo
to simplify the usage of the API. Behind the scenes, all methods have the same behavior as before but most of the code was rewritten to support the new projects feature.
Example:
T Get<T>( Guid contentGuid) where T : IContentData;
T Get<T>( Guid contentGuid, CultureInfo language) where T : IContentData ;
T Get<T>( Guid contentGuid, LoaderOptions settings) where T : IContentData ;
In IContentRepository
, methods where you create content for a specific language now only take a CultureInfo
:
T GetDefault<T>(ContentReference parentLink, CultureInfo language) where T : IContentData;
Upgrade an existing site
To make sure it is a smooth upgrade the LanguageSelector
inherits LoaderOptions
and works pretty much as before if you choose to continue using it.
public class LanguageSelector : LoaderOptions, ILanguageSelector
{
protected LanguageSelector(){}
}
Since language handling often is handled by Optimizely and not by the code in the templates, many sites will just work after a recompilation. What may require more changes are unit tests where you are using fakes/mocks, but in many cases it is just a case of replacing mocks of ILanguageSelector
with LoaderOptions
or LanguageSelector
. An example of mocking using Moq
:
_contentRepository.Setup(repo => repo.Get<IContent>( It.IsAny<ContentReference>(), It.IsAny<LoaderOptions >())).Returns(childContent);
Upgrade content providers that are multilingual
The interface ILanguageSelector
was simplified. Now it only contains a single property Language
of type CultureInfo
. This means that content provider implementations no longer need to call any methods on ILanguageSelector
to decide which language version to return. Instead, they can just return the language version specified by ILanguageSelector.Language
. If CultureInfo.InvariantCulture
is passed in the provider should return content in the master language. If the content does not exist on the specified language then the master language should be returned.
GetPublishedOrLatest
ContentReference
have a property GetPublishedOrLatest
that loads the published version or the latest version, if no published version exists. When using this property with language fallback, it previously only loaded published versions in the fallback chain. This have been corrected and it now picks the published or latest version in the fallback chain as well. This property is normally not used in templates.
ILanguageSelectionSource
In previous versions of CMS it was possible to cast the passed in LanguageSelector
to ILanguageSelectionSource
after content retrieval. That interface contained information on why the content was selected, such as if the content was selected due to language fallback or replacement rules. The same information can now be retrieved after content retrieval by calling MatchLanguageSettings
on IContentLanguageSettingsHandler
passing in the retrieved content instance and the requested language, as:
var selectionSource = ServiceLocator.Current.GetInstance<IContentLanguageSettingsHandler>().MatchLanguageSettings(content, "sv");
In previous versions of CMS it was possible to cast the passed in LanguageSelector to ILanguageSelectionSource after content retrieval. That interface contained information on why the content was selected, such as if the content was selected due to language fallback or replacement rules. The same information can now be retrieved after content retrieval by calling MatchLanguageSettings on IContentLanguageSettingsHandler passing in the retrieved content instance and the requested language, as:
var selectionSource = ServiceLocator.Current.GetInstance<IContentLanguageSettingsHandler>().MatchLanguageSettings(content, "sv");
Updated 7 months ago