Copied to clipboard

Flag this post as spam?

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


  • Paulo Paiva 7 posts 28 karma points
    May 17, 2010 @ 20:21
    Paulo Paiva
    0

    Get media file by name

    Hello,

    I'm trying to build one macro that returns the path of one media library file. The name of the media is constructed inside the macro, depending on some params.

    After having the name of the image I need to get the mediaId, so I can use inside the GetMedia function.

    <xsl:value-of select="umbraco.library:GetMedia($mediaId, 'false')/data [@alias = 'umbracoFile']"/>

    I can't get the mediaId having the name of the image.

    I've tryed

    <xsl:variable name="mediaId" select="./data [@alias = 'ImageName']" />

    If the image were in the Content section, the previous line would work. But in the Media section it doesn't work..

    Nothing seems to work. Am i missing something? Is there another way to do this?

     

    Thanks for your help

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    May 17, 2010 @ 21:08
    Jan Skovgaard
    0

    Hi Paulo

    To get the name of the file you need to just ask for the @nodeName property.

    You can get the image name by writing this

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

    /Jan

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 17, 2010 @ 21:08
    Dirk De Grave
    0

    Paulo,

    It's something that just won't work. Even if you'd write some xslt extension to iterate all media items, you may end up getting the wrong media item, as multiple media items can have the same name, and even the same filename... so it's a no-go imo.

    Can you elaborate on your context for trying to get the id based on name of media item? Maybe other solutions may be more efficient...

     

    Cheers,

    /Dirk

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 17, 2010 @ 21:10
    Dirk De Grave
    0

    @Jan: that's exactly the point, he doesn't know the media id until he gets it based on the "name" (filename?) of the media item

     

    Cheers,

    /Dirk

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    May 17, 2010 @ 21:12
    Jan Skovgaard
    0

    Oh sorry, my bad! :-)

    /Jan

  • Paulo Paiva 7 posts 28 karma points
    May 18, 2010 @ 12:07
    Paulo Paiva
    0

    Ok, this is the scenario I need to get the media.

    I'm building one 1-1 multilingue site. This site has some images that contains labels. I want to switch the image to the approprieted language.

    In my template, i choose the media in the default language than, depending on the selected language, I change the name of the image (All images are in the same folder) - Lets see the example:

    I have this 3 images:

    • Media > Labels > label1
    • Media > Labels > label1_en
    • Media > Labels > label1_es

    The first one is the default image, the others are the translated versions of the same label. I have the MediaID for the first image, but want to display the image, depending of the selected language (passed by querystring).

    I use this code tho build the mediaName for the image I want to load, where mediaPropId is the ID of the default image:

    <xsl:variable name="mediaName" select="concat(umbraco.library:GetMedia($mediaPropId, 'false')/@nodeName, $flang)" /> 

    So, refrasing the question, having one image (that we have the mediaId), can we get another image in the same folder with that image name?

     

    Thanks again.

  • Paulo Paiva 7 posts 28 karma points
    May 19, 2010 @ 19:02
    Paulo Paiva
    0

    I went the route of getting the folder of the image, and then go over all images to see if they are translated. (each folder only contains 20/30 images).

    I come up with this code, but is still not working :(

    <!-- Source media ID parameter -->
    <xsl:variable name="mediaPropId" select="/macro/MediaId/node/@id" /> 
    
    <xsl:template match="/">
        <xsl:if test="$mediaPropId != '' ">
    
        <!-- Target Media Name -->
        <xsl:variable name="mediaName" select="concat(umbraco.library:GetMedia($mediaPropId, 'false')/@nodeName, $flang)" /> 
        <xsl:variable name="sourceImageNode" select="umbraco.library:GetMedia($mediaPropId, 'false')" /> 
    
        <!-- Source Media Gallery ID (folder) -->
        <xsl:variable name="imageFolder" select="$sourceImageNode/@parentID"/>
    
        <xsl:if test="$imageFolder &gt; 0">
            <!-- Target Media --> 
    
            <xsl:variable name="targetImage">
                    <xsl:call-template name="GetImage">
                            <xsl:with-param name="FolderId" select="$imageFolder"/>
                            <xsl:with-param name="MediaName" select="$mediaName"/>
                    </xsl:call-template>
            </xsl:variable>
    
            <!-- if empty, choose source Image. Otherwhise, choose target -->
            <xsl:choose>
            <xsl:when test="$targetImage = ''">
                <xsl:value-of select="$sourceImageNode/data [@alias = 'umbracoFile']"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$targetImage/data [@alias = 'umbracoFile']"/>       
            </xsl:otherwise>
            </xsl:choose>
    
        </xsl:if>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="GetImage">
        <xsl:param name="FolderId" select="."/>
        <xsl:param name="MediaName" select="."/>
    
        <xsl:variable name="imagesChilds" select="umbraco.library:GetMedia($FolderId, 1)" />
    
        <xsl:if test="count($imagesChilds/node) &gt; 0">
            <xsl:for-each select="$imagesChilds/node">
                <xsl:if test="@nodeName = $MediaName">
                    <xsl:value-of select="/data [@alias = 'umbracoFile']" />
                </xsl:if>
            </xsl:for-each>
        </xsl:if>
    </xsl:template >
    

     

    The result of $targetImage is always empty. Some lights?

    Thanks in advanced.

  • Paulo Paiva 7 posts 28 karma points
    May 20, 2010 @ 13:01
    Paulo Paiva
    0

    I managed to fix this.

    I had to do this changes:

    In the GetImageTemplate, insted of

    <xsl:value-of select="/data [@alias = 'umbracoFile']" />

    It should be:

    <xsl:value-of select="./data [@alias = 'umbracoFile']" />

    And in the otherwise, insted of

    <xsl:value-of select="$targetImage/data [@alias = 'umbracoFile']"/>

    It should be:

    <xsl:value-of select="$targetImage" />

     

    Whith this, i can get the right image.

    Thanks to everybody for the help....

Please Sign in or register to post replies

Write your reply to:

Draft