LibrarySites.Banner

Fallback Series Post 7: Defaulting Language Versions for Fallback

Once you have decided that using Enforce Language Version Presence is the way to go for your website, you commit your content authors to certain actions for content to show up on the website in the various languages.  If the any of the language versions of an item are missing, then within the context of that language, the item will be considered missing.

Alex Shyba's Partial Language Fallback Module has cut one tedious aspect out of creating additional language versions.   Content authors no longer need to copy and paste content from original versions to new languages if they want the content to be the same across languages.  As long as the fields in the templates have their "Enable Fallback If Configured" checkboxes checked, the fallback language value will be displayed in both the content editor and on the front-end of the site.

There is still the potential tedious task of creating these language versions, though.  If you have a site with more than two or three languages, it could become time consuming to switch the language icon and click the link to create the language version.

It is with this in mind that I added the next two features.

The first is a new item command.  Click on any item and navigate to the Versions tab in the ribbon.  In the Language section, there is a new button: "Add All Lang Versions".  Clicking this button will run a new method: CreateVersionInAllLanguages.  I have to give 'boro' credit for showing me the way in this blog post.

I created a class that implements Command.  The Execute method kicks off the Run method which gets the current item that the user is on.  It will first prompt the user with a confirm box to make sure they want to create a version for each language.  If the answer is 'yes', it will run a method in the LanguageHelper class named CreateVersionInEachLanguage which takes a parameter of Item.

This method gets all of the languages in Sitecore that are not the item's current language and loops through them.  It gets the item in each language and checks if it exists by checking the version count.  If there are no versions of the item in that language, it will create it.

public static void CreateVersionInEachLanguage(Item item)

        {

            IEnumerable<Language> languages = LanguageManager.GetLanguages(item.Database).Where(a => a != item.Language);

 

            foreach (Language language in languages)

            {

                Item localizedItem = item.Database.GetItem(item.ID, language);

 

                //if Versions.Count == 0 then no entries exist in the given language

                if (localizedItem.Versions.Count == 0)

                {

                    localizedItem.Editing.BeginEdit();

                    localizedItem.Versions.AddVersion();

                    localizedItem.Editing.EndEdit();

                }

            }

        }

With this method in place, you need to hook up a command that the user can click within the content editor.  First add an entry to the Commands.config file in App_Config

  <command name="item:addversiontoalllanguages" type="sharedsource_verndale._Classes.SitecoreCommands.CreateVersionInAllLanguages,Verndale.SharedSource" />

Then in Sitecore, open the core database and navigate to the following location:

/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Language

Add a Large Button (/sitecore/templates/System/Ribbon/Large Button), named Add All Language Versions, and set the following values:

  • Header: Add All Lang Versions
  • Icon: Network/32x32/download.png
  • Click: item:addversiontoalllanguages
  • (this is referencing the value set in the commands.config)
    • Tooltip: Add all language versions to this item.

Fallback 7

That's it.  When you click on an item and click this button in the content editor, you will find that it adds blank versions of all languages that have not yet been added.

Some clients want to take it to the next level though and ALWAYS have all language versions added when the item is created.  They would rather assume every item should have every language version and then have content authors remove versions if necessary.

We already have the logic for this, we can reuse the CreateVersionInEachLanguage in the LanguageHelper class.  We just need to add something to the item created event, to make the call.  ALSO, we don't want this to run for every item that gets created, only items in certain locations (or maybe certain templates).

This solutions assumes item locations is enough, but you could extend it to check for template and make a decision based on that.

In my Sitecore.SharedSource.PartialLanguageFallback.config file, I added a setting named Fallback.PathsToCheckForLanguageVersions.  Multiple paths can be entered, separated by pipe.

<setting name="Fallback.PathsToCheckForLanguageVersions" value="/sitecore/content/fallback demo|/sitecore/media library/files|/sitecore/media library/images" />

Then I added a class to be called from the item created event, CreateVersionInAllLanguagesOnCreate.

It checks to see if the Fallback.PathsToCheckForLanguageVersions setting is set.  If not, it returns without doing anything.  It splits on the '|' character and then loops through the paths in the list.  It checks if the current item that has just been created is within that path.  If it finds that it was, it will call the CreateVersionInEachLanguage method in the LanguageHelper class.

Finally, add the configuration in the Sitecore.SharedSource.PartialLanguageFallback.config to hook into that event:

<events timingLevel="custom">

      <event name="item:created">

        <handler type="Verndale.SharedSource.SitecoreProcessors.CreateVersionInAllLanguagesOnCreate, Verndale.SharedSource" method="OnItemCreated"/>

      </event>

    </events>

Now, whenever an item is created within any of those locations, it will automatically create all of the language versions for you.

These little additions are a nice-to-have for content authors when implementing fallback with enforcement of language version presence.  In my next post, I will provide information about a tool that will help when needing to do mass updates to language versions: useful when adding a brand new language to a site, or removing a language from a site, or copying content from one language to another, or just verifying all content is created in a particular language version.

Link to Resources:

https://github.com/Verndale-Corp/Sitecore-Fallback-Default-Language-Creation

https://github.com/Verndale-Corp/Sitecore-Fallback-FullDemo


  • We did something similar to this, but instead of creating language versions for those items that have no changes yet, we coupled the Partial Language Fallback module with the Language Fallback module. The second does item-level fallback, which handles the case where the item does not exist in that language.  Of course, if your desired behavior is that you get a 404 if the language version does not exist for an item, then our solution will not work.