I can't wrap my head around this little feature :/
I'm selecting a body background image through a MediaPicker field. If no image has been picked i want to output a default image for the Media section (image id = 296).
So far i have this working IF an image has been picked for the current page.
If i navigate to a page where an image has NOT been picked, i get this with debug trace:
Værdien var enten for stor eller for lille til en Int32.
Værdien var enten for stor eller for lille til en Int32.
ved System.Convert.ToInt32(Double value)
ved System.Double.System.IConvertible.ToInt32(IFormatProvider provider)
ved System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
ved System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType)
ved System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
and so forth. The ol' int32 problem.
How do i go about getting the image with the ID of 296 shown as a default image until something else has been picked?
The problem is that Umbraco is running the GetMedia call even when bodyBy has no value, thus causing the error. To fix you need to check to make sure there's a value before you call GetMedia. This should do it:
This solution worked for what I wanted to do with a dynamic body tag, but I would like to extend it slightly. Instead of hard coding the mediaDefault variable to be a certain image, I need to call in the value in the parent folder (the toplevel page as shown below).
I think your code looks right, except in the recursive call I think you're using hte wrong element ('media' instead of 'backgroundImage'). Try this instead:
Also, no need to use GetXmlNodeById, you can jump straight into your Media stuff, using the same code as your first condition, but changing $mediaId to $parentID. You might also create a template so you can re-use that code.
I did try GetMedia first but it throws a System.OverflowException: Value was either too large or too small for an Int32.error. Your code does the same thing. Any ideas on why it might be doing that?
You rock! Thank you! Tom's solution worked in the end. All I had to do was change $mediaNode to $parentID and then change the level that it was searching in from 2 to 1 like the following, as I had made an error in where it was looking.
4.7.1 XSLT default image if none selected
Hi there
I can't wrap my head around this little feature :/
I'm selecting a body background image through a MediaPicker field. If no image has been picked i want to output a default image for the Media section (image id = 296).
So far i have this working IF an image has been picked for the current page.
If i navigate to a page where an image has NOT been picked, i get this with debug trace:
and so forth. The ol' int32 problem.
How do i go about getting the image with the ID of 296 shown as a default image until something else has been picked?
Thanks in advance
The imageroot variable is old leftovers, that i forgot to remove for this example.
Hi,
The problem is that Umbraco is running the GetMedia call even when bodyBy has no value, thus causing the error. To fix you need to check to make sure there's a value before you call GetMedia. This should do it:
HTH,
Tom
Hi Martin
Maybe something along the lines of:
I haven't tested the above - relying on the ol' grey matter...
Cheers
Nigel
Hi Nigel
I got around to this in the meantime, it looks close to yours and it seems to work, all i need now is the default value.
Thank you very much for your time and suggestion . i will give it a go if the above fails
Alright, so far so good.
I need to pass this value:
How do i construct a variable containg the above value so it would be inserted correctly into;
<body style="background:#f2eee9 url('<umbraco:Macro Alias="BodyBackground" runat="server"></umbraco:Macro>') no-repeat 50% 0;">
I know it's a bit ugly, but bear with me :-)
Oh! Sorry Tom, didn't see your post, it seems it's identical to what i found.
I guess i could integrate the body tag in the macro, i just think being able to pass the value is more...well....fun. Is it doable?
That should work, instead of the <img> tag you can just do an <xsl:value-of select="$mediaNode/umbracoFile"/> - that should just return the path
-Tom
Hi Tom
Idk if i understand you correctly, but try taking a look at this:
If i pick an image for any given currentpage my body tag looks like this - The WHEN part:
<body style="background:#f2eee9 url(/media/296/dusk.jpg) no-repeat 50% 0">
If i do not pick an image and want to fall back to the default background image value - The OTHERWISE part, i get this right now:
The value is passed, but it breaks the body tag somehow. How can i pass '/media/2967dusk.jpg' as a variable that'll work for this?
I know it's a bit akward but i hope it's clear what i'm trying to achieve.
JESUS CHRIST!
It didn't work because i was testing a childpage with the umbdebugtrace extension in the url. What the bloody hell :(((
Thanks to both of you for your kind assistance
Ok, to sum up what i did.
I'm setting my body background through an inline style that references a macro for the url path:
The macro where '/media/296/dusk.jpg' is the fallback image if no background image is chosen through a mediaPicker for the current page:
Testing a page without a picked background and using the ?umbdebugtrace="true" will break the body tag for whatever reason.
Hi guys,
This solution worked for what I wanted to do with a dynamic body tag, but I would like to extend it slightly. Instead of hard coding the mediaDefault variable to be a certain image, I need to call in the value in the parent folder (the toplevel page as shown below).
The structure is:
Home
Toplevel page
subpage
subpage
subpage
I've tried the following, but can't get it right.
Any help would be greatly appreciated.
Hi,
I think your code looks right, except in the recursive call I think you're using hte wrong element ('media' instead of 'backgroundImage'). Try this instead:
Also, no need to use GetXmlNodeById, you can jump straight into your Media stuff, using the same code as your first condition, but changing $mediaId to $parentID. You might also create a template so you can re-use that code.
I think the below piece should work:
Hope this helps,
Tom
Thanks for the help Tom.
I did try GetMedia first but it throws a System.OverflowException: Value was either too large or too small for an Int32. error. Your code does the same thing. Any ideas on why it might be doing that?
Oh, sorry, you need to wrap GetMedia in an if statement to make sure it gets passed a value:
-Tom
Hmm, This line keeps throwing an error.
Could it be because it is being called before the variable is defined?
Hi Wade
Having just looked at it briefly i think you need to to it like this:
and a space between "if" and "test" :-)
Sorry, yes, $mediaNode should be $parentId instead - copy/paste error :)
Hi guys.
You rock! Thank you! Tom's solution worked in the end. All I had to do was change $mediaNode to $parentID and then change the level that it was searching in from 2 to 1 like the following, as I had made an error in where it was looking.
Martin, thank you for your solution too, however the if test
kept throwing an error. Doesn't matter though as Tom's solotion worked.
The final code to show an image from a parent node if none is selected on the child node is:
is working on a reply...