Wednesday, February 7, 2018

SXA Fake Navigation Link

If you have worked on any website navigation, you may have encountered a scenario where you need to represent a link in the menu, such as to the Home page. In the following post I'll describe how I created a link to a page, without there actually being a page.

As you can see in the above example, the Home page is represented as a sibling to the About and Locations pages.

I created an item in the tree to represent the Home page, but without all of the overhead of a full page.

Out of the box

Let's have a look at what comes out of the box, and why I decided to do something different.

While working with SXA you'll see that there is a Navigation component which allows you to specify a few properties to simplify the setup.

The option to include the root page should work for you in most scenarios. When the markup is generated, you however do not see the Home page on the same level. Let me show the differences in markup.

Here is an example with the root page included and the structure flattened. Notice that the real Home page is at level0, and my fake Home page is at level0 but nested under a ul li.

As you would expect, if you don't flatten the structure the level classes change but the markup is the same.

By creating the fake Home item you are able to eliminate the level0 and thus simplify the markup. Notice that the levels begin with level1.

Custom Template

Getting started is really pretty straightforward.

  1. Create a new template called "Navigation Link".
  2. Add a few properties to the item.
  3. Inherit from "_Navigable".
  4. Add template to the Insert Options for other pages.
  5. Don't forget to set the source field on custom templates:
    1. Image field -> query:$siteMedia
    2. General Link field -> query:$home
    3. Rich Text -> /sitecore/system/Settings/Html Editor Profiles/Rich Text XA

When using the names I depict in the screenshot, the default rendering variants will work automatically.

Have fun with this!

- Michael