Copied to clipboard

Flag this post as spam?

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


  • Fredrik 89 posts 108 karma points
    Oct 22, 2010 @ 15:05
    Fredrik
    0

    Display image from mediapicker property

    I have a document type product that has a property(image) mediapicker in which I select an image.

    When I loop through my products I wish to display the image just like i display the other properties for example:

    <xsl:for-each select="$currentPage/* [name() = $documentTypeAlias and string(umbracoNaviHide) != '1']">
     <li>
      <a>

    <xsl:value-of select="description" />

    </a>

     </li>

    </xsl:for-each>

    I've tried this link but I do not get the hang of it

    http://blog.leekelleher.com/2010/08/11/how-to-use-umbraco-library-getmedia-in-xslt-for-umbraco-v4-5/

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Oct 22, 2010 @ 15:14
    Tom Fulton
    1

    Lee's code looks good, all you should need to do is change "$currentPage/mediaId" - change mediaId to the property alias of your media picker field.

    <xsl:for-each select="$currentPage/* [name() = $documentTypeAlias and string(umbracoNaviHide) != '1']">
     <li>

        <xsl:variable name="mediaId" select="number($currentPage/mediaId)" />
        <xsl:if test="$mediaId > 0">
          <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
          <xsl:if test="$mediaNode/umbracoFile">
            <img src="{$mediaNode/umbracoFile}" alt="[image]" height="{umbracoHeight}" width="{umbracoWidth}" />
          </xsl:if>
        </xsl:if>

      <a>
        <xsl:value-of select="description" />
      </a>

     </li>

    </xsl:for-each>


    The code might look a bit complicated but thats because it has some error checking and is simplified a bit for readability.

    Let us know if you have any problems/questions

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Oct 22, 2010 @ 15:16
    Tom Fulton
    0

    In short, you can't just call it like a regular field, because it stores the Media ID rather than the path.  You need to convert the Media ID to the file path by using GetMedia to get the XML of the media node, then use the umbracoFile property.

    Basically:  <img src="{umbraco.library:GetMedia($currentPage/mediaId)/umbracoFile}"/> - but use Lee's code as it has good error checking.

  • Fredrik 89 posts 108 karma points
    Oct 22, 2010 @ 15:38
    Fredrik
    0

    Still no display...

    <xsl:variable name="mediaId" select="number($currentPage/productimage)" />   
    <xsl:if test="$mediaId > 0">     
    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />     
    <xsl:if test="$mediaNode/umbracoFile">       
    <img src="{$mediaNode/umbracoFile}" alt="[image]" height="{umbracoHeight}" width="{umbracoWidth}" />     
    </xsl:if>   
    </xsl:if>

    productimage is the property alias

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Oct 22, 2010 @ 15:40
    Tom Fulton
    0

    Try writing out this line at the beginning of your loop to make sure there is actually a value there. 

    mediaid:  <xsl:value-of select="$currentPage/productimage"/>

    If there is, add some text after each IF statement so you can see how far it's getting.

  • Fredrik 89 posts 108 karma points
    Oct 22, 2010 @ 15:44
    Fredrik
    0

    There is no value. Which is strange since  I have choosen media in the mediapicker property and I can get output from the other properties(textstrings etc)

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Oct 22, 2010 @ 15:49
    Tom Fulton
    0

    I do believe the properties are case sensitive, it's not productImage is it?

    To troubleshoot further you can look at the raw XML of the node using the code below.  This will show you the XML your code is reading.  If it's not there, the data isn't published or you've got some other problem :)

    Put this as the first line in your loop to output the XML for the current item in the loop:

        <textarea><xsl:copy-of select="."/></textarea>

    Take a look at the XML inside the textarea and make sure there is a <productimage> tag with a value in it.

  • Fredrik 89 posts 108 karma points
    Oct 22, 2010 @ 15:58
    Fredrik
    0

    I got it to display the productimage ID(1061) now but still no display of the image

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Oct 22, 2010 @ 16:34
    Tom Fulton
    0

    To troubleshoot, I would add some text after some of the <xsl:if statements so you can see how far the code is getting and where it is failing.

    Something like:

    <xsl:variable name="mediaId" select="number($currentPage/productimage)" />  
    <xsl:if test="$mediaId > 0">
    Found mediaId...
    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />     
    <xsl:if test="$mediaNode/umbracoFile">
    Found umbracoFile property...
    <img src="{$mediaNode/umbracoFile}" alt="[image]" height="{umbracoHeight}" width="{umbracoWidth}" />     
    </xsl:if>   
    </xsl:if>

  • Kim Andersen 1447 posts 2196 karma points MVP
    Oct 22, 2010 @ 23:00
    Kim Andersen
    0

    Hi Fredrik

    Just to be sure, is the productImage-property located on the current page or on the child nodes, that you are iterating through?

    I guess that it's on the child nodes, but is that correct?

    If that's the case, you should change your variable from this:

    <xsl:variable name="mediaId" select="number($currentPage/productimage)" />

    to this:

    <xsl:variable name="mediaId" select="number(./productimage)" />

    otherwise the code will try to grab the media id from the current page...

    /Kim A

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Oct 25, 2010 @ 03:36
    Tom Fulton
    0

    Kim is right..sorry Frederik, I am blind!

  • Fredrik 89 posts 108 karma points
    Oct 25, 2010 @ 08:57
    Fredrik
    0

    It works finally!

  • Kim Andersen 1447 posts 2196 karma points MVP
    Oct 25, 2010 @ 19:12
    Kim Andersen
    0

    Great to hear Fredrik! Glad I could help.

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft