【问题标题】:Typoscript: how do I add a parameter to all links in the RTE?Typoscript:如何为 RTE 中的所有链接添加参数?
【发布时间】:2016-07-15 14:54:38
【问题描述】:

我想为用户在 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 是否已经有查询字符串?谢谢!

【问题讨论】:

    标签: typo3 typoscript typolink


    【解决方案1】:

    这将是解决方案:

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

    请注意,它必须以 & 开头,错字3 然后生成有效链接。如果相应配置,链接中的参数也将使用 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 变量

    【讨论】:

    • 对不起,这似乎不起作用。它不会更改在 RTE 中输入的链接中的任何内容。例如,domain.com/blah 保持不变,domain.com/blah?id=200 也保持不变。我的参数永远不会被添加。感谢您的考虑。
    • 带有附加参数的解决方案仅适用于内部链接,因此我添加了关于如何处理外部链接的更详细的评论。我很肯定它只能使用 TypoScript 存档,但使用 userFunc 会花费更多时间
    • 感谢您为回答我的问题所做的努力。我有点困惑 Typoscript 没有提供一种干净简单的方法来测试字符串中的字符并相应地采取行动,哦,好吧......再次感谢您的输入!
    【解决方案2】:

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

    【讨论】:

    • 谢谢安德鲁。不幸的是,我不明白如何实现这一点,因为我不知道如何测试“typolink contains character X”。我见过的所有示例似乎都使用了精确的字符串匹配。如果你能提供一个代码示例,那就太棒了。谢谢!
    【解决方案3】:

    对于任何感兴趣的人,这里有一个使用 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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-04
      • 2013-08-06
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 2020-12-08
      相关资源
      最近更新 更多