Quantcast
Channel: Yet Another Tridion Blog
Viewing all articles
Browse latest Browse all 215

A DD4T.net Implementation - Custom Binary Publisher (part 2)

$
0
0
In previous post Custom Binary Publisher, I presented the main logic needed to publish our Multimedia Components using custom code in DD4T .net. In this post, I present the Template Building Blocks (TBB) that call the custom binary publisher.

If you take a closer look at the code, you will notice it is basically the same code as the existing TBBs PublishBinariesComponent and PublishBinariesPage. I just created a separate PublishBinariesHelper class that uses the CustomBinaryPublisher described earlier. Calling methods PublishMultimediaComponent and PublishBinariesInRichTextField will call the overridden method PublishItem.

publicclassPublishBinariesHelper
{
privatereadonly CustomBinaryPublisher binaryPublisher;

publicPublishBinariesHelper(Package package, Engine engine)
{
binaryPublisher = new CustomBinaryPublisher(package, engine);
}

publicvoidPublishAllBinaries(Component component)
{
if (component.ComponentType == ComponentType.Multimedia)
{
component.Multimedia.Url = binaryPublisher.PublishMultimediaComponent(component.Id);
}

PublishAllBinaries(component.Fields);
PublishAllBinaries(component.MetadataFields);
}

publicvoidPublishAllBinaries(Page page)
{
PublishAllBinaries(page.MetadataFields);
}

privatevoidPublishAllBinaries(FieldSet fieldSet)
{
foreach (IField field in fieldSet.Values)
{
switch (field.FieldType)
{
case FieldType.ComponentLink:
case FieldType.MultiMediaLink:
foreach (IComponent component in field.LinkedComponentValues)
{
PublishAllBinaries(component as Component);
}
break;

case FieldType.Embedded:
foreach (FieldSet embeddedSet in field.EmbeddedValues)
{
PublishAllBinaries(embeddedSet);
}
break;

case FieldType.Xhtml:
for (int i = 0; i < field.Values.Count; i++)
{
field.Values[i] = binaryPublisher.PublishBinariesInRichTextField(field.Values[i]);
}
break;
}
}
}
}

Next, we create the actual TBB classes that use the PublishBinariesHelper -- CustomPublishBinariesComponent and CustomPublishBinariesPage, which extend their DD4T counterparts PublishBinariesComponent and PublishBinariesPage.

publicclassCustomPublishBinariesComponent : PublishBinariesComponent
{
protectedoverridevoidTransformComponent(Component component)
{
PublishBinariesHelper helper = new PublishBinariesHelper(Package, Engine);
helper.PublishAllBinaries(component);
}
}

publicclassCustomPublishBinariesPage : PublishBinariesPage
{
protectedoverridevoidTransformPage(Page page)
{
PublishBinariesHelper helper = new PublishBinariesHelper(Package, Engine);
helper.PublishAllBinaries(page);
}
}

Finally, use the custom TBB classes in your Component Template and Page Template, instead of the default DD4T PublishBinariesComponent and PublishBinariesPage TBBs.



Viewing all articles
Browse latest Browse all 215

Trending Articles