I cannot for the life of me get the node ID of a media picker component. I have a macro setup with a parameter called "source" of type mediaCurrent. In my template, I have placed the macro and added the source attribute/value.
This outputs nothing. If I change the parameter type to text and change the select to "/macro/source" it correctly spits out the attribute's value. I have triple checked that "mastheadImage" matches the alias of the media picker. Any idea why I'm not getting the node ID?
I'm not sure exactly what you're trying to do, but I'd like to try and help....
as far as I know (which means I could be wrong): <umbraco:Macro Alias="Image" source="mastheadImage" runat="server"></umbraco:Macro> wont return a node or anything, it's just a way of passing a text string on to your macro
so <xsl:variable name="source" select="/macro/source/node/@id" /> won't work
and <xsl:variable name="source" select="/macro/source" /> will just give you back your text string.
maybe go into a bit more detail about what you're trying to do and then I can try and offer something a bit more solid to help you out.
From my research, I had the understanding that if a macro parameter is set to type "mediaCurrent" Umbraco would treat this as more than just text. I read it would look up the node information relating to the passed alias of a media picker. Or perhaps I misread altogether :)
My ultimate goal would be to have a macro that would display the image selected via a media picker to my template. I can do this if I hard-code the alias name into the macro, but I'd like it to be a bit more dynamic so I don't have to make a separate macro for each media picker control. This is why I was attempting to identify the control at the template level and pass it on to the macro. FYI, this is version 4.5.
I had a quick play and this is how it worked for me:
made a new macro, made a new parameter and set its alias to mediaCurrent and chose its type to also be MediaCurrent dropped my macro into a template and just chose any old media item (in this case 1202): <umbraco:Macro mediaCurrent="1202" Alias="MediaCurrent" runat="server">umbraco:Macro>
in my xslt for my macro I can now grab the source of the image by doing the following: <xsl:value-of select="/macro/mediaCurrent/Image/umbracoFile"/>
Your solution works, but I was hoping to avoid the need to use ID's. Essentially I'd just like to specify the alias of the media picker (as specified in the document type).
I'm unsure if this is actually possible or if there is a way to get an ID from an alias.
Side note -- if the above isn't possible, is there an easy way to find the media ID?
So you have a mediapicker in your Document type, and through the content section you're choosing a media item right? your media pickers alias is called mastheadImage?
I don't think you need to pass any parameters to the macro in this case just render your macro:
<--a variable to target your mediapicker --> <xsl:variable name="mymediaPicker" select="mastheadImage"/> <--lets make sure its not empty --> <xsl:if test="$mymediaPicker!=''">
You are correct in your questions mentioned above. The problem with this approach is you are hard-coding the alias into the macro, which I don't want. I believe this would require me to make several different macros, one for each media picker.
If I can do something like the following to get an id based off an alias, I'd be home free, but it is either not possible or I don't know the correct syntax:
(Changed source parameter to type text)
<xsl:param name="currentPage"/>
<!-- get the alias name of the media picker as specified on template level --> <xsl:variable name="source" select="/macro/source" />
Below is the full solution below incase anyone arrives at this post in the future. Feel free to use since I think this really makes life a lot easier.
If your media picker alias is named something different than mine, no problem -- simply change the source value in the template. If you have multiple media pickers, no problem -- simply embed the macro multiple times and adjust the source value accordingly. No need to manually lookup ID's or create redundant macros.
Setup:
Create a XSLT & Macro named "Image"
Under the macro parameters of Image, add a new parameter with the name/alias "source" of type text (I also checked Show)
<!-- The source attribute value is the alias of the media picker as defined in the document type properties --> <umbraco:Macro Alias="Image" source="mastheadImage" runat="server">
Problems with mediaCurrent Parameter
I cannot for the life of me get the node ID of a media picker component. I have a macro setup with a parameter called "source" of type mediaCurrent. In my template, I have placed the macro and added the source attribute/value.
Template Call:
<umbraco:Macro Alias="Image" source="mastheadImage" runat="server"></umbraco:Macro>
XSLT:
<xsl:param name="currentPage"/>
<xsl:variable name="source" select="/macro/source/node/@id" />
<xsl:template match="/">
<xsl:value-of select="$source" />
</xsl:template>
This outputs nothing. If I change the parameter type to text and change the select to "/macro/source" it correctly spits out the attribute's value. I have triple checked that "mastheadImage" matches the alias of the media picker. Any idea why I'm not getting the node ID?
I'm not sure exactly what you're trying to do, but I'd like to try and help....
as far as I know (which means I could be wrong):
<umbraco:Macro Alias="Image" source="mastheadImage" runat="server"></umbraco:Macro>
wont return a node or anything, it's just a way of passing a text string on to your macro
so
<xsl:variable name="source" select="/macro/source/node/@id" />
won't work
and
<xsl:variable name="source" select="/macro/source" />
will just give you back your text string.
maybe go into a bit more detail about what you're trying to do
and then I can try and offer something a bit more solid to help you out.
Thanks for your reply.
From my research, I had the understanding that if a macro parameter is set to type "mediaCurrent" Umbraco would treat this as more than just text. I read it would look up the node information relating to the passed alias of a media picker. Or perhaps I misread altogether :)
My ultimate goal would be to have a macro that would display the image selected via a media picker to my template. I can do this if I hard-code the alias name into the macro, but I'd like it to be a bit more dynamic so I don't have to make a separate macro for each media picker control. This is why I was attempting to identify the control at the template level and pass it on to the macro. FYI, this is version 4.5.
Thanks for your help.
I think you're right.... after reading http://umbraco.org/documentation/books/macro-parameters-syntax , there's a bunch of stuff you can do with macro parameters that I never realised.
what do you from the following?
<xsl:copy-of select="/macro/source" />
does this return anything?:
<xsl:value-of select="/macro/source/node/umbracoFile" />
I'm gonna have a play around myself... If I discover something that may help you I'll post it here.
- Tim
I didn't have any luck with the following:
<xsl:copy-of select="/macro/source" />
<xsl:value-of select="/macro/source" />
<xsl:value-of select="/macro/source/node/umbracoFile" />
Out of desperation, I added a second parameter of type text and ran the following to see all macro data:
<xsl:value-of select="/macro"/>
It contained the value for the new text parameter but nothing for the mediaCurrent type.
This is mind-boggling but I appriciate the help.
I had a quick play and this is how it worked for me:
made a new macro, made a new parameter and set its alias to mediaCurrent and chose its type to also be MediaCurrent
dropped my macro into a template and just chose any old media item (in this case 1202):
<umbraco:Macro mediaCurrent="1202" Alias="MediaCurrent" runat="server">umbraco:Macro>
in my xslt for my macro I can now grab the source of the image by doing the following:
<xsl:value-of select="/macro/mediaCurrent/Image/umbracoFile"/>
hope that helps?
- Tim
Tim,
Your solution works, but I was hoping to avoid the need to use ID's. Essentially I'd just like to specify the alias of the media picker (as specified in the document type).
I'm unsure if this is actually possible or if there is a way to get an ID from an alias.
Side note -- if the above isn't possible, is there an easy way to find the media ID?
Thanks for your help!
So you have a mediapicker in your Document type, and through the content section you're choosing a media item right?
your media pickers alias is called mastheadImage?
I don't think you need to pass any parameters to the macro in this case
just render your macro:
<umbraco:Macro Alias="Image" runat="server">umbraco:Macro>
and then in your xslt:
which should render the media item you chose through the content section and the media item you chose.
or have I got it wrong?
maybe you want to do something trickier like this?:
http://www.delphicsage.com/home/blog.aspx/d=854/title=Code_Expressions_to_Programmaticify_Your_Umbraco_Site
You are correct in your questions mentioned above. The problem with this approach is you are hard-coding the alias into the macro, which I don't want. I believe this would require me to make several different macros, one for each media picker.
If I can do something like the following to get an id based off an alias, I'd be home free, but it is either not possible or I don't know the correct syntax:
(Changed source parameter to type text)
<xsl:param name="currentPage"/>
<!-- get the alias name of the media picker as specified on template level -->
<xsl:variable name="source" select="/macro/source" />
<xsl:template match="/">
<xsl:value-of select="$source" /> <!-- correctly outputs "mastheadImage" -->
<xsl:value-of select="$currentPage/mastheadImage" /> <!-- correctly gets me the id based off the alias, but this is hard-coded -->
<xsl:value-of select="$currentPage/$source" /> <!-- syntax error, but basically what i want -->
</xsl:template>
ok, in that case try this:
<xsl:value-of select="$currentPage/* [name() = $source and not(@isDoc)]" />
Yes Tim, thank you! I owe ya a beer.
Below is the full solution below incase anyone arrives at this post in the future. Feel free to use since I think this really makes life a lot easier.
If your media picker alias is named something different than mine, no problem -- simply change the source value in the template.
If you have multiple media pickers, no problem -- simply embed the macro multiple times and adjust the source value accordingly.
No need to manually lookup ID's or create redundant macros.
Setup:
XSLT Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="source" select="/macro/source" />
<xsl:template match="/">
<xsl:if test="$source != '' ">
<xsl:variable name="sourceID" select="number($currentPage/* [name() = $source and not(@isDoc)])" />
<xsl:if test="string($sourceID) != 'NaN' ">
<img>
<xsl:attribute name="src">
<xsl:value-of select="umbraco.library:GetMedia($sourceID, 'false')/umbracoFile" />
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="umbraco.library:GetMedia($sourceID, 'false')/@nodeName" />
</xsl:attribute>
</img>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Template Code (example):
<!-- The source attribute value is the alias of the media picker as defined in the document type properties -->
<umbraco:Macro Alias="Image" source="mastheadImage" runat="server">
is working on a reply...