SaaS CMS has officially launched! Learn more now.

Dac Thach Nguyen
Oct 30, 2018
  1892
(8 votes)

Language Manager: Replace Content with Unpublished Version

When using Language Manager (LM) to duplicate content from other language. It always get content from Published or Common Draft version. There is a customer they want to get content from Ready To Publish version instead. To change the behavior we can intercept the LanguageBranchManager service. In IConfigurableModule you add following line:

public void ConfigureContainer(ServiceConfigurationContext context)
{
    context.Services.Intercept<ILanguageBranchManager>((locator, defaultManager) => new MyLanguageBranchManager(defaultManager));
}

And implment the MyLanguageBranchManager, most of methods we should forward to original service to process. We just modify the [CopyDataFromMasterBranch] method for copying content from Ready To Publish version instead of Published/CommonDraft one. Below is the code for doing this:

public bool CopyDataFromMasterBranch(ContentReference contentReference, string fromLanguageID, string toLanguageID, Func<object, object> transformOnCopyingValue, out ContentReference createdContentLink, bool autoPublish = false)
{
    fromLanguageID = fromLanguageID.Trim();
    toLanguageID = toLanguageID.Trim();

    createdContentLink = null;

    // get ReadyToPublish version here to process copying, fallback to Published or CommonDraft version
    var masterContent = GetReadyForPublishVersion(contentReference, fromLanguageID) ?? GetPublishedOrCommonDraftVersion(contentReference, fromLanguageID);
    var destContent = GetPublishedOrCommonDraftVersion(contentReference, toLanguageID);

    if (masterContent == null)
    {
        throw new ContentNotFoundException(contentReference);
    }

    if (destContent == null)
    {
        CreateLanguageBranch(contentReference, toLanguageID, out createdContentLink);
    }
    var createdDestContent = contentRepository.Service.Get<IContent>(contentReference.ToReferenceWithoutVersion(), new LanguageSelector(toLanguageID));
    createdDestContent = (createdDestContent as IReadOnly).CreateWritableClone() as IContent;

    #region process for Name, PageURLSegment property of both page and block
    if (transformOnCopyingValue == null)
    {
        createdDestContent.Name = masterContent.Name;
    }
    else
    {
        createdDestContent.Name = transformOnCopyingValue(masterContent.Name) as string;
        if (masterContent.Property["PageURLSegment"] != null)
        {
            string url = transformOnCopyingValue(masterContent.Property["PageURLSegment"].ToWebString().Replace('-', ' ')) as string;   /* the result from Bing */
            url = Regex.Replace(url, @"\s+", " ", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant).Trim(); // convert multiple spaces into one space
            url = Regex.Replace(url, @"\s", "-", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant); // Replace spaces by dashes
            url = Regex.Replace(url, @"[^a-z0-9~_\-\.]", matchEvaluatorRandomReplace, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant); // Remove all non valid chars
            createdDestContent.Property["PageURLSegment"].Value = url;
        }
    }
    #endregion

    #region process all LanguageSpecific, non-meta, editable properties

    foreach (PropertyData destProp in createdDestContent.Property.Where(pd =>
        (pd.IsLanguageSpecific && !pd.IsMetaData && !pd.IsReadOnly) // find all non-metadata properties, which is editable, language specific
        ))
    {
        var nestedContentData = destProp.Value as IContentData;     // block
        if (null != nestedContentData) // if is block, ...
        {
            CopyDataForNestedContentRecursive(masterContent.Property[destProp.Name] as IContentData, nestedContentData, transformOnCopyingValue);
        }
        else
        {
            // some data type and its property type (e.g. CategoryList & PropertyCategory) implements IModifiedTrackable and then a call to IsModified 
            // (leading to IReadOnly.ThrowIfReadOnly) from ContentRepository.Save below, so that must use WritableClone value
            var srcPropValue = masterContent.Property[destProp.Name].Value;
            destProp.Value = srcPropValue is IReadOnly ? ((IReadOnly)srcPropValue).CreateWritableClone() : srcPropValue;
            if (transformOnCopyingValue != null && destProp.Value != null)
            {
                var translatedText = transformOnCopyingValue(destProp);
                TryToAssignStringToProperty(translatedText, destProp);
            }
        }
    }   // end foreach prop in page level

    #endregion

    // skip validation, because translate might be failed sometime.
    var saveFlag = SaveAction.Save | SaveAction.SkipValidation;
    saveFlag = (createdDestContent as IVersionable).Status == VersionStatus.Published ?
        saveFlag | SaveAction.ForceNewVersion : saveFlag | SaveAction.ForceCurrentVersion;
    if (autoPublish)
    {
        saveFlag = saveFlag | SaveAction.Publish;
    }

    createdContentLink = contentRepository.Service.Save(createdDestContent, saveFlag, AccessLevel.NoAccess);
    contentVersionRepository.Service.SetCommonDraft(createdContentLink);

    return true;

    // return _defaultLanguageBranchManager.CopyDataFromMasterBranch(contentReference, fromLanguageID, toLanguageID, transformOnCopyingValue, out createdContentLink, autoPublish);
}

private IContent GetReadyForPublishVersion(ContentReference contentLink, string languageID)
{
    var contentVersionRepository = ServiceLocator.Current.GetInstance<IContentVersionRepository>();
    var versions = contentVersionRepository.List(contentLink, languageID);
    var readyToPublishVersion = versions.FirstOrDefault(v => v.Status == VersionStatus.CheckedIn);

    var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
    if (readyToPublishVersion != null)
    {
        return contentRepository.Get<IContent>(readyToPublishVersion.ContentLink);
    }

    return null;
}

/// <summary>
/// Copies the data for nested content recursively.
/// </summary>
/// <param name="sourceContentData">The source content data.</param>
/// <param name="targetContentData">The target content data.</param>
/// <param name="transformOnCopyingValue">perform a transformation on copying property's value</param>
private void CopyDataForNestedContentRecursive(IContentData sourceContentData, IContentData targetContentData, Func<object, object> transformOnCopyingValue)
{
    using (IEnumerator<PropertyData> propertyDataEnumerator = targetContentData.Property
        .Where(pd => pd.IsLanguageSpecific && !pd.IsMetaData && !pd.IsReadOnly)
        .GetEnumerator())
    {
        while (propertyDataEnumerator.MoveNext())
        {
            var targetProp = propertyDataEnumerator.Current;
            if (targetProp is IContentData)
            {
                CopyDataForNestedContentRecursive(sourceContentData.Property[targetProp.Name] as IContentData, targetProp as IContentData, transformOnCopyingValue);
            }
            else
            {
                // some data type and its property type (e.g. CategoryList & PropertyCategory) implements IModifiedTrackable and then a call to IsModified 
                // (leading to IReadOnly.ThrowIfReadOnly) from ContentRepository.Save below, so that must use WritableClone value
                var srcPropValue = sourceContentData.Property[targetProp.Name].Value;
                targetProp.Value = srcPropValue is IReadOnly ? ((IReadOnly)srcPropValue).CreateWritableClone() : srcPropValue;
                if (transformOnCopyingValue != null)
                {
                    var translatedText = transformOnCopyingValue(targetProp);
                    TryToAssignStringToProperty(translatedText, targetProp);
                }
            }
        }
    }
}

/// <summary>
/// assign <paramref name="obj"/> to <paramref name="prop"/> might lead to exception because <paramref name="prop"/> cannot accept string >255.
/// We try to shorten it before assigning again.
/// </summary>
/// <param name="obj"></param>
/// <param name="prop"></param>
/// <returns></returns>
private void TryToAssignStringToProperty(object obj, PropertyData prop)
{
    try
    {
        prop.Value = obj;
    }
    catch (EPiServerException ex)
    {
        if (ex.Message.Contains("exceeded"))    // exceeded 255 characters
        {
            prop.Value = ((string)obj).Substring(0, 255);
        }
        else
        {
            throw;
        }
    }
}

If you have similar requirement for your site. I hope this will help you a litle bit.

Oct 30, 2018

Comments

valdis
valdis Oct 30, 2018 12:50 PM

just have couple of my 2 cents:

- why ServiceLocator?

- why ".GetEnumerator()" & "while(..MoveNext())"

- if method starts with "Try.." I would not expect to receive Exception back, bot bool instead

- also I would try to split code into smaller chunks to organize this quite complex code

Please login to comment.
Latest blogs
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024