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.
Next, we create the actual TBB classes that use the PublishBinariesHelper -- CustomPublishBinariesComponent and CustomPublishBinariesPage, which extend their DD4T counterparts PublishBinariesComponent and PublishBinariesPage.
Finally, use the custom TBB classes in your Component Template and Page Template, instead of the default DD4T PublishBinariesComponent and PublishBinariesPage TBBs.
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.
public class PublishBinariesHelper { private readonly CustomBinaryPublisher binaryPublisher; public PublishBinariesHelper(Package package, Engine engine) { binaryPublisher = new CustomBinaryPublisher(package, engine); } public void PublishAllBinaries(Component component) { if (component.ComponentType == ComponentType.Multimedia) { component.Multimedia.Url = binaryPublisher.PublishMultimediaComponent(component.Id); } PublishAllBinaries(component.Fields); PublishAllBinaries(component.MetadataFields); } public void PublishAllBinaries(Page page) { PublishAllBinaries(page.MetadataFields); } private void PublishAllBinaries(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.
public class CustomPublishBinariesComponent : PublishBinariesComponent { protected override void TransformComponent(Component component) { PublishBinariesHelper helper = new PublishBinariesHelper(Package, Engine); helper.PublishAllBinaries(component); } } public class CustomPublishBinariesPage : PublishBinariesPage { protected override void TransformPage(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.
Comments