Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 25, 2011 @ 20:39
    Bo Damgaard Mortensen
    0

    List all media children (folders) from a media id - old scheme

    Hi all,

    I'm in a situation where I have to make a list of all media children folders of a specificed media folder using XSLT. The challenge for me is, that this umbraco solution is running 4.0.4.2 - the old XSLT scheme which I'm clueless about :/

    The $currentPage has got a mediapicker datatype which points to the root media folder, so I've found that I can get the id from that like this:

    <xsl:variable name="galleryId" select="number($currentPage/data [@alias = 'galleri'])" />
    <xsl:if test="$galleryId > 0">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($galleryId, 0)" />    
    </xsl:if>

    The above returns the root folder id as expected, but I honestly haven't got a clue how to list the children folders of this.

    Anyone know anything about this? :)

    All the best,

    Bo Mortensen

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 25, 2011 @ 20:49
    Bo Damgaard Mortensen
    0

    Hrmm, I've tried this one aswell, it seems to make more sense to me:

    <ul>
    <xsl:for-each select="umbraco.library:GetMedia(number($currentPage/data [@alias = 'galleri']), 'true')/node">
        <li>
            <xsl:value-of select="@nodeName" />
        </li>
    </xsl:for-each>
    </ul>

    However, I get the dreaded error: System.OverflowException: Value was either too large or too small for an Int32.

    To me, the number($currentPage/data [@alias = 'galleri']) *should* return the id of the root folder as an integer/a numer.. ?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 25, 2011 @ 20:52
    Bo Damgaard Mortensen
    0

    O.k it seemed to be a 'bug' in the old scheme, I just hit "Skip testing" and everything is running just as expected! ;) Now I just need to exclude a certain folder and I'm all set!

  • Donald St. Martin 83 posts 128 karma points
    Jan 25, 2011 @ 22:35
    Donald St. Martin
    0

    Bo,

    Have you considered something similar to this:

    <xsl:param name="mediaFolder" select="/macro/mediaFolder" />

    <!-- The value of mediaFolder is an XML snippet, where we need to select the id attribute from the XML node <node> -->
    <xsl:param name="mediaFolderID" select="$mediaFolder/node/@id" />

    <xsl:template match="/">
    <xsl:if test="$mediaFolderID !=''">
    <xsl:variable name="files" select="umbraco.library:GetMedia($mediaFolderID,1)" />
       
          <!-- For each XML node <node> where the nodeTypeAlias attribute = Folder -->
          <xsl:for-each select="$files//node [@nodeTypeAlias ='Folder']">
            <!-- Do stuff here -->
         </xsl:for-each>

    </xsl:template>

    The mediaFolder is a parameter in the macro so I can select which media folder I want it to start at. 

    --
    Donald

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 25, 2011 @ 23:43
    Chriztian Steinmeier
    0

    Hi Bo,

    Actually, the "Save" thing is not a "bug" per se - it's because Umbraco tries to run the transform to verify the validty of the XSLT, but it doesn't put values in the macro parameters so depending on how your code is set up, it calls GetMedia() with no @id...

    Anyways, here's a way to use real XSLT to do what you want to do - commented to help, but ask away if there's something you'd like to know about:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <xsl:template match="/">
            <!-- Process the 'galleri' property if it has a value -->
            <xsl:apply-templates select="$currentPage/data[@alias = 'galleri'][normalize-space()]" mode="media.all" />
        </xsl:template>
    
        <!-- Template for media that needs fetching - handles potential error -->
        <xsl:template match="*" mode="media.all">
            <xsl:variable name="mediaNode" select="umb:GetMedia(., true())" />
            <xsl:apply-templates select="$mediaNode[not(error)]" />
        </xsl:template>
    
    
        <!-- Template for a Media Folder -->
        <xsl:template match="node[@nodeTypeAlias = 'Folder']">
            <ul>
                <xsl:apply-templates select="node[@nodeTypeAlias = 'Image']" />
            </ul>
        </xsl:template>
    
        <!-- Template for a Media Image -->
        <xsl:template match="node[@nodeTypeAlias = 'Image']">
            <li>
                <a href="{data[@alias = 'umbracoFile']}">
                    <xsl:value-of select="@nodeName" />
                </a>
            </li>
        </xsl:template>
    
        <!-- Exclude a specific Folder -->
        <xsl:template match="node[@nodeTypeAlias = 'Folder'][@nodeName = 'Word files']" />
    
        <!-- - or a specific File/Image -->
        <xsl:template match="node[@nodeTypeAlias = 'Image'][@nodeName = 'My 80s hair style']" />
    
    </xsl:stylesheet>

    /Chriztian 

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 26, 2011 @ 22:27
    Bo Damgaard Mortensen
    0

    Hi Chriztian! Thanks a lot for your help :) appreciated!

    It looks a lot cleaner than the XSLT I made, so i'll go with that instead :) Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft