digitalwerk/content-element-registry

此软件包已被弃用且不再维护。作者建议使用devskio/content-element-registry软件包。

内容元素注册表 - Typo3内容元素的注册助手

安装次数: 13,541

依赖关系: 0

建议者: 0

安全性: 0

星标: 1

关注者: 4

分支: 1

开放问题: 0

类型:typo3-cms-extension

12.0.5 2023-06-24 20:31 UTC

This package is auto-updated.

Last update: 2023-08-22 13:56:41 UTC


README

项目已迁移到另一个项目。

请在此项目中继续

Extension.svg?sanitize=true 内容元素注册表

Typo3 扩展简化了在Typo3方式中创建新的内容元素(CE)的过程

安装

通过composer composer req digitalwerk/content-element-registry 安装扩展,并在扩展模块中激活它

设置

激活扩展后,您必须定义内容元素配置类。有两种方法可以实现

  1. 在扩展配置中定义路径(即extConf)。可以包含以逗号分隔的目录路径列表 示例: EXT:your_ext_1/Classes/ContentElements/,EXT:your_ext_2/Classes/ContentElements/

  2. 在扩展的Services.yaml中注册监听器如下

  Vendor\Extension\EventListeners\ContentElementRegistryListener:
    tags:
      - name: event.listener
        identifier: 'contentElementRegistryListener'
        event: Digitalwerk\ContentElementRegistry\Events\ContentElementRegistryClassEvent

方法Vendor\Extension\EventListeners\ContentElementRegistryListener可能如下所示

<?php
declare(strict_types=1);
namespace Vendor\Extension\EventListeners;

class ContentElementRegistryListener
{
    /**
     * @param RegisterContentElementRegistryClassEvent $event
     */
    public function __invoke(RegisterContentElementRegistryClassEvent $event): void
    {
        $contentElementRegistry = $event->getContentElementRegistry();
        $contentElementsClassMap = \Composer\Autoload\ClassMapGenerator::createMap(
            \TYPO3\CMS\Core\Core\Environment::getPublicPath() .
            '/typo3conf/ext/your_extension/Classes/ContentElement/'
        );
        foreach ($contentElementsClassMap as $elementClass => $elementClassPath) {
            $contentElementRegistry->registerContentElement(
                \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($elementClass)
            );
        }
    }
}

创建新内容元素

要创建新的内容元素,您必须在设置部分中定义的文件夹内创建一个新的,该类扩展Digitalwerk\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem

<?php
namespace \YourVendor\YourExtension\ContentElement;

use Digitalwerk\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{

}

在清除Typo3缓存后,您现在应该可以在向导中看到新的内容元素

如您所见,内容元素没有标题也没有描述。这些是从扩展内部的locallang文件中自动获取和翻译的:EXT:your_extension/Resources/Private/Language/locallang_db.xlf。您现在可以定义您的CE标题和描述如下

<trans-unit id="tt_content.yourextension_yournewcontentelement.title">
    <source>Your new content element</source>
</trans-unit>
<trans-unit id="tt_content.yourextension_yournewcontentelement.description">
    <source>Your new content element description</source>
</trans-unit>
<trans-unit id="tt_content.yourextension_yournewcontentelement.palette.default">
    <source>Default palette</source>
</trans-unit>

当您添加此新CE时,它将仅包含默认CE字段:

添加CE字段

要添加新字段,您必须在 \YourVendor\YourExtension\ContentElement\YourNewContentElement 中定义它。

<?php
namespace \YourVendor\YourExtension\ContentElement;

use Digitalwerk\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{

    /**
     * YourNewContentElement constructor.
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->addPalette(
            'default',
            'header, --linebreak--, bodytext'
        );
    }
}

通过这种方式,我们定义了新的 CE 调色板,名称为 default,包含两个字段 headerbodytext

代码描述

  1. 每个 CE 的调色板名称必须唯一。调色板的标签可以在 locallang_db.xlf 中定义,使用以下密钥:tt_content.yourextension_yournewcontentelement.palette.default
  2. 字段定义语法必须遵循 TCA 调色板 showitem 语法
  3. 使用的字段必须在 tt_content TCA 中正确配置
  4. 您可以添加任意数量的调色板;()

我们的 CE 现在应该看起来像这样:

如果您需要覆盖字段配置,可以这样做:(在以下示例中,我们已为 bodytext 字段启用了富文本编辑器)

<?php
    /**
     * @return array
     */
    public function getColumnsOverrides()
    {
        return [
            'bodytext' => [
                'config' => [
                    'enableRichtext' => true,
                ],
            ],
        ];
    }

CE 模板

内容元素的模板路径必须在 typoscript 中按以下方式配置

lib.contentElement {
    layoutRootPaths {
        10 = EXT:your_extension/Resources/Private/Layouts
    }

    partialRootPaths {
        10 = EXT:your_extension/Resources/Private/Partials
    }

    templateRootPaths {
        10 = EXT:your_extension/Resources/Private/Templates/ContentElements
    }
}

模板名称与 CE 类名称匹配。例如,如果注册了类名为 YourNewContentElement 的 CE,则此模板必须存在 EXT:your_extension/Resources/Private/Templates/ContentElements/YourNewContentElement.html。模板的内容可以看起来像这样

<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
      xmlns:f="http://typo3.org/ns/TYPO3/Fluid/ViewHelpers"
      data-namespace-typo3-fluid="true">

<f:layout name="ContentElements/{contentElement.layout}" />

<f:section name="Main">
  ...
</f:section>

<f:section name="Preview">
  ...
</f:section>

</html>

您是否使用 <f:layout /><f:section /> 完全取决于您。您还可以添加用于 BE 预览的 <f:section name="Preview"> 部分。

CE 图标

如果您不想使用默认图标,可以更改它

  • 将图标添加到文件夹 EXT:your_extension/Resources/Public/Icons/ContentElement/yourextension_yournewcontentelement.svg

CE 域模型

模型名称与 CE 类名称匹配。例如,如果注册了类名为 YourNewContentElement 的 CE,则此模型可以在 EXT:your_extension/Classes/Domain/Model/YourNewContentElement.php 中存在。模型的内容可以看起来像这样

<?php
declare(strict_types=1);
namespace \YourVendor\YourExtension\Domain\Model\ContentElement;

use DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement;

/**
 * Class YourNewContentElement
 * @package YourVendor\YourExtension\Domain\Model\ContentElement
 */
class YourNewContentElement extends ContentElement
{

}

在类中,您可以编写一些 函数、获取器、设置器等。其中一些是从 DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement 继承的(看看吧;)。

模板中的模型

模型可以通过元素模板中的变量名称 {contentElement} 访问。要找出模板中模型加载了哪些数据,请使用:<f:debug>{contentElement}</f:debug>

创建新字段并设置

创建新字段

  1. 在表 ext_table.sql 中创建字段,例如 new_field
  2. 在 TCA EXT:your_extension/Configuration/TCA/Overrides/tt_content.php 中创建新列和配置 Typo3 列配置
<?php
defined('TYPO3') or die();

$ttContentNewColumns = [
    'new_field' => [
        'label' => 'New field',
        'config' => [
            'type' => 'input',
            'eval' => 'trim',
         ],
    ],
];

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('tt_content', $ttContentNewColumns);

设置新字段

  • 在类 \YourVendor\YourExtension\ContentElement\YourNewContentElement 中添加创建的字段 new_field
<?php
namespace \YourVendor\YourExtension\ContentElement;

use YourVendor\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{

    /**
     * YourNewContentElement constructor.
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->addPalette(
            'default',
            'new_field'
        );
    }
}
  • 在CE模型EXT:your_extension/Classes/Domain/Model/YourNewContentElement.php中添加创建字段的属性$newField。您必须遵循new_field的语法,例如在ext_table.php中写入,在模型中必须写入newField

具有IRRE的内容元素

映射

ext_typoscript_setup.typoscript关系表中映射到CE模型

      YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement\YourNewRelation {
        mapping {
         tableName = tx_contentelementregistry_domain_model_relation
         recordType = yourextension_yournewcontentelement_yournewrelation
        }
      }

注册关系图标

您可以使用Typo3 Icon API注册关系图标

关系TCA

在TCA EXT:your_extension/Configuration/TCA/Overrides/tx_contentelementregistry_domain_model_relation.php中创建

$tempTca = [
    'ctrl' => [
        'typeicon_classes' => [
            'yourextension_yournewcontentelement_yournewrelation' => 'yourextension_yournewcontentelement_yournewrelation',
        ],
    ],
    'types' => [
        'yourextension_yournewcontentelement_yournewrelation' => [
                    'showitem' => '--palette--;;mediaPalette,',
                ],
    ],
    'palettes' => [
        'mediaPalette' => [
            'showitem' => 'title, media',
        ],
    ],
    'columns' => [
        'title' => [
            'label' => 'Title',
            'config' => [
                'type' => 'text',
            ],
        ],

    ],
];

$GLOBALS['TCA']['tx_contentelementregistry_domain_model_relation'] = array_replace_recursive($GLOBALS['TCA']['tx_contentelementregistry_domain_model_relation'], $tempTca);

将关系添加到CE

将关系名称添加到调色板

<?php
namespace \YourVendor\YourExtension\ContentElement;

use YourVendor\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{
  public function __construct()
    {
        parent::__construct();
        $this->addPalette(
            'default',
            'tx_contentelementregistry_relations'
        );
    }
}

关系模型

  • 在CE模型中创建属性和获取器
<?php
namespace \YourVendor\YourExtension\Domain\Model\ContentElement;

use DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement;

/**
 * Class YourNewContentElement
 * @package YourVendor\YourExtension\Domain\Model\ContentElement
 */
class YourNewContentElement extends ContentElement
{
    /**
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement\YourNewRelation>
     */
    protected $relations = null;

    /**
     * @return ObjectStorage|null
     */
    public function getRelations(): ? ObjectStorage
    {
        return $this->relations;
    }

}
  • \YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement中创建新文件YourNewRelation.php。在这里,您可以编写TCA中关系字段的获取器和属性
<?php
namespace \YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement;

use Digitalwerk\ContentElementRegistry\Domain\Model\Relation;

/**
 * Class YourNewRelation
 * @package YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement
 */
class YourNewRelation extends Relation
{
    /**
     * @var string
     */
    protected $type = '';

    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

}

CE字段映射

在CE类中,您可以在需要将其用于模型时映射字段,例如,在模型中调用tx_contentelementregistry_relations时,但经过映射'tx_contentelementregistry_relations' => 'relations'后,在模型中调用tx_contentelementregistry_relations时,将使用relations

namespace \YourVendor\YourExtension\Domain\Model\ContentElement;

use DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement;

/**
* Class YourNewContentElement
* @package YourVendor\YourExtension\Domain\Model\ContentElement
*/
class YourNewContentElement extends ContentElement
{
  /**
   * @var array
   */
  protected $columnsMapping = [
      'tx_contentelementregistry_relations' => 'relations',
  ];

}

##变更日志

v1.0.0

  • 取消对typo3 8和typo3 9的支持
  • 仅支持typo3 v10.4
  • 实验性无头支持