Improving link resolving in DD4T

When you use DD4T to create a web site, there are two ways to handle links. One is to write out a hyperlink. This is done by calling the LinkFactory and passing it the current component. For this purpose, all you need to know is the component’s URI.

The other option is to write out content from the linked component itself. In this case, it helps if you have the complete linked component at your disposal, URI, title, fields, metadata, etc.

It’s very neat to have the linked component inside your model, without having to retrieve it dynamically. However, there is a price to pay: the XML that you publish becomes much bigger because it incorporates the linked components as well.  This slows down publishing and may slow down your broker database.

Also, it does not always stop at one level of links. Sometimes, you may feel the need to follow a chain of links, for 2, 3 or more levels. To make this manageable, DD4T uses a mechanism called ‘LinkLevels’: by setting a parameter ‘LinkLevels’, you tell the DD4T templates how deep they should go when following links.

This is a rather rough principle though. In reality, there are some fields which you never want to follow (for example if you know up front that you will write them out as hyperlinks anyway). The LinkLevels mechanism however does not distinguish between fields, so you will get these linked components in the broker anyway!

This diagram shows how it works:

link levels - old

 

With link level set to two, a total of 7 components are published. Only 2 are needed completely, and for 3 we only need the URI. So most of the content is rendered to XML, published and stored in the broker for no reason at all.

Configuring links per field

To do anything about this, we need to have a way to configure link resolving behaviour on a per-field basis. At the recent MVP retreat I had the pleasure of talking to Jaime Santos Alcón. Jaime is a real GUI extension guru. He showed me how easy it is to add custom properties to a schema field in Tridion (at least, he made it look easy!).

With this in mind, and a lot of helpful code by Jaime to get met started (see http://jaimesantosalcon.blogspot.nl/2013/10/adding-extended-information-to-schema_28.html), I created a GUI extension of my own: the Trivident DD4T Editor. This editor adds one little checkbox to the schema definition screen:

dd4t editor

 

When the property ‘Follow link when rendering XML (dd4t)’ is checked, this information is stored in the ExtensionXml of the schema field. It is then picked up by the DD4T template code to determine whether or not it should follow a link.

 

Now, the diagram would look a bit different:

link levels

 

As you can see, there are a lot less components in the broker now, which will improve publisher performance and reduce the amount of data in the broker database.

Installing and configuring the new solution

To install, download trivident.dd4t.editor 1.0.1 and follow the instructions in the README.txt.

 

After the installation you should first enable one or more schema fields to ‘Follow link when rendering XML’. Note that you will only see the checkbox for component link and multimeda link fields!

Next, open your existing templates (component + page) in the template builder and set the parameter ‘Use field setting to determine whether or not to follow links’ to ‘yes’ (okay, the name is a bit too long, please suggest a better one in a comment!).

follow link levels per field

You’ll notice that in the example, the LinkLevels are set to 3. Note that the LinkLevels parameter is not obsolete in this new solution. Rather, it is treated as a ‘Max Link Level’. This is necessary because otherwise you may still acumulate too much XML by following links.

The configuration above means: “follow fields which are configured to be followed, but stop when you reach level 3”.

 

Download

Installation instructions for the GUI extension are in the README.txt inside the zip.

To download the latest DD4T templates, go to https://github.com/dd4t/DD4T.TridionTemplates/releases.

Thanks to Jaime Santos Alcón.