Is this how *not* to use UmbracoHelper in extension?
Hi all,
I have come accross an XSLT macro handling some media but because of the new UDIs, the macro fails to render some images (the GetMedia() extension doesn't handle UDI URIs like "umb://media/7893547576476763").
I've created a substitute extension that handles this transparently for me:
[XsltExtension("custom.helpers")]
public class Helpers {
private readonly static UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
private readonly static string MediaIdentifier = "umb://media/";
...
/// <summary>
/// Fix to support getting a Media node's XML even if the identifier is a UDID.
/// </summary>
/// <param name="identifier">Either an ID or a UDI as a string</param>
/// <param name="deep">Boolean flag whether to return children of the node</param>
public static XPathNodeIterator GetMedia(string identifier, bool deep) {
Umbraco.Core.Models.IPublishedContent mediaNode;
if (identifier.StartsWith(MediaIdentifier)) {
mediaNode = umbracoHelper.TypedMedia(Umbraco.Core.Udi.Parse(identifier));
} else {
mediaNode = umbracoHelper.TypedMedia(identifier);
}
return umbraco.library.GetMedia(mediaNode.Id, deep);
}
}
My concern here is the umbracoHelper — is this the wrong way (w/ respect to what's stated on the Common Pitfalls page?) to use this? Is there a(nother|better) way?
private static UmbracoHelper GetUmbracoHelper()
{
return new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
}
...
/// <summary>
/// Fix to support getting a Media node's XML even if the identifier is a UDID.
/// </summary>
/// <param name="identifier">Either an ID or a UDI as a string</param>
/// <param name="deep">Boolean flag whether to return children of the node</param>
public static XPathNodeIterator GetMedia(string identifier, bool deep) {
Umbraco.Core.Models.IPublishedContent mediaNode;
var umbracoHelper = GetUmbracoHelper();
if (identifier.StartsWith(MediaIdentifier)) {
mediaNode = umbracoHelper.TypedMedia(Umbraco.Core.Udi.Parse(identifier));
} else {
mediaNode = umbracoHelper.TypedMedia(identifier);
}
return umbraco.library.GetMedia(mediaNode.Id, deep);
}
///// <summary>
///// Fix to support getting a Media node's XML even if the identifier is a UDID.
///// </summary>
///// <param name="identifier">Either an ID or a UDI as a string</param>
///// <param name="deep">Boolean flag whether to return children of the node</param>
public static XPathNodeIterator GetMedia(this UmbracoHelper helper, string identifier, bool deep)
{
Udi udi;
var mediaNode = Udi.TryParse(identifier, out udi) ? helper.TypedMedia(udi) : helper.TypedMedia(identifier);
return umbraco.library.GetMedia(mediaNode.Id, deep);
}
But the tricky part was that I need this in an XSLT macro, thus my problem of getting the UmbracoHelper in the first case, which don't seem to just exists in an App_Code file...
Is this how *not* to use UmbracoHelper in extension?
Hi all,
I have come accross an XSLT macro handling some media but because of the new UDIs, the macro fails to render some images (the
GetMedia()
extension doesn't handle UDI URIs like "umb://media/7893547576476763").I've created a substitute extension that handles this transparently for me:
My concern here is the
umbracoHelper
— is this the wrong way (w/ respect to what's stated on the Common Pitfalls page?) to use this? Is there a(nother|better) way?/Chriztian
Hi Chriztian,
I would change to something like this:
To avoid the static Umbraco helper and context.
-Brad
Hi Brad,
I accidentally marked this as the solution without testing it, so it better be the right way :-)
Thanks !
/Chriztian
Hi Chriztian
I would use this code:
THanks,
Alex
Thanks Alex,
Can you explain how this works? I'm not sure I understand the
this
part of it.../Chriztian
it's Extension Method, you can add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type, read more - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
So I just added needed method to UmbracoHelper class, and we don't need to create new instance, use in views:
Thanks,
Alex
Ah OK - that makes sense;
But the tricky part was that I need this in an XSLT macro, thus my problem of getting the
UmbracoHelper
in the first case, which don't seem to just exists in an App_Code file...But thanks anyway!
/Chriztian
is working on a reply...