簡體   English   中英

如何在流體動力的typo3內容元素中使用自定義記錄

[英]How to use custom records in fluid powered typo3 content element

幾天前,我開始使用TYPO3 8.7.7,並使用Fluid提供動力的TYPO3擴展創建了一個小型網站。 現在,我想創建一個自定義內容元素,以顯示編輯器可以在后端管理的一些產品。 在擴展程序中,我為產品創建了一個簡單的模型:

namespace Something\Products\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class Product extends AbstractEntity
{

    protected $name;

    protected $description;

    public function __construct(string $name, string $description)
    { ... }

    public function getName(): string
    { ... }

    public function setName(string $name)
    { ... }

    public function getDescription(): string
    { ... }

    public function setDescription(string $description)
    { ... }
}

我還創建了一個ProductRepository類,該類僅擴展了\\ TYPO3 \\ CMS \\ Extbase \\ Persistence \\ Repository。

在EXT:something / Configuration / TCA / tx_product.php中

我有以下定義:

return [
    'ctrl' => [
        'title' => 'Products',
        'label' => 'name'
    ],

    'columns' => [
        'name' => [
            'label' => 'Name',
            'config' => [
                'type' => 'input',
                'eval' => 'trim,required',
            ],
        ],
        'description' => [
            'label' => 'Description',
            'config' => [
                'type' => 'text',
                'eval' => 'trim,required',
            ],
        ],
    ],

    'types' => [
        '0' => ['showitem' => 'name, description'],
    ],
];

在Web->列表模塊中,添加和編輯產品的效果很好。 我將它們存儲在頁面樹中名為“產品”的文件夾中。

我的目標是在任意列的頁面上創建一個名為Products的內容元素。 前端中的模板應顯示產品列表。

我希望在EXT:something / Resources / Private / Template / Content / Product.html中有類似的內容

<div xmlns="http://www.w3.org/1999/xhtml" lang="en"
    xmlns:f="http://typo3.org/ns/TYPO3/Fluid/ViewHelpers"
    xmlns:flux="http://typo3.org/ns/FluidTYPO3/Flux/ViewHelpers"
    xmlns:v="http://typo3.org/ns/FluidTYPO3/Vhs/ViewHelpers">

    <f:layout name="Content"/>

    <f:section name="Configuration">
        <flux:form id="newsitem" options="{group: 'Unicontrol'}">
            <flux:field.input name="settings.headline" required="true"/>
        </flux:form>
    </f:section>

    <f:section name="Main">
        <ul>
            <f:for each="{products}" as="product">
                <li>{product.name}</li>
            </f:for>
        </ul>
    </f:section>
</div>

現在我不知道該怎么辦。 我想我需要一個控制器類,但我不知道如何將其與我的Fluid供電TYPO3模板/部分連接。 誰能幫助我找到實現這一目標的方法?

在此先感謝您,祝您愉快。

編輯:

我當前的CE模板現在看起來像這樣:

<div xmlns="http://www.w3.org/1999/xhtml" lang="en"
    xmlns:f="http://typo3.org/ns/TYPO3/Fluid/ViewHelpers"
    xmlns:flux="http://typo3.org/ns/FluidTYPO3/Flux/ViewHelpers"
    xmlns:v="http://typo3.org/ns/FluidTYPO3/Vhs/ViewHelpers">

    <f:layout name="Content"/>

    <f:section name="Configuration">
        <flux:form id="products" options="{group: 'Unicontrol'}">
            <flux:field.relation name="settings.relation" table="tx_products_domain_model_product"
                                transform="TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage<Something\\Products\\Domain\\Model\\Product>"
                                multiple="true"/>
        </flux:form>
    </f:section>

    <f:section name="Main">
        <ul>
            <f:for each="{settings.relation}" as="product">
                <li>{product.name}</li>
            </f:for>
        </ul>
    </f:section>
</div>

現在,我可以在CE中選擇列表中的一種產品,但是在前端,它僅顯示一個元素,即我在后端中選擇的元素。 是否逃脫斜線都沒有關系。

通常,您通常希望為此使用一個實際的插件-原因有很多,但最重要的是,它將您的內容與交互式插件分開,並為每個插件提供了專用的自變量范圍。

也就是說,您可以將產品列表顯示為啟用Flux的元素。 它需要結合三件事:

  1. 您的模型的存儲庫必須存在(或不被視為聚合根。“產品”絕對聽起來像聚合根,因此非常合適!
  2. 在內容元素模板中,根據您要如何處理選擇內容,添加一個flux:field.relation.select.multiRelation 將此指向您的域模型的SQL表(並查看Flux ViewHelper文檔以獲取更多參數來指定選擇方式)
  3. 使用該ViewHelper的transform參數聲明最終類型應為TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage<Your\\Model\\ClassName> (請注意,您可能需要轉義反斜杠,例如TYPO3\\\\CMS\\\\... )。

然后完全按照模板“ Main部分中的顯示方式渲染產品列表。 在內部,Flux會轉換在字段中選擇的任何值或關系,讀取UID列表並使用您創建的存儲庫來選擇每個UID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM