Typoscript: how do I add a parameter to all links in the RTE?

1.7k Views Asked by At

I want to add a parameter to all links entered in the RTE by the user.

My initial idea was to do this:

lib.parseFunc_RTE.tags.link {
    typolink.parameter.append = TEXT
    typolink.parameter.append.value = ?flavor=lemon
}

So for example:

http://domain.com/mypage.php

becomes

http://domain.com/mypage.php?flavor=lemon

which sounds great -- as long as the link does not already have a query string! In that case, I obviously end up with two question marks in the URL

So for example:

http://domain.com/prefs.php?id=1234&unit=moon&qty=300

becomes

http://domain.com/prefs.php?id=1234&unit=moon&qty=300?flavor=lemon

Is there any way to add my parameter with the correct syntax, depending on whether the URL already has a query string or not? Thanks!

3

There are 3 best solutions below

0
Kerans On BEST ANSWER

For anyone interested, here's a solution that worked for me using the replacement function of Typoscript. Hope this helps.

lib.parseFunc_RTE.tags.link {

    # Start by "replacing" the whole URL by itself + our string
    # For example: http://domain.com/?id=100    becomes http://domain.com/?id=100?flavor=lemon
    # For example: http://domain.com/index.html becomes http://domain.com/index.html?flavor=lemon

    typolink.parameter.stdWrap.replacement.10 {

        #this matches the whole URL
        search = #^(.*)$#i

        # this replaces it with itself (${1}) + our string
        replace =${1}?flavor=lemon

        # in this case we want to use regular expressions
        useRegExp = 1
    }

    # After the first replacement is done, we simply replace
    # the first '?' by '?' and all others by '&'
    # the use of Option Split allow this
    typolink.parameter.stdWrap.replacement.20 {
        search = ?
        replace = ? || & || &
        useOptionSplitReplace = 1
    }

}
1
Andrew On

Test whether the "?" character is already in the URL and append either "?" or "&", then append your key-value pair. There's a CASE object available in the TypoScript Reference, with an example you can modify for your purpose.

3
Dimitri L. On

That would be the solution:

lib.parseFunc_RTE.tags.link {
    typolink.additionalParams = &flavor=lemon
}

Note that it has to start with an &, typo3 then generates a valid link. The parameter in the link also will be parsed with realURL if configured accordingly.

Edit: The above solution only works for internal links as described in the documentation https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html

The only solution that works for all links that I see is to use a userFunc

lib.parseFunc_RTE.tags.link {
    typolink.userFunc = user_addAdditionalParams
}

Then you need to create a php script and include in your TS with:

includeLibs.rteScript = path/to/yourScript.php

Keep in mind that includeLibs is outdated, so if you are using TYPO3 8.x (and probably 7.3+) you will need to create a custom extension with just a few files

<?php

function user_addAdditionalParams($finalTagParts) {
    // modify the url in $finalTagParts['url']
    // $finalTagParts['TYPE'] is an indication of link-kind: mailto, url, file, page, you can use it to check if you need to append the new params
    switch ($finalTagParts['TYPE']) {
        case 'url':
        case 'file':
            $parts = explode('#', $finalTagParts['url']);
            $finalTagParts['url'] = $parts[0] 
                . (strpos($parts[0], '?') === false ? '?' : '&') 
                . 'newParam=test&newParam=test2'
                . ($parts[1] ? '#' . $parts[1] : '');
        break;
    }
    return '<a href="' . $finalTagParts['url'] . '"' .
           $finalTagParts['targetParams'] .
           $finalTagParts['aTagParams'] . '>'
}

PS: i have not tested the actual php code, so it can have some errors. If you have troubles, try debugging the $finalTagParts variable