absolute links in wiziwig content
We have a few pages in the CMS that are used for emails and/or inside other software so unless the user inputs absolute links, most links are not absolute and therefore broken. I tackled that issue today :)
I did some crazy stuff but it works...
#set ($content = $_SerializerTool.serialize($data.getChild("content"), true))
#set ($content2 = $content.replaceAll('<', 'ltsign123').replaceAll('>', 'gtsign123').replaceAll(',', 'comma123').replaceAll('href="', 'linkstart123'))
#set ($linksets = $content2.split("ltsign123a "))
#set ($linked = [])
#foreach ($linkset in $linksets)
#if($linkset.contains("[.]") || $linkset.contains(";") || $linkset.contains(".html") || $linkset.contains(".pdf") || $linkset.contains(".doc"))
#set ($addls = $linked.add($linkset))
#else
#set ($linkset1 = $linkset.replaceFirst('"','.html"'))
#set ($addls = $linked.add($linkset1))
#end
#end
#set ($linked1 = $linked.toString())
#set ($linked2 = $linked1.replaceAll(',','').replace("[","").replace("]",""))
#set ($linked3 = $linked2.replaceAll('site://Secure Site/','https://www.nova.edu/'))
#set ($linked4 = $linked3.replaceAll('ltsign123','<').replaceAll('gtsign123','>').replaceAll('comma123',',').replaceAll('linkstart123','<a href="').replaceAll('site://$currentPageSiteName/',$cpath))
$linked4
$spath is set in an import that checks the current site name and sets the url...
the if contains line messes me up, if I don't specify .html, .doc, .pdf SOMETIMES it replaces on lines that shouldn't... weird
when you get a chance can you let me know what you think? any recommendations to clean up the code.
thanks in advance!
Discussions are closed to public comments.
If you need help with Cascade CMS please
start a new discussion.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Nando on 09 May, 2016 01:24 PM
there was an issue with the code above, this code works but we a specific issue I can't seem to resolve:
when setting $linked2 I'd like to use $currentPageSiteName instead of manually setting the site name but every time I use $currentPageSiteName nothing gets replaced...
thoughts?
2 Posted by Ryan Griffith on 09 May, 2016 02:22 PM
Hi Nando,
Curious, but I'm not seeing
$cpath
being initialized anywhere. If you adjust that does using$currentPageSiteName
work?Thanks!
3 Posted by Nando on 09 May, 2016 02:26 PM
$cpath is initialized in a import above this code (not shown - sorry)
it populates fine and so does $currentPageSiteName - it just doesn't see to find the end result of $currentPageSiteName to replace...
4 Posted by Ryan Griffith on 09 May, 2016 02:56 PM
Got it, thanks Nando.
I believe I may have ran into a similar issue in the past where I was not able to use a variable within the first argument in the
replaceAll()
. Perhaps try something like:Let me know if this adjustment helps at all.
Thanks!
5 Posted by Nando on 09 May, 2016 03:03 PM
no go :(
I also tried:
6 Posted by Ryan Griffith on 09 May, 2016 03:28 PM
What about
replaceAll($currentPageSiteName, $cpath)
?7 Posted by Nando on 09 May, 2016 03:31 PM
nope :(
An error occurred while rendering asset preview: Invocation of method 'replaceAll' in class java.lang.String threw exception java.lang.NullPointerException at templateValidation[line 20, column 103]
8 Posted by Ryan Griffith on 09 May, 2016 05:38 PM
Thank you for trying, Nando.
I believe the issue here is that
$currentPageSiteName
is a variable that is registered at run time as opposed to something like a static string. So when thereplaceAll
is called the variable is null, which explains the NullPointerException.Let me keep playing with this a bit to see if there's something else we can do.
Please let me know if you have any questions.
Thanks!
9 Posted by Ryan Griffith on 09 May, 2016 06:03 PM
Hi Nando,
I believe I was able to come up with a possible solution using the String Tool. I've created a macro which basically does a "string before" and "string after" replacement. Here is the code:
Using your Format as an example, you'd call this macro like:
Please let me know if you have any questions.
Thanks!
10 Posted by Nando on 09 May, 2016 06:20 PM
NICE!! that worked great!
few questions:
when setting variables, using the same name is ok? just overwritten?
the if contains line still messes me up, if I don't specify .html, .doc, .pdf SOMETIMES it replaces on lines that shouldn't... thoughts?
in general, what are your thoughts on this for outputting absolute links when publishing but still using system links inside?
thanks again!!!
11 Posted by Ryan Griffith on 09 May, 2016 07:36 PM
Thank you for following up, Nando. I am glad to hear the proposed solution did the trick.
Correct, the variable will be overwritten.
I'm having a tough time following the code without some context. Can you explain this piece a bit more?
Generally, clients will append a static domain or use the Site name (which represents the URL) when building out the links, so this is not too far fetched. Using a combination of
[system-view:external]
and[system-view:internal]
to generate different links for preview and publish would be recommended so you can still link to pages within Cascade.Please let me know if you have any questions.
Thanks!
12 Posted by Nando on 09 May, 2016 08:28 PM
Gotcha.
actually, your previous fix gave me an idea, is there way for me to check if a variable contains a specific character BEFORE a specific character?
for example, I would like to check if the variable has a
.
or a;
before the first"
13 Posted by Nando on 09 May, 2016 08:57 PM
trying something like:
but it gets stuck because its breaking elements I guess...
14 Posted by Ryan Griffith on 09 May, 2016 09:01 PM
Hm, are you seeing any errors, or is it just not getting into that conditional? Curious, but do you need
[.]
, or can it just be.
? The former looks more like you're trying to perform a regular expression match, whichcontains()
does not do.Please let me know if you have any questions.
Thanks!
15 Posted by Nando on 11 May, 2016 01:12 PM
You were right about
[.]
:)The big issue I was having here is when I would split the one value into multiple values I would get a "element must be terminated by the matching end-tag" error. Which I get bc the split values were a mix of content missing start/end elements BUT it would've all worked out in the end... no?
to get around that, I replaced all <, >, a href, etc so that I can split them without issue... this is my latest code:
Nando closed this discussion on 16 May, 2016 12:40 PM.