Extending DD4T models with additional data

Welcome to another post of the Digital Experience Accelerator (DXA) marathon, meant to explain how DXA works, what are the different ways of using it and how can you contribute to it.

Yesterday’s post talked about writing high quality code with the coding standards / guidelines principles for DXA.

Today we will take a look into DD4T (Dynamic Delivery 4 Tridion), in particular at how to use the extension point in IModel to add custom information into the DD4T models. Using the extension point will ensure that we keep the flexibility of upgrading our DD4T in a later stage. It extends DD4T and later on, extending DXA in the right and supported way.

Achieving this is fairly easy, just as a ground rule try to never change any of the DD4T or DXA sources. This will create a custom version of the frameworks and limits the flexibility of upgrading and support.

If you are certain that something is missing or wrong, you can create a pull request for DD4T or DXA, other possibility is to ask questions on http://tridion.stackexchange.com/.

Let’s get our hands dirty and build a custom Template Building Block (TBB) in which we add information to a Component Presentation (CP). Alternatives to CP can be Component (Stub component, ECL), Page, Keyword as long as they are extensions of IModel.

This interface has one method that allows us to add information through a Dictionary, IDictionary<String, IFieldSet> ExtensionData. This Getter can be accessed within our TBB to add the custom information.

Note, the support for IModel is at the moment of writing not released, but can be downloaded https://github.com/sdl/DD4T.TridionTemplates/tree/feature/ecl-data. This is the same version of DD4T that is currently released with DXA 1.2.

Once the source is downloaded and unpacked we can create a new “Class Library” project in the “DD4T Template” solution. In this example I will be creating templates for a site module, where I prefer to use the name CustomerName.Templates. If they are for a module then create a separate project for each module.

Once the project is added to the solution, the reference of DD4T Templates can be added;

  • DD4T.Template
  • DD4T.Template.Base

Next step is to create a custom TBB, and decide on which item you would like to add additional information. In this example I will added “Hello World!” to the Component Presentations on the page. This means the TBB needs to be added within the Page flow, where the JSON is created.

Within DXA you can place it as one of the first items in “Default Page Template Finish Actions”

The additional data should now be available in the “ExtensionData” of the Component Presentations on a page. In tomorrow’s post we will create a module that will access this information, and after 3 Component Presentations with “Hello World” remove the Component from the page.

 

Thank you, and hope to see you tomorrow for another DXA post [:$]