renderItemTitle is another presentational pipeline introduced in Sitecore 6, allowing to modify the appearance of item blocks, or tiles, in the folder views. Folder view is what you see on the right: a default tab on folder items. You can also add it to any other item by adding it to the Editors list.
Hooking into the renderItemTile allows changing the appearance of individual item tiles. Most often, however, customization scenarios include adding a few bits of information, relevant to all items or to specific item types.
This is how the pipeline looks out of the box (simplified):
<renderItemTile>
<processor type="RenderFolderTile" />
<processor type="RenderTemplateTile" />
<processor type="RenderDefaultTile" />
</renderItemTile>
The RenderFolderTile and RenderTemplateTile add information relevant to folders (a number of subitems) or templates (a number of usages).
In this example, I’ll be adding information about a number of validation errors of the item.
The processor has to inherit from Sitecore.Pipelines.RenderItemTile.RenderTileBase. This base class is already capable of rendering a default item tile, so I’m just slightly changing its behavior to also output a number of validation errors.
public class RenderItemTile : RenderTileBase {
public override void Process(RenderItemTileArgs args) {
if (args.View != TileView.Tiles) {
return;
}
base.Process(args);
args.AbortPipeline();
}
protected override void RenderTileDetails(HtmlTextWriter output, RenderItemTileArgs args) {
base.RenderTileDetails(output, args);
var errorCount = GetErrorCount(args.Item);
if (errorCount == 0) {
return;
}
var message = errorCount > 1 ? "{0} errors".FormatWith(errorCount) : "1 error";
output.Write("<div class="\"scTileItemDetailsLine\"" style="color: red">");
output.Write(message);
output.Write("</div>");
}
int GetErrorCount(Item item) {
var errorCount = 0;
var validators = ValidatorManager.BuildValidators(ValidatorsMode.Gutter, item);
ValidatorManager.Validate(validators, new ValidatorOptions(false));
foreach(BaseValidator validator in validators) {
if (validator.Result >= ValidatorResult.Warning) {
errorCount++;
}
}
return errorCount;
}
}
A few pieces to notice:
- args.View defines the mode the pipeline runs in: Tiles, Large Icons, Small Icons, etc. Most of those are reserved for the future use and at the moment Sitecore only uses Tiles or IconOnly modes. In this customization, we’re after the Tiles mode.
- A relatively simple RenderTileDetails method is overriden. This method is only responsible only for the information to the right of the icon. The general shape and style of the tile, as well as the icon, are handled by other more complicated methods, which we’re not interested in.
- Note the usage of the new Validation API to validate the item: GetErrorCount method. Here I’m validating the item in the ‘Gutter’ (which is the old name for Quick Action Bar) mode. This means that the folder view will only run validators configured for the quick action bar (see the validation documentation on SDN and my post on validation for more)
- Once the tile is rendered, the pipeline must be aborted, or the RenderDefaultTile processor will kick in and do it’s job.
As with other pipelines, Sitecore only knows about generic data concepts such as templates, folders and items. We cannot provide the customizations specific to the business domain of each site, but you can. The renderItemTile pipeline can be used to add information that describes each specific item type best: a publication date of the news item, a number of comments of the blog post or a discount percentage for a sales partner. And because it’s a pipeline, you can even display different information to different types of users.