简体   繁体   English

如何在 TYPO3 10 中以自定义 extbase 扩展的 flexform 创建文件上传字段?

[英]How to create a file upload field in flexform of a custom extbase extension in TYPO3 10?

I tried to create a file upload field in flexform of an extbase extension in TYPO3 10. since the internal_type "file" is not supported in TYPO3 10, I tried the below given code.我尝试在 TYPO3 10 中以 extbase 扩展的 flexform 创建文件上传字段。由于 TYPO3 10 不支持 internal_type “文件”,因此我尝试了以下给定代码。

<settings.bgImage>
    <TCEforms>
        <label>Background Image</label>
        <config>
            <type>inline</type>
            <maxitems>1</maxitems>
            <foreign_table>sys_file_reference</foreign_table>
            <!--<foreign_field>uid_foreign</foreign_field>-->
            <foreign_table_field>tablenames</foreign_table_field>
            <foreign_label>uid_local</foreign_label>
            <foreign_sortby>sorting_foreign</foreign_sortby>
            <foreign_selector>uid_local</foreign_selector>
            <foreign_selector_fieldTcaOverride type="array">
            <config>
                <appearance>
                    <elementBrowserType>file</elementBrowserType>
                    <elementBrowserAllowed>jpg,jpeg,png,svg</elementBrowserAllowed>
                </appearance>
            </config>
            </foreign_selector_fieldTcaOverride>
            <foreign_match_fields type="array">
                <fieldname>image</fieldname>
            </foreign_match_fields>
            <appearance type="array">
                <newRecordLinkAddTitle>1</newRecordLinkAddTitle>
                <createNewRelationLinkTitle>Add Image</createNewRelationLinkTitle>
                <headerThumbnail>
                    <field>uid_local</field>
                    <height>64</height>
                    <width>64</width>
                </headerThumbnail>
            </appearance>
        </config>
    </TCEforms>
  </settings.bgImage>

But this is also not working correctly.但这也无法正常工作。 please help me to fix this.请帮我解决这个问题。 Thank you谢谢

Here is a currently working complete FlexForm with only a single FAL-Image field.这是一个当前工作的完整 FlexForm,只有一个 FAL-Image 字段。 The config has changed again... :-(配置再次更改... :-(

FlexForm Example FlexForm 示例

    <T3DataStructure>
        <sheets>
            <sDEF>
                <ROOT>
                    <TCEforms>
                        <sheetTitle>Example 1</sheetTitle>
                    </TCEforms>
                    <type>array</type>
                    <el>
                        <!-- example of a working fal image -->
                        <images>
                            <label>FAL-Images</label>
                            <config>
                                <type>inline</type>
                                <foreign_table>sys_file_reference</foreign_table>
                                <foreign_field>uid_foreign</foreign_field>
                                <foreign_sortby>sorting_foreign</foreign_sortby>
                                <foreign_table_field>tablenames</foreign_table_field>
                                <foreign_match_fields>
                                    <!-- this will be stored in sys_file_reference.fieldname -->
                                    <fieldname>image</fieldname>
                                </foreign_match_fields>
                                <foreign_label>uid_local</foreign_label>
                                <foreign_selector>uid_local</foreign_selector>
                                <overrideChildTca>
                                    <columns>
                                        <uid_local>
                                            <config>
                                                <appearance>
                                                    <elementBrowserType>file</elementBrowserType>
                                                    <elementBrowserAllowed></elementBrowserAllowed>
                                                </appearance>
                                            </config>
                                        </uid_local>
                                    </columns>
                                </overrideChildTca>
                                <filter>
                                    <userFunc>TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter->filterInlineChildren</userFunc>
                                    <parameters>
                                        <allowedFileExtensions></allowedFileExtensions>
                                        <disallowedFileExtensions></disallowedFileExtensions>
                                    </parameters>
                                </filter>
                                <appearance>
                                    <useSortable>1</useSortable>
                                    <headerThumbnail>
                                        <field>uid_local</field>
                                        <width>45</width>
                                        <height>45c</height>
                                    </headerThumbnail>
                                    <enabledControls>
                                        <info>1</info>
                                        <new>0</new>
                                        <dragdrop>1</dragdrop>
                                        <sort>0</sort>
                                        <hide>1</hide>
                                        <delete>1</delete>
                                    </enabledControls>
                                </appearance>
                            </config>
                        </images>
                        <!-- end -->
                    </el>
                </ROOT>
            </sDEF>
        </sheets>
    </T3DataStructure>

Update: I'll show here a ViewHelper and how to use it...更新:我将在这里展示一个 ViewHelper 以及如何使用它......

Template Example模板示例

{namespace t=Your\Extension\ViewHelpers}

<f:for each="{t:FAL(uid=entry.uid, field='image', table='tt_content')}" as="preview">
    <div class="preview">
        <f:image src="{f:uri.image(image=preview)}" title="{preview.title}" />
        <figcaption>{preview.title} {preview.description}</figcaption>
    </div>
</f:for>

ViewHelper Example ViewHelper 示例

<?php
namespace Your\Extension\ViewHelpers;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class FALViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    public function initializeArguments()
    {
        $this->registerArgument('table', 'string', '', false);
        $this->registerArgument('field', 'string', '', true);
        $this->registerArgument('uid', 'integer', '', true);
    }
    public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
    {
        $resFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
        $table = $arguments['table'] != NULL ? $arguments['table'] : 'tt_content';
        $field = $arguments['field'];
        $uid = intval($arguments['uid']);

        $fileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\FileRepository::class);
        $fileObjects = $fileRepository->findByRelation($table, $field, $uid);

        return $fileObjects;
    }
}

Explanation解释

The images are resolved thru the table sys_file_reference.图像通过表 sys_file_reference 解析。

When you take a look at it, you will find, that these fields will be filled with your extensions data: - tablenames, - fieldname and - uid_foreign当您查看它时,您会发现这些字段将填充您的扩展数据:- tablenames,- fieldname 和 - uid_foreign

A flexform field will probably have tt_content as tablenames , image as fieldname and the uid of your extensions tt_content record as uid_foreign .一个 flexform 字段可能tt_content作为tablenames ,将image作为fieldname ,并将扩展 tt_content 记录的 uid 作为uid_foreign

The flexform defines the <fieldname>image</fieldname> ... this will become the fieldname . flexform 定义了<fieldname>image</fieldname> ...这将成为fieldname

The template must tell the ViewHelper what to look for: {t:FAL(uid=entry.uid, field='image', table='tt_content')}模板必须告诉 ViewHelper 要查找的内容: {t:FAL(uid=entry.uid, field='image', table='tt_content')}

  • The entry.uid must match your content element's uid. entry.uid 必须与您的内容元素的 uid 匹配。
  • The field='image' must match the FlexForm fieldname. field='image' 必须与 FlexForm 字段名匹配。
  • The table must match the table where the data is stored in. (Here: tt_content)该表必须与存储数据的表匹配。(此处:tt_content)

If you are using it in an extension you will need to change the tablename to your tx_yourextension_whatever .如果您在扩展中使用它,则需要将表名更改为您的tx_yourextension_whatever You will probably change the fieldname as well...您可能还会更改字段名...

Note笔记

This code still seems to produce some deprecation warnings... I havn't yet figured out, how to overcome these:-/这段代码似乎仍然会产生一些弃用警告......我还没有弄清楚,如何克服这些:-/

Thanks for the code.感谢您的代码。 I've been looking for a solution here for a while myself.我自己在这里寻找解决方案已有一段时间了。

Unfortunately, the wrong image is displayed in my template.不幸的是,我的模板中显示了错误的图像。 The output in my fluid template is like this:我的流体模板中的output是这样的:

{f:uri.image(src:'{data.flexform_images}', treatIdAsReference:'1')}

What's wrong?怎么了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM