2016-07-15 29 views
0

我想添加一个参数到全部用户在RTE中输入的链接。Typoscript:如何向RTE中的所有链接添加参数?

我最初的想法是这样:

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

因此,例如:

http://domain.com/mypage.php 

成为

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

这听起来不错 - 只要这个链接不已经有有一个查询字符串! 在这种情况下,我明明有两个问号结束的URL

因此,例如:

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

成为

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

有什么办法来增加我的参数与正确的语法,取决于URL是否已经有查询字符串?谢谢!

回答

0

对于任何感兴趣的人,这里有一个解决方案,使用Typoscript的replacement函数为我工作。希望这可以帮助。

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

测试是否“?”字符已经在URL中并追加“?”或“&”,然后附加您的键值对。有一个CASE object available in the TypoScript Reference,您可以根据自己的目的修改示例。

+0

谢谢安德鲁。不幸的是,我不明白如何实现这一点,因为我不知道如何测试“typolink包含字符X”。我见过的所有例子似乎都使用了精确的字符串匹配。如果你可以提供一个代码示例,那就太棒了。谢谢! – Kerans

1

这将是解决办法:

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

注意,它必须开始与&,TYPO3然后生成一个有效的链接。如果配置相应,链接中的参数也将使用realURL进行分析。

编辑:如文档中描述的上述解决方案仅适用于内部链接https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html

,对,我看到的所有环节的工作,唯一的解决方法是使用一个userFunc

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

然后,你需要创建一个PHP脚本,并在您的TS中包含:

includeLibs.rteScript = path/to/yourScript.php 

请记住,includeLibs已过时,所以如果你正在使用TYPO3 8.x中(也可能是7.3+),你将需要创建只有几个文件自定义扩展

<?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:我没有测试实际的PHP代码,因此它可以有一些错误。如果您有麻烦,请尝试调试$finalTagParts变量

+0

对不起,这似乎不起作用。它不会改变在RTE中输入的链接中的任何内容。例如,** http://domain.com/blah**保持完全一样,并且** http://domain.com/blah?id = 200 **也保持完全一样。我的参数永远不会被添加。感谢您的考虑。 – Kerans

+0

带有additionalParams的解决方案仅适用于内部链接,因此我已经添加了关于如何处理外部链接的更详细的评论。我确信它只能用TypoScript存档,但是需要更多的时间才能使用'userFunc' –

+0

感谢您为回答我的问题所做的努力。我有点困惑,Typoscript并没有提供一种简洁的方式来测试一个角色的字符串并采取相应的行动,呵呵......再次感谢您的输入! – Kerans

相关问题