使用Fluid显示Extbase的FileReference

4
在一个extbase扩展中,我有一个FileReference对象。它最初是使用extension_builder创建的。 来自模型:
/**
 * apprenticeshipDocument
 *
 @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
 */
protected $apprenticeshipDocument = NULL;

在前端,<f:debug>{institution.apprenticeshipDocument}</f:debug> 给我这个:

Extbase Var Dump

第一件事:缺少 originalResource

第二件事:直接调用 {institution.apprenticeshipDocument.uidLocal},值为NULL!尽管上面说是450。

第三件事:假设我们能够获得对应于 sys_file 中 uid 的 uidLocal。

可搜索的解决方案:

<f:link.page pageUid="{f:uri.image(src:450)}" target="_blank">Text</f:link.page>

这里的链接指向PDF文件的渲染GIF,而不是PDF文件本身。 我想做的就是在链接中输出文件路径(sys_file.identifier)... 必须有一种方法,不是吗?

编辑:Jost提供的解决方案:

<f:if condition="{institution.apprenticeshipDocument}">
    <li>
    <f:link.page pageUid="{institution.apprenticeshipDocument.originalResource.publicUrl}" target="_blank">Text</f:link.page>
    </li>
</f:if>

也许与 https://forge.typo3.org/issues/48965 有关?但我使用的是6.2.12版本,该补丁似乎已经包含在内了。 - Urs
啊,刚刚发现了 https://forum.typo3.org/index.php/t/198281/ ,我会研究一下的。 - Urs
2
originalResource 是延迟加载的,所以它为 NULL 是正确的。只需访问它即可。我相信您也可以访问它,然后使用 f:debug 查看其所有字段。其中之一将是 originalFile,它也是延迟加载的。 - Jost
2
uidLocal 不可访问的原因可能是没有相应的 getter,所以 fluid 无法读取它。我通常使用 EXT:vhs 中的 v:link.typolink ViewHelper 来链接文件,你可以像这样使用它:<v:link.typolink configuration="{parameter: 'file:{variableContainingUid}'}>Linktext</v:link.typoscript>。或者,你可以使用 {institution.apprenticeshipDocument.originalResource.publicUrl} 来获取 URL(不确定确切的名称)。 - Jost
2
尝试使用文件(或引用)的publicUrl属性,而不是直接使用文件路径 - 在fileInterface中有一个getter方法可以获取它。此外,还有更多可用的属性,这些属性在调试输出中看不到,主要是元数据 - 它们使用魔法getter方法访问。 - Jost
显示剩余5条评论
1个回答

4

在Fluid中,文件和文件引用的行为与通常有些不同:

  1. They are lazy-loaded, so the originalResource (Extbase file reference) and originalFile (Core file reference) values are NULL before the first access to them. But you can just access them as usual.
  2. The value uidLocal of an extbase file reference can't be accessed because there is no getter for it (as of TYPO3 6.2.12 and 7.2).
  3. To get the url of a file, use its publicUrl attribute, or use the ViewHelper v:link.typolink from EXT:vhs, like this:

    <v:link.typolink configuration="{parameter: 'file:{uid}'}>
        Linktext
    </v:link.typolink>
    

    uid is the id of the file in this case.

  4. Many properties of files (especially metadata-properties) are not saved as normal object attributes, but are internally saved into an associative array and then fetched using a magic getter. They are also lazy-loaded. Thus, they usually don't show up as separate properties in variable dumps, and may be not set at all or NULL (same as above).

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接