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
    Apr 10, 2013 @ 03:39
    syn-rg
    0

    Media section: Display first published child node preview image

    I have a macro linking to the media section. My mediatype has a true/false property "published", so I can publish and unpublish the media item. Hiding it from the macro by unpublishing it.

    My macro lists all the media items with the media folder, and displays the first items preview image, as the preview image for the macro container on the front end.

    However, if the first media item is "unpublished" then no preview image is displayed.

    I need help, so that the first "published" media item within the list, has it's preview image displayed.

    My current code only displays the first item in the list, and unfortunately if the first item is "unpublished" then no preview image is displayed on the front end. I need it to check through the list of items and find the first "published" media item and display it's preview image.

    <xsl:if test="position()=1">
      <xsl:for-each select="AssetVersion[@nodeTypeAlias = 'AssetVersion' and string(published) = '1']">
        <xsl:sort select="versionNumber" data-type="number" order="descending"/>
        <xsl:if test="position()=1">
          <xsl:if test="./preview != ''">
            <a href="{./preview}" class="fancybox" title="{$heading}"> <img alt="{$heading}" title="{$heading}">
            <xsl:if test="$previewWidth !=''">
              <xsl:attribute name="width"> <xsl:value-of select="$previewWidth" /> </xsl:attribute>
            </xsl:if>
            <xsl:if test="$previewHeight !=''">
              <xsl:attribute name="height"> <xsl:value-of select="$previewHeight" /> </xsl:attribute>
            </xsl:if>
            <xsl:attribute name="src"> <xsl:value-of select="./preview" /> </xsl:attribute>
            </img> </a>
          </xsl:if>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>

    Cheers, JV.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Apr 10, 2013 @ 08:53
    Chriztian Steinmeier
    0

    Hi JV,

    Your problem is caused by the outermost xsl:if - it takes the first item, regardless of the child AssetVersion's published property...

    What is the context for the outermost xsl:if ? You need to perform a test there somehow, but which one, depends on how your structure looks; what's the name of the nodes that contain AssetVersion items?

    /Chriztian

  • syn-rg 282 posts 425 karma points
    Apr 11, 2013 @ 08:17
    syn-rg
    0

    Hi Chriztian,

    The name of the node that contains the "AssetVersion" is "Asset", which has a parent folder "AssetsFolder".
    I link to the "AssetsFolder" and it lists all the "Asset" media items, and each "Asset" contains an "AssetVersion".

    The "AssetVersion" has the "published" property type. If the "AssetVersion" is "unpublished" it doesn't display in the macro.

    So I need my XSLT to first check all the items in the "AssetsFolder" then find the first "published" "AssetVersion" and display it's preview image.

    Here's a snippet of my XSLT:

    <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
    <xsl:param name="currentPage" />
    <xsl:param name="mediafolder" select="/macro/mediaFolder/AssetsFolder/@id" />
    <xsl:param name="previewWidth" select="/macro/previewWidth" />
    <xsl:param name="previewHeight" select="/macro/previewHeight" />
    <xsl:param name="heading" select="/macro/heading" />
    <xsl:param name="number" select="/macro/number" />
    <xsl:variable name="numberOfPosts" select="1"/>
    <xsl:template match="/">
      <xsl:if test="$mediafolder !=''">
        <xsl:variable name="files" select="umbraco.library:GetMedia($mediafolder, true())" />
        <xsl:if test="$files != ''">
          <div class="download-container">
            <h4>
              <xsl:if test="/macro/number">
                <span class="redNumber">
                <xsl:copy-of select="/macro/number" />
                </span>
              </xsl:if>
              <xsl:copy-of select="/macro/heading" />
            </h4>
            <xsl:for-each select="$files/Asset">
              <xsl:if test="position()=1">
                <xsl:for-each select="AssetVersion[@nodeTypeAlias = 'AssetVersion' and string(published) = '1']">
                  <xsl:sort select="versionNumber" data-type="number" order="descending"/>
                  <xsl:if test="position()=1">
                    <xsl:if test="./preview != ''">
                      <a href="{./preview}" class="fancybox" title="{$heading}"> <img alt="{$heading}" title="{$heading}" />
                      <xsl:if test="$previewWidth !=''">
                        <xsl:attribute name="width"> <xsl:value-of select="$previewWidth" /> </xsl:attribute>
                      </xsl:if>
                      <xsl:if test="$previewHeight !=''">
                        <xsl:attribute name="height"> <xsl:value-of select="$previewHeight" /> </xsl:attribute>
                      </xsl:if>
                      <xsl:attribute name="src"> <xsl:value-of select="./preview" /> </xsl:attribute>
                      </img> </a>
                    </xsl:if>
                  </xsl:if>
                </xsl:for-each>
              </xsl:if>
            </xsl:for-each>
          </div>
        </xsl:if>
      </xsl:if>
    </xsl:template>
    </xsl:stylesheet> 
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Apr 11, 2013 @ 09:18
    Chriztian Steinmeier
    0

    Hi JV,

    Great - here's how I would approach it - feel free to ask about any specifics, if you like:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:param name="mediafolder" select="/macro/mediaFolder/AssetsFolder/@id" />
        <xsl:param name="previewWidth" select="/macro/previewWidth" />
        <xsl:param name="previewHeight" select="/macro/previewHeight" />
        <xsl:param name="heading" select="/macro/heading" />
        <xsl:param name="number" select="/macro/number" />
        <xsl:variable name="numberOfPosts" select="1"/>
    
        <xsl:template match="/">
            <xsl:if test="normalize-space($mediaFolder)">
                <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaFolder, true())" />
                <xsl:apply-templates select="$mediaNode[not(error)]" />
            </xsl:if>
        </xsl:template>
    
        <xsl:template match="AssetsFolder">
            <div class="download-container">
                <h4>
                    <xsl:if test="$number">
                        <span class="redNumber">
                            <xsl:value-of select="$number" />
                        </span>
                    </xsl:if>
                    <xsl:value-of select="$heading" />
                </h4>
                <!-- Process the first Asset that has at least one published AssetVersion --> <xsl:apply-templates select="Asset[AssetVersion[published = 1]][1]" />
            </div>
        </xsl:template>
    
        <xsl:template match="Asset">
            <xsl:for-each select="AssetVersion[published = 1]">
                <xsl:sort select="versionNumber" data-type="number" order="descending" />
                <xsl:if test="position() = 1">
                    <!-- Process this with the AssetVersion template -->
                    <xsl:apply-templates select="." />
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
    
        <xsl:template match="AssetVersion">
            <a href="{preview}" class="fancybox" title="{$heading}">
                <img src="{preview}" alt="{$heading}" title="{$heading}" />
                    <xsl:if test="normalize-space($previewWidth)">
                        <xsl:attribute name="width"><xsl:value-of select="$previewWidth" /></xsl:attribute>
                    </xsl:if>
                    <xsl:if test="normalize-space($previewHeight)">
                        <xsl:attribute name="height"><xsl:value-of select="$previewHeight" /></xsl:attribute>
                    </xsl:if>
                </img>
            </a>
        </xsl:template>
    </xsl:stylesheet>
    

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft