an abstract image of a grid adhering to waves with various spotlights of teal and purple

Sitecore Base Layouts Part II



Ben Golden
Ben Golden

Learn how Base Layouts, which were introduced in an earlier post, can be updated to support versioned layout in Sitecore 8.

In my previous post, I introduced the concept of Base Layouts.  If you haven’t read that post yet, please do so now.  I’ll wait…


 
Ready? Then, let’s get started.

If you were paying attention to the first post, you may have noticed that I used an extension method field.IsLayoutField(). In the Sitecore 7 version, that method looks like this:

public static bool IsLayoutField(this Field field)
{
  return field.ID == FieldIDs.LayoutField;
}

The Sitecore 8 version looks like this:

public static bool IsLayoutField(this Field field)
{
  return field.ID == FieldIDs.LayoutField
              || field.ID == FieldIDs.FinalLayoutField;
}

That’s it. Post done.


 

...ok, not quite. Actually, I was quite surprised to find out how easily Base Layouts were adapted to Sitecore 8. While this really is the only change needed to get the core functionality working, we do need to consider real world usage.  

Pavel Veller wrote a series of posts earlier this year titled Sitecore 8. Versioned Layouts – Mixed Feelings (part 1, part 2, part 3). I highly recommend that you read them. In parts 1 and 2 he presents two issues related to the versioned layouts. The issue discussed in part 1 is resolved in Sitecore 8.1. The issue he raises in part 2 merits more discussion, particularly in the context of base layouts.

Pavel observed that applying a shared layout delta over a versioned layout value from a standard values item could lead to unpredictable and undesirable results. This potential issue is amplified when you can have a chain of several base layouts. To resolve this issue we can do a little validation when a user selects a new base layout:

public bool CreatesVersioningConflict(
  Item item,
  Item baseLayoutItem)
{
  var itemLayout = item.Fields[FieldIDs.LayoutField]
               .GetValue(false, false);
  return !string.IsNullOrWhiteSpace(itemLayout)
      && XmlPatchUtils.IsXmlPatch(itemLayout)
      && !string.IsNullOrWhiteSpace(
         baseLayoutItem.Fields[FieldIDs.FinalLayoutField]
                  .GetValue(false, false));
}

Note that this is a bit less stringent than Pavel’s validator. It only fails the validation if the item has a delta in the shared layout field and the selected base layout has a value in the final layout field.

I was hoping that the new pipelines introduced in Sitecore 8.1 (getFieldValue or getLayoutSourceFields) would allow the code for Base Layouts to be simplified. Unfortunately, that does not appear to be the case. It seems that using those pipelines would only complicate matters. The 8.0 version does continue to work for 8.1, though.

 

Let's Get Started

We'd love to hear from you. We probably have a lot in common. I mean, you like chatting about data-binding, UX patterns, and javascript functions, right?

X

Cookies help us improve your website experience. By using our website, you agree to our use of cookies and our privacy policy.

Accept