Skip to content

Commit

Permalink
[TASK] Ensure to have a default showitem info when adding plugins as …
Browse files Browse the repository at this point in the history
…CType

This change adds the basic information for a type
when adding a plugin as CType. When adding a plugin with
"list_type" (default), then the "list" CType is used,
and extension authors are able to add or remove fields
easily with the API. However, when encouraging extension authors
to use CType, this is currently much more boilerplate and not
possible to make this more flexible.

As shown, EXT:felogin and EXT:form previously used this concept
manually, which does not allow to extend fields properly,
but extensions with plugins as CTypes had to define them explicitly.

As TYPO3 Core encourages people to use custom CTypes in favor
of list_type (moving towards a unifieid Content Block concept),
this change will also be available for 11.5.

Resolves: #98960
Releases: main, 11.5
Change-Id: Iee5e1a35f2d99faf03081d7265c2fe6e4845397f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76465
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
  • Loading branch information
bmack authored and o-ba committed Nov 8, 2022
1 parent 5e63517 commit 1b52899
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 53 deletions.
11 changes: 9 additions & 2 deletions typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
Expand Up @@ -232,7 +232,7 @@ public static function addToAllTCAtypes($table, $newFieldsString, $typeList = ''
return;
}
if ($position !== '') {
[$positionIdentifier, $entityName] = GeneralUtility::trimExplode(':', $position);
[$positionIdentifier, $entityName] = GeneralUtility::trimExplode(':', $position, false, 2);
} else {
$positionIdentifier = '';
$entityName = '';
Expand Down Expand Up @@ -274,7 +274,7 @@ public static function addToAllTCAtypes($table, $newFieldsString, $typeList = ''
switch ($positionIdentifier) {
case 'after':
case 'before':
if (preg_match('/\\b' . $entityName . '\\b/', $palette['showitem']) > 0) {
if (preg_match('/\\b' . $entityName . '\\b/', $palette['showitem']) > 0 || $entityName === 'palette:' . $paletteName) {
$newPosition = $positionIdentifier . ':--palette--;;' . $paletteName;
}
break;
Expand Down Expand Up @@ -1217,6 +1217,9 @@ public static function addPlugin($itemArray, $type = 'list_type', $extensionKey
if (!isset($itemArray[2]) || !$itemArray[2]) {
// @todo do we really set $itemArray[2], even if we cannot find an icon? (as that means it's set to 'EXT:foobar/')
$itemArray[2] = 'EXT:' . $extensionKey . '/' . static::getExtensionIcon(static::$packageManager->getPackage($extensionKey)->getPackagePath());
} elseif ($type === 'CType' && !isset($GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes'][$itemArray[1]])) {
// Set the type icon as well
$GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes'][$itemArray[1]] = $itemArray[2];
}
if (!isset($itemArray[3])) {
$itemArray[3] = 'default';
Expand All @@ -1230,6 +1233,10 @@ public static function addPlugin($itemArray, $type = 'list_type', $extensionKey
}
$GLOBALS['TCA']['tt_content']['columns'][$type]['config']['items'][] = $itemArray;
}
// Ensure to have at least some basic information available when editing the new type in FormEngine
if ($type === 'CType' && !isset($GLOBALS['TCA']['tt_content']['types'][$itemArray[1]]) && isset($GLOBALS['TCA']['tt_content']['types']['header'])) {
$GLOBALS['TCA']['tt_content']['types'][$itemArray[1]] = $GLOBALS['TCA']['tt_content']['types']['header'];
}
}

/**
Expand Down
@@ -0,0 +1,70 @@
.. include:: /Includes.rst.txt

.. _important-98960-1667212946:

===================================================================
Important: #98960 - Default type definition of custom Content Types
===================================================================

See :issue:`98960`

Description
===========

Due to the deprecation of Switchable Controller Actions for Extbase, it is
recommended to use custom content types as plugins. When using Extbase's API
:php:`\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin()` with
the 5th argument being set to
:php:`\TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT`
or TYPO3's native API :php:`\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPlugin()`
with the second argument being set to `CType`, the entered icon identifier
is now automatically added as `typeicon_classes` for the given `CType` and
the `TCA` types definition (`showitem`) of the default `header` type is
automatically applied, so extension authors do not need to add all default
fields anymore.

Note
----

These defaults are only applied if they are not set manually, so the changes
are optional defaults.

In addition, the API method :php:`\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes()`
now also allows to add custom fields after a palette, so common variants such
as the `Plugin` tab can be added after a specific palette.

Example
-------

Example for a custom Extbase plugin with TYPO3's Core "felogin" extension
in `EXT:felogin/Configuration/TCA/Overrides/tt_content.php`:

.. code-block:: php
call_user_func(static function () {
$contentTypeName = 'felogin_login';
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
'Felogin',
'Login',
'LLL:EXT:felogin/Resources/Private/Language/Database.xlf:tt_content.CType.felogin_login.title',
'mimetypes-x-content-login',
'forms'
);
// Add the FlexForm for the new content type
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:felogin/Configuration/FlexForms/Login.xml',
$contentTypeName
);
// Add the FlexForm to the showitem list
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tt_content',
'--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.plugin, pi_flexform',
$contentTypeName,
'after:palette:headers'
);
});
.. index:: Backend, TCA, ext:core
Expand Up @@ -393,6 +393,18 @@ public function addToAllTCAtypesReplacesExistingOnes(): void
self::assertEquals('fieldX, --palette--;;foo, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
}

/**
* @test
*/
public function addToAllTCAtypesAddsToPaletteIdentifier(): void
{
$table = StringUtility::getUniqueId('tx_coretest_table');
$GLOBALS['TCA'] = $this->generateTCAForTable($table);
ExtensionManagementUtility::addToAllTCAtypes($table, 'fieldX, --palette--;;newpalette', '', 'after:palette:paletteC');
self::assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldX, --palette--;;newpalette, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
self::assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldX, --palette--;;newpalette, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
}

/**
* Tests whether fields can be added to a palette before existing elements.
*
Expand Down Expand Up @@ -1792,6 +1804,29 @@ public function addPluginThrowsExceptionForMissingExtkey(): void
ExtensionManagementUtility::addPlugin('test');
}

/**
* @test
*/
public function addPluginAsContentTypeAddsIconAndDefaultItem(): void
{
$extKey = 'felogin';
$expectedTCA = [
[
'label',
'felogin',
'content-form-login',
'default',
],
];
$GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes'] = [];
$GLOBALS['TCA']['tt_content']['types']['header'] = ['showitem' => 'header,header_link'];
$GLOBALS['TCA']['tt_content']['columns']['CType']['config']['items'] = [];
ExtensionManagementUtility::addPlugin(['label', $extKey, 'content-form-login'], 'CType', $extKey);
self::assertEquals($expectedTCA, $GLOBALS['TCA']['tt_content']['columns']['CType']['config']['items']);
self::assertEquals([$extKey => 'content-form-login'], $GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes']);
self::assertEquals($GLOBALS['TCA']['tt_content']['types']['header'], $GLOBALS['TCA']['tt_content']['types']['felogin']);
}

public function addTcaSelectItemGroupAddsGroupDataProvider(): array
{
return [
Expand Down
30 changes: 8 additions & 22 deletions typo3/sysext/felogin/Configuration/TCA/Overrides/tt_content.php
Expand Up @@ -8,7 +8,7 @@
'Felogin',
'Login',
'LLL:EXT:felogin/Resources/Private/Language/Database.xlf:tt_content.CType.felogin_login.title',
null,
'mimetypes-x-content-login',
'forms'
);

Expand All @@ -18,26 +18,12 @@
'FILE:EXT:felogin/Configuration/FlexForms/Login.xml',
$contentTypeName
);
$GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes'][$contentTypeName] = 'mimetypes-x-content-login';

$GLOBALS['TCA']['tt_content']['types'][$contentTypeName]['showitem'] = '
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
--palette--;;general,
--palette--;;headers,
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.plugin,
pi_flexform,
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
--palette--;;frames,
--palette--;;appearanceLinks,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
--palette--;;language,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access,
--palette--;;hidden,
--palette--;;access,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:categories,
categories,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:notes,
rowDescription,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:extended,
';
// Add the FlexForm to the show item list
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tt_content',
'--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.plugin, pi_flexform',
$contentTypeName,
'after:palette:headers'
);
});
48 changes: 19 additions & 29 deletions typo3/sysext/form/Configuration/TCA/Overrides/tt_content.php
Expand Up @@ -3,36 +3,9 @@
defined('TYPO3') or die();

call_user_func(static function () {
// Add the FlexForm
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:form/Configuration/FlexForms/FormFramework.xml',
'form_formframework'
);

$GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes']['form_formframework'] = 'content-form';
// Set content type name
$contentTypeName = 'form_formframework';

$GLOBALS['TCA']['tt_content']['types']['form_formframework']['previewRenderer'] = \TYPO3\CMS\Form\Hooks\FormPagePreviewRenderer::class;
$GLOBALS['TCA']['tt_content']['types']['form_formframework']['showitem'] = '
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
--palette--;;general,
--palette--;;header,
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.plugin,
pi_flexform,
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
--palette--;;frames,
--palette--;;appearanceLinks,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
--palette--;;language,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access,
--palette--;;hidden,
--palette--;;access,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:categories,
categories,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:notes,
rowDescription,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:extended,
';
// Register the plugin
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
'Form',
Expand All @@ -41,4 +14,21 @@
'content-form',
'forms'
);

// Add the FlexForm
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
'*',
'FILE:EXT:form/Configuration/FlexForms/FormFramework.xml',
$contentTypeName
);

// Add the FlexForm to the show item list
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tt_content',
'--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.plugin, pi_flexform',
$contentTypeName,
'after:palette:headers'
);

$GLOBALS['TCA']['tt_content']['types'][$contentTypeName]['previewRenderer'] = \TYPO3\CMS\Form\Hooks\FormPagePreviewRenderer::class;
});

0 comments on commit 1b52899

Please sign in to comment.