Copied to clipboard

Flag this post as spam?

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


  • syn-rg 282 posts 425 karma points
    Jan 10, 2013 @ 06:10
    syn-rg
    0

    Umbraco library GetMedia parent node property

    How can I get a property from the parent node of a media item using "umbraco.library:GetMedia"?

    This allows me to get the current nodes "@nodeName"

    <xsl:value-of select="umbraco.library:GetMedia(., 0)/@nodeName" />

    I want to get the parent of the current nodes "@nodeName", I've tried the following but it doesn't work:

    <xsl:value-of select="umbraco.library:GetMedia(., 0)/../@nodeName" />

    Can anyone help me out?

    Cheers, JV

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 10, 2013 @ 09:41
    Chriztian Steinmeier
    101

    Hi JV,

    Though your reasoning is 100% correct, the problem is that you're only getting the XML for the requested item, without its context. So you need to do one of two things - either:

    1. Grab all the media XML once, and then use XPath to select the items you need, or...
    2. Use a second call to GetMedia() with the @parentID attribute of the first, to get to its parent

    Which option to choose depends on the size of your Media library and on how much you're hitting this functionality, i.e., if you're oing to do this for a bunch of items or just for a few? If you're just doing this for a couple of items, use #2 and do something like this:

    <xsl:template match="/">
        <!-- Do not call unless an image was picked -->
        <xsl:apply-templates select="$currentPage/image[normalize-space()]" />
    </xsl:template>
    
    <xsl:template match="image">
        <!-- Note: These WILL fail if no media is selected -->
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(., false())" />
        <xsl:variable name="parentMedia" select="umbraco.library:GetMedia($mediaNode/@parentID, false())" />
    
        <p>
            Media: <xsl:value-of select="$mediaNode/@nodeName" />
        </p>
        <p>
            Parent: <xsl:value-of select="$parentMedia/@nodeName" />
        </p>
    </xsl:template>

    Note that I'm assuming the property alias image for the property - change to suit your setup.

    /Chriztian

  • syn-rg 282 posts 425 karma points
    Jan 10, 2013 @ 23:25
    syn-rg
    0

    Thanks Chriztian, that is perfect for what I need. I greatly appreciate your help.

    How can I get properties from the mediaNodes "following-sibling"?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 11, 2013 @ 18:39
    Chriztian Steinmeier
    1

    Hi again - hadn't seen the bonus question :-)

    It's more or less the same problem - in $mediaNode you only have the XML for that one item. And in parentMedia, you only have the parent's XML - the mediaNode's siblings aren't present in either of those - BUT - if you set the boolean flag to true() on $parentMedia, you'll suddenly have all of mediaNode's siblings (plus their children and their children etc.) - so:

    <xsl:template match="image">
        <!-- Note: These WILL fail if no media is selected -->
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(., false())" />
        <xsl:variable name="parentMedia" select="umbraco.library:GetMedia($mediaNode/@parentID, true())" />
    
        <p>
            Media: <xsl:value-of select="$mediaNode/@nodeName" />
        </p>
        <p>
            Parent: <xsl:value-of select="$parentMedia/@nodeName" />
        </p>
        <p> Following sibling: <xsl:value-of select="$parentMedia/*[@id = $mediaNode/@id]/following-sibling::*[1]/@nodeName" /> </p>
    </xsl:template>

    /Chriztian

  • syn-rg 282 posts 425 karma points
    Jan 14, 2013 @ 05:01
    syn-rg
    0

    Thanks again Chriztian, that has worked perfectly! Cheers, JV

Please Sign in or register to post replies

Write your reply to:

Draft