OK, so you've written your Resize Image TBB that publishes a variant image... But now you need a reference (e.g. the URL) of this variant in DWT. How would I get that?
The first thing I tried, in my DW TBB, was to reference the image variant using the attribute tridion:variantId. How cool it would be that by just referencing the image TcmUri and variantId in DWT to actually have that resolved and replaced by the Resolve Links default TBB. Something like this:
Well, it doesn't work! The TBB Resolve Links doesn't know about attribute tridion:variantId on an img element.
So, I told myself "I will write My Own Resolve Links TBB" and I started investigating... What I tried was retrieving the image variant from the package (lookup by name or Tcmuri), then identify the one I needed by matching on the variantId. Well, that doesn't work either! It seems that there is only one MMComponent in the Package, added by the DW TBB and that one does not have a variantId (i.e. this is the original MMComponent). I event iterated over the entire Package hoping to identify the image variant added by RenderedItem.AddBinary() method. It is not in the package! So I could not retrieve it!
Next, I tried adding the image variant to the package and specify the variant myself. I quickly abandoned this approach, simply because I could not find a Package.Create*Item that would allow me to add a MMComponent with a variant. Seemed like a dead-end!
Finally, I still had to solve this problem, so I ended up using the good old method of creating a String package item holding the URL of the image variant. The following code goes at the end of the Resize Image TBB.
Binary binary = m_Engine.PublishingContext.RenderedItem.AddBinary(
Therefore in DW TBB, you would just use @@MyImage@@ to output the image variant URL.
The first thing I tried, in my DW TBB, was to reference the image variant using the attribute tridion:variantId. How cool it would be that by just referencing the image TcmUri and variantId in DWT to actually have that resolved and replaced by the Resolve Links default TBB. Something like this:
<img src="tcm:1-2" tridion:variantId="thumbnail"/>
Well, it doesn't work! The TBB Resolve Links doesn't know about attribute tridion:variantId on an img element.
So, I told myself "I will write My Own Resolve Links TBB" and I started investigating... What I tried was retrieving the image variant from the package (lookup by name or Tcmuri), then identify the one I needed by matching on the variantId. Well, that doesn't work either! It seems that there is only one MMComponent in the Package, added by the DW TBB and that one does not have a variantId (i.e. this is the original MMComponent). I event iterated over the entire Package hoping to identify the image variant added by RenderedItem.AddBinary() method. It is not in the package! So I could not retrieve it!
Next, I tried adding the image variant to the package and specify the variant myself. I quickly abandoned this approach, simply because I could not find a Package.Create*Item that would allow me to add a MMComponent with a variant. Seemed like a dead-end!
Finally, I still had to solve this problem, so I ended up using the good old method of creating a String package item holding the URL of the image variant. The following code goes at the end of the Resize Image TBB.
Binary binary = m_Engine.PublishingContext.RenderedItem.AddBinary(
resizedStream, newFilename, variantId, mmc,
binaryContent.MultimediaType.MimeType);
m_Package.PushItem("MyImage",
m_Package.CreateStringItem(ContentType.Text, binary.Url));
m_Package.CreateStringItem(ContentType.Text, binary.Url));
Therefore in DW TBB, you would just use @@MyImage@@ to output the image variant URL.
<img src="@@MyImage@@"/>
Comments