Copied to clipboard

Flag this post as spam?

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


  • Proxicode 128 posts 324 karma points
    Apr 21, 2017 @ 21:07
    Proxicode
    0

    Umbraco 7.6 and Media

    Hey everyone!

    I am working with 7.6 and made a pretty basic Archetype called "Partners" which looks like this...

    enter image description here

    So a partner is just a name, a logo, and a link. partnerName is a simple text string, partnerLogo is a MediaPicker (with multi items and folder select disabled), and partnerLink is from another of Kevin's awesome projects-- UrlPicker.

    My trouble is in getting the image from the media picker while looping through the Archetype. My code is below.

    <section class="partners_in">
        <h1>@Model.Content.PartnersHeading</h1>
        <ul>
            @foreach (var partner in Model.Content.GetPropertyValue<ArchetypeModel>("partners"))
            {
                <li>
                    @if (partner.HasValue("partnerLink"))
                    {
                        var widgetLink = partner.GetValue<UrlPicker>("partnerLink");
                        var linkTarget = widgetLink.Meta.NewWindow ? "_blank" : null;
                        var linkUrl = widgetLink.Url;
                        if (widgetLink.Type == UrlPicker.UrlPickerTypes.Url)
                        {
                            if (!string.IsNullOrEmpty(linkUrl) && !(linkUrl.StartsWith("http://") && !(linkUrl.StartsWith("https://"))))
                            {
                                linkUrl = "http://" + linkUrl;
                            }
                        }
                        <a href="@linkUrl" target="@linkTarget" class="link">
                            <figure>
                                <img src="@Umbraco.Media(partner.GetValue("partnerLogo")).Url" alt="img" />
                            </figure>
                        </a>
                    }                    
                </li>
            }
        </ul>
    </section>
    

    This does run, but it just outputs

    <img src(unknown) alt="img">
    

    When I pull this out and run it on it's own line

    partner.GetValue("partnerLogo")
    

    It returns

    Umbraco.Core.Udi[]
    

    So I'm not sure if this has anything to do with Archetype or if this is just a change in how Media should be returned in 7.6. I've always had success using @Umbraco.Media(string or int) but it seems that no Media method is expecting a Udi. I've read the little bit of info that I can find but I'm not having much luck. Thanks for any pointers on this.

    Thanks

    -Roger

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 21, 2017 @ 21:18
    Dennis Adolfi
    1

    Hi Proxicode.

    Yes, this is an issue with 7.6 but it is soon solved. Read the comments in this thread: http://issues.umbraco.org/issue/U4-7318

    Have a great weekend!

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 22, 2017 @ 07:40
    Dave Woestenborghs
    103

    Hi Proxicode,

    This is because in v7.6 links to items from pickers(media, content, multinode,...) are not stored as integer id's anymore, but in the new UDI format.

    See this topic which had the same issue for the related links datatype : https://our.umbraco.org/forum/templates-partial-views-and-macros//85224-umbraco-76-rc-related-links-umbracowebmodelsrelatedlink-does-not-contain-a-definition-for-link-or-for-isinternal#comment-269795

    Luckily the core value convertors package got integrated in the core in v7.6.

    https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/

    Now you can do this in your code :

    partner.GetValue<IPublishedContent>("partnerLogo")
    

    This will return a IPublished content item representing the selected media item.

    Dave

  • Proxicode 128 posts 324 karma points
    Apr 22, 2017 @ 15:54
    Proxicode
    2

    Ah thanks a lot Dave! This got me going.

    Seems worth mentioning that this this doesn't work...

    <figure>
         <img src="@partner.GetValue<IPublishedContent>("partnerLogo").Url" alt="img" />
    </figure>
    

    no errors, builds fine, but nothing displays.

    This however DOES work!

    <figure>
         @{
            var imageUrl = partner.GetValue<IPublishedContent>("partnerLogo").Url;
         }
         <img src="@imageUrl" alt="img" />
    </figure>
    

    Thanks again for taking the time to help me out. Have a good one!

    -Roger

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Apr 28, 2017 @ 12:22
    Paul Wright (suedeapple)
    1

    Untested but this might work:

    <img src="@(partner.GetValue<IPublishedContent>("partnerLogo").Url)" alt="img" />
    
  • Proxicode 128 posts 324 karma points
    Apr 29, 2017 @ 16:52
    Proxicode
    0

    Hey Paul - Nice - yeah your solution does work as well!

    One of those crazy syntax things that I'm sure has a meaning that I don't understand as to why this doesn't work

    <img src="@partner.GetValue<IPublishedContent>("partnerLogo").Url" alt="img" />
    

    but this does

    <img src="@(partner.GetValue<IPublishedContent>("partnerLogo").Url)" alt="img" />
    

    Thanks for chiming in on this :-)

    -Roger

  • Tiago 12 posts 94 karma points
    Nov 07, 2017 @ 15:01
    Tiago
    3

    I know this an extremely late reply, but I came across this thread, and your reply, and thought Id answer your question.

    The reason that doesn't work is because without the parenthesis the razor parser will interpret the '<...>' as an html element which means you will be trying to access the getPropertyValue method as a property of the product object and not as a method.

Please Sign in or register to post replies

Write your reply to:

Draft