检查流体中的TYPO3链接类型

问题描述 投票:1回答:3

我想在流体中呈现一个typolink,但我需要检查它是否是下载文件(t3:// file?uid = 1),页面链接(t3:// page?uid = 1)或外部链接(https://www.abc.de _blank)。是否有方法或视图检查流体中的链接类型?

我发现的只是通过typoscript或与VHS一样的hacky方式

<f:if condition="{target -> v:math.round()} > 0">

它适用于TYPO3 9.x.

typo3 fluid typo3-9.x
3个回答
1
投票
$linkService = $this->objectManager->get(LinkService::class);
$result = $linkService->resolve($linkValue);

这可以帮助您在自定义ViewHelper

$linkValue的可能值:

  • t3://page?uid=1 => [string(pageuid),'page']
  • [email protected] => [string(email),'email']
  • https://typo3.org => [string(url),'url']
  • t3://file?uid=226 => [TYPO3 \ CMS \ Core \ Resource \ File,'file']

$result返回一个数组。每个案例都有“类型”参数。根据类型,返回另一个值或对象。我在上面列出了这个。

该课程可从TYPO3版本8获得。


1
投票

您还可以使用vhs扩展程序检查linktype,例如设置不同的目标:

{namespace v=FluidTYPO3\Vhs\ViewHelpers}
...
<f:variable name="target">_self</f:variable> 
<v:condition.string.contains haystack="{url}" needle="t3://file?uid">
  <f:variable name="target">_blank</f:variable>         
</v:condition.string.contains>
<v:condition.string.contains haystack="{url}" needle="http">
  <f:variable name="target">_blank</f:variable>         
</v:condition.string.contains>
<v:condition.string.contains haystack="{url}" needle="www">
  <f:variable name="target">_blank</f:variable>
</v:condition.string.contains> 

<f:link.typolink parameter="{url}" target="{target}">the link</f:link.typolink>

0
投票

这是我现在使用的ViewHelper:

/**
 * A view helper for rendering the linktype
 *
 * = Examples =
 *
 * <code>
 * {nc:linkType(parameter: link)}
 * </code>
 * <output>
 * page, file, url, email, folder, unknown
 * </output>
 */
class LinkTypeViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    /**
     * Initialize arguments
     */
    public function initializeArguments()
    {
        $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
    }

    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string Linktype (page, file, url, email, folder, unknown)
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
    {
        $parameter = $arguments['parameter'];

        // workaround if parameter has _blank or other additional params
        $arr = explode(' ',trim($parameter));
        $firstparameter = $arr[0];

        $linkService = GeneralUtility::makeInstance(LinkService::class);
        $linkDetails = $linkService->resolve($firstparameter);

        return $linkDetails['type'];

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.