【问题标题】:TYPO3 9.5 How to get File from stored FileReferenceTYPO3 9.5 如何从存储的 FileReference 中获取文件
【发布时间】:2019-07-31 11:15:16
【问题描述】:

所以我们有了 a21glossary 扩展名,我们用我们自己的扩展名 a21glossary_file 进行了扩展。在我们的扩展中,我们对扩展表进行 TCA 覆盖,并添加我们的文件字段,如下所示:

$fields = array(
    'tx_a21glossary_file_reference' => array(
        'label' => 'Winerap',
        'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
            'tx_a21glossary_file_reference',
            array(
                'foreign_types' => array(
                    \TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO => array(
                        'showitem' => '--palette--;;filePalette',
                    )
                ),
                'maxitems' => 1,
                'readOnly' => true,
                'foreign_match_fields' => [
                    'fieldname' => 'tx_a21glossary_file_reference',
                    'tablenames' => 'tx_a21glossary_main',
                    'table_local' => 'sys_file',
                ]
            ),
            'mp3'
        ),
    ),
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('tx_a21glossary_main', $fields);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
    'tx_a21glossary_main', // Table name
    '--palette--;Winerap;tx_a21glossary_file' // Field list to add
);

$GLOBALS['TCA']['tx_a21glossary_main']['palettes']['tx_a21glossary_file'] = array(
    'showitem' => 'tx_a21glossary_file_reference'
);

Domain/Model 也被我们的新字段的 getter 和 setter 扩展。

所以我们可以转到我们的词汇表页面并添加带有 FileReference 的列表元素,可以选择和归档。

我的问题是我如何以及在哪里可以获得视图层的实际文件,因为存储在数据库中的是 0 或 1。

如果您需要任何类型的信息,请发表评论,非常感谢您的帮助。

编辑:模型

namespace xxx\A21glossaryFile\Domain\Model;


use TYPO3\CMS\Extbase\Domain\Model\FileReference;

class Glossary extends \SveWap\A21glossary\Domain\Model\Glossary
{


    /**
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     * @cascade remove
     */
    protected $tx_a21glossary_file_reference = null;

    /**
     * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference
     */
    public function getTxA21glossaryFileReference(): FileReference
    {
        return $this->tx_a21glossary_file_reference;
    }

    /**
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $tx_a21glossary_file_reference
     */
    public function setTxA21glossaryFileReference(\TYPO3\CMS\Extbase\Domain\Model\FileReference $tx_a21glossary_file_reference)
    {
        $this->tx_a21glossary_file_reference = $tx_a21glossary_file_reference;
    }
}

在我们切换到模型之前,音频文件刚刚被该片段渲染,这已经完全过时了,对吧?现在我们使用模型并将字段数据传递给模板,我们只是在模板结构中使用它。

tx_a21glossary_main {

    60 = FILES
    60 {
        references {
            table = tx_a21glossary_main
            uid.data = uid
            fieldName = tx_a21glossary_file_reference
        }
        renderObj = TEXT
        renderObj {
            stdWrap.data = file:current:publicUrl
            stdWrap.wrap = <audio class="audio" controls><source src="|" type="audio/mpeg"/></audio>

        }
    }
}

【问题讨论】:

    标签: extbase typo3-9.x typo3-extensions


    【解决方案1】:

    我假设您的模型中有以下内容(我将字段命名为“图像”):

    /**
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     * @cascade remove
     */
    protected $image = null;
    
    /**
     * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     */
    public function getImage()
    {
        return $this->image;
    }
    
    /**
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     * @return void
     */
    public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image)
    {
        $this->image = $image;
    }
    

    如果提供了所有类型,那么只需像这样在流体中使用该属性:

    <f:image image="{item.image}" width="200c" height="200c" alt="Foo Bar"/>
    

    如果您需要从实际图像中获得更多信息,请这样做:

    <f:image image="{item.image}" width="200c" height="200c" alt="{item.image.originalResource.originalFile.alternative}"/>
    // or
    {item.image.originalResource.publicUrl}
    

    虽然docs 不是专门用于 v9,但那里的代码应该仍然有效。

    我猜的部分

    'foreign_match_fields' => [
        'fieldname' => 'tx_a21glossary_file_reference',
        'tablenames' => 'tx_a21glossary_main',
        'table_local' => 'sys_file',
    ]
    

    可以安全删除,这可能是您获得 0/1 的原因。与sys_file 的连接是默认连接,并使用中间表sys_file_reference 完成。因此,表中的列将始终只包含关系计数器,而不是 sys_file 的 uid(这是有意的)。您的 uid 和 sys_file uid 之间的连接存储在sys_file_reference。来自工作中的 v9 项目的示例 TCA:

    'image'            => [
        'label'  => 'FooBar Label',
        'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('image', [
            'maxitems'         => 1,
            'appearance'       => [
                'createNewRelationLinkTitle' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:images.addFileReference'
            ],
            'overrideChildTca' => [
                'types' => [
                    '0' => [
                        'showitem' => '
                            --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                            --palette--;;filePalette'
                    ],
                ]
            ]
        ], $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'])
    ],
    

    【讨论】:

    • 是否有可能我必须创建自己的 FileReference,我从 \TYPO3\CMS\Extbase\Domain\Model\FileReference 扩展并通过 $this->getOriginalResource 和其他东西在 gettern 中获取数据?目前模型输入正确,但视图中的字段仍然为空
    • 不,您不必这样做。使用默认的 extbase FileReference 很好。错误必须在其他地方。您的 TCA 是否在配置 => TCA 中的 BE 中正确显示?如果您可以在后端编辑记录(附加文件),那么您的模型类中可能有问题。
    • 是的,我可以毫无问题地创建带有音频文件的新词汇表条目
    • 我会将模型添加到问题中
    • protected $tx_a21glossary_file_reference = null; 我认为是错误的,因为下划线映射到驼峰式,所以protected $txA21glossaryFileReference = null; 应该是正确的。
    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多