Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 23, 2017 @ 22:10
    Chriztian Steinmeier
    0

    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?

    /Chriztian

  • Brad McDavid 11 posts 123 karma points
    Oct 24, 2017 @ 00:01
    Brad McDavid
    100

    Hi Chriztian,

    I would change to something like this:

    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);
    }
    

    To avoid the static Umbraco helper and context.

    -Brad

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 24, 2017 @ 07:24
    Chriztian Steinmeier
    1

    Hi Brad,

    I accidentally marked this as the solution without testing it, so it better be the right way :-)

    Thanks !

    /Chriztian

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Oct 25, 2017 @ 18:38
    Alex Skrypnyk
    0

    Hi Chriztian

    I would use this code:

    ///// <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);
    }
    

    THanks,

    Alex

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 25, 2017 @ 20:57
    Chriztian Steinmeier
    0

    Thanks Alex,

    Can you explain how this works? I'm not sure I understand the this part of it...

    /Chriztian

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Oct 25, 2017 @ 21:01
    Alex Skrypnyk
    0

    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:

    var nodeIterator = Umbraco.GetMedia(identifier, deep);
    

    Thanks,

    Alex

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 25, 2017 @ 21:06
    Chriztian Steinmeier
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft