Copied to clipboard

Flag this post as spam?

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


  • Jules 269 posts 560 karma points
    Apr 08, 2017 @ 11:15
    Jules
    0

    I used to create a static utilities class in my projects for getting various things but particularly important, was media and media properties.

    After reading the Umbraco Pitfalls I stopped doing that because the utility class would need a static instance of the UmbracoHelper

    _umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    

    However, getting a media url or a media cropUrl from a picker property is a pain. To do it properly you must first check to see if the picker property has a value then you need to check to see if the media id found on the property still points to a valid piece of media (media could have been deleted but the reference stays).

    So to get a crop url generally would mean something like this ...

    if (page.HasValue(UmbracoPropertyAlias.MainImage))
    {
       var media = Umbraco.TypedMedia(page.GetPropertyValue<string>(UmbracoPropertyAlias.MainImage));
       if (media != null)
       {
          listItem.ThumbnailUrl = media.GetCropUrl(CropAlias.ThumbnailImage);
       }
       else
       {
           listItem.ThumbnailUrl = string.Empty;
       }
    }
    

    Which is rather long-winded. I would much rather have the methods from my old utility classes which were something like...

    public static string GetMediaCropUrl(int mediaId, string cropAlias)

    public static string GetMediaUrl(int mediaId)

    public static IPublishedContent GetMedia(string propertyAlias, IPublishedContent node)

    Anyway, just interested to know if anybody has found a good way to do this.... extensions maybe?

    Jules

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 08, 2017 @ 14:01
    Nik
    1

    Hi Jules,

    I've not done this, however, it does sound like it would suite creating extension methods for the UmbracoHelper. I don't imagine you'd have too much of an issue changing your existing methods to become extension methods :-)

    Nik

  • Marcio Goularte 374 posts 1346 karma points
    Apr 08, 2017 @ 17:47
    Marcio Goularte
    1

    What you are looking for here:

    https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Utilities/ExtensionMethods.cs

    I created my media utility class using some methods of this extension class of Hybrid Framework.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Apr 08, 2017 @ 17:59
    Dan Diplo
    3

    Yep, just add extension methods to the Umbraco helper. So, for instance, your GetMediaUrl method would become (something like):

    public static string GetMediaUrl(this UmbracoHelper helper, int mediaId)
    {
        var media = helper.TypedMedia(mediaId);
        return media != null  ? media.Url : null;
    }
    

    Remember to create your extensions in a static class.

    You'd then just call it like:

    string url = Umbraco.GetMediaUrl(1234);
    
  • Jules 269 posts 560 karma points
    Apr 09, 2017 @ 20:49
    Jules
    1

    Thanks to all !

    All sorted now.

    J

Please Sign in or register to post replies

Write your reply to:

Draft