I'm trying to append a string to the end of all rich text links. Is there an already existing plugin for this?
-
1I don't think there is a clean way to solve this without using/creating a plugin or at least a twig filter. Twig itself is not supposed to contain business logic and even so there are no build-in functions to attach string into a text at specific places. You can't "find" links in the Rich Text Field by default, so you'll need a regex or something like that. However within the last 4 Years I didn't create a single site without creating a custom plugin for it, so I don't see any reason to not use/create one– Robin SchambachCommented Feb 7, 2018 at 15:01
-
Thanks for your response @RobinSchambach. I figured I would have to use a plugin. I was told to try and not use one, only because when Craft 3 comes out and we switch to that, there's the concern as to whether any plugins we're using will be compatible, so we don't want to rely heavily on them.– MorganCommented Feb 7, 2018 at 15:11
-
I see. Thats makes sense but my suggestion would be to start with Craft 3 from the beginning and create your own plugins. It's far better to start to develop with Craft 3 instead of doing everything with Craft 2 and update afterwards, because there are twig token (includeJs/includeCssRessource and all these) that won't work anymore after your update. Since you'll probably have to change all your Rich Text Fields too because Craft3 does not come up with this field type by default you'll have to download the plugin anyway. Just as a hint. You'll have much more work with your way– Robin SchambachCommented Feb 7, 2018 at 15:48
-
Do you know of a way to append text to the end of a link? I have to do it the manual way, as that's they only method I can utilize for how we handle tracking links.– MorganCommented Feb 7, 2018 at 21:16
-
You can create a twig filter that searches all links in the text (regex) stackoverflow.com/questions/23366790/… and replace those values with the same value + the string you like to append w3schools.com/php/func_string_str_replace.asp– Robin SchambachCommented Feb 7, 2018 at 21:31
Add a comment
|
1 Answer
I found a way to use the RetconHTML plugin for what I wanted. I just utilized the Attr filter and it worked the way I wanted. I had to also remove the way Retcon would add spaces to appended strings.
{{ block.paragraph | retconAttr('a', { href : '?some_text' ~ entry.title|kebab }, false) }}
By including false
, it adds to the already existing href, rather than overwriting it.