Skip to content

Commit

Permalink
[BUGFIX] Add missing descriptions by element
Browse files Browse the repository at this point in the history
The TcaFieldDefinitions need to have the full
descriptionByElement set, or else on save, only
one entry is added, which will cause an error, if
it is a shared field.
  • Loading branch information
nhovratov committed Dec 22, 2021
1 parent 0c5d10b commit 93fdf3d
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Classes/Definition/TableDefinitionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ public function getElementsWhichUseField(string $key, string $table = 'tt_conten
$elementsInUse->addElement($element);
break;
}
if ($this->getFieldType($column, $table)->equals(FieldType::PALETTE)) {
$fieldDefinition = $this->loadField($table, $column);
if ($fieldDefinition instanceof TcaFieldDefinition && !$fieldDefinition->isCoreField && $fieldDefinition->type->equals(FieldType::PALETTE)) {
foreach ($definition->palettes->getPalette($column)->showitem as $item) {
if ($item === $key) {
$elementsInUse->addElement($element);
Expand Down
5 changes: 5 additions & 0 deletions Classes/Enumeration/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public function canBeShared(): bool
{
return !in_array($this->value, [self::INLINE, self::CONTENT, self::PALETTE, self::TAB, self::LINEBREAK], true);
}

public function hasDescription(): bool
{
return $this->value === self::PALETTE || $this->canBeShared();
}
}
97 changes: 97 additions & 0 deletions Classes/Loader/DescriptionByElementCompatibilityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace MASK\Mask\Loader;

use MASK\Mask\Definition\TableDefinitionCollection;
use MASK\Mask\Definition\TcaFieldDefinition;
use MASK\Mask\Enumeration\FieldType;
use MASK\Mask\Utility\AffixUtility;

trait DescriptionByElementCompatibilityTrait
{
public function addMissingDescriptionsByElement(TableDefinitionCollection $tableDefinitionCollection): void
{
// Add defaults, if missing. This can happen on updates or when the defaults change.
foreach ($tableDefinitionCollection as $tableDefinition) {
// Fields on custom tables can't have descriptions by element.
if (AffixUtility::hasMaskPrefix($tableDefinition->table)) {
continue;
}

foreach ($tableDefinition->tca as $tcaFieldDefinition) {
if (!$tcaFieldDefinition->type->isRenderable()) {
continue;
}

// If a field exists, it must have at least one entry in descriptionByElement.
// If this is not the case, the definition is from before Mask 7.1 and needs migration.
if ($tcaFieldDefinition->descriptionByElement === []) {
$this->fillDescriptions($tableDefinitionCollection, $tcaFieldDefinition, $tableDefinition->table);
}

// Go through all palette fields on the root level.
if ($tcaFieldDefinition->type->equals(FieldType::PALETTE)) {
$paletteField = $tableDefinition->palettes->getPalette($tcaFieldDefinition->fullKey);
foreach ($paletteField->showitem as $item) {
$itemField = $tableDefinitionCollection->loadField($tableDefinition->table, $item);

if (!$tcaFieldDefinition->type->hasDescription()) {
continue;
}

if ($itemField instanceof TcaFieldDefinition && $itemField->descriptionByElement === []) {
$this->fillDescriptions($tableDefinitionCollection, $itemField, $tableDefinition->table);
}
}
}
}
}
}

private function fillDescriptions(TableDefinitionCollection $tableDefinitionCollection, TcaFieldDefinition $tcaFieldDefinition, string $table): void
{
$elements = $tableDefinitionCollection->getElementsWhichUseField($tcaFieldDefinition->fullKey, $table);
$descriptionToUse = '';
if ($tcaFieldDefinition->description !== '') {
$descriptionToUse = $tcaFieldDefinition->description;
$tcaFieldDefinition->description = '';
unset($tcaFieldDefinition->realTca['description']);
}
if ($tcaFieldDefinition->inPalette) {
foreach ($elements as $element) {
$tcaFieldDefinition->descriptionByElement[$element->key] = $descriptionToUse;
}
} else {
foreach ($elements as $element) {
$index = array_search($tcaFieldDefinition->fullKey, $element->columns, true);
// Only, if the description is not already set.
if (($element->descriptions[$index] ?? '') === '') {
$element->descriptions[$index] = $descriptionToUse;
}
}
}

// If palette, add it to the palette definition as well.
if ($tcaFieldDefinition->type->equals(FieldType::PALETTE)) {
$paletteDefinition = $tableDefinitionCollection->getTable($table)->palettes->getPalette($tcaFieldDefinition->fullKey);
if ($paletteDefinition->description === '') {
$paletteDefinition->description = $descriptionToUse;
}
}
}
}
2 changes: 2 additions & 0 deletions Classes/Loader/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class JsonLoader implements LoaderInterface

use DefaultTcaCompatibilityTrait;
use ConfigCleanerTrait;
use DescriptionByElementCompatibilityTrait;

public function __construct(array $maskExtensionConfiguration)
{
Expand Down Expand Up @@ -88,6 +89,7 @@ public function load(): TableDefinitionCollection

$this->cleanUpConfig($this->tableDefinitionCollection);
$this->addMissingDefaults($this->tableDefinitionCollection);
$this->addMissingDescriptionsByElement($this->tableDefinitionCollection);

return $this->tableDefinitionCollection;
}
Expand Down
224 changes: 224 additions & 0 deletions Tests/Unit/Loader/DescriptionsByElementCompatibilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

namespace MASK\Mask\Tests\Unit\Loader;

use MASK\Mask\Definition\TableDefinitionCollection;
use MASK\Mask\Loader\DescriptionByElementCompatibilityTrait;
use MASK\Mask\Tests\Unit\PackageManagerTrait;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class DescriptionsByElementCompatibilityTest extends UnitTestCase
{
protected $resetSingletonInstances = true;

use PackageManagerTrait;

/**
* @test
*/
public function descriptionsByElementsAddedIfMissing(): void
{
$loader = new class {
use DescriptionByElementCompatibilityTrait;
};

$input = [
'tt_content' => [
'elements' => [
'element1' => [
'key' => 'element1',
'label' => 'Element 1',
'labels' => [
'Field 1',
'Field direct description',
'Palette 1',
'Inline Field',
],
'columns' => [
'tx_mask_field',
'tx_mask_direct',
'tx_mask_palette',
'tx_mask_inline',
],
],
],
'tca' => [
'tx_mask_field' => [
'config' => [
'type' => 'input',
],
'key' => 'field',
],
'tx_mask_direct' => [
'config' => [
'type' => 'input',
],
'key' => 'direct',
'description' => 'Direct description',
],
'tx_mask_palette' => [
'config' => [
'type' => 'palette',
],
'key' => 'palette',
'description' => 'Palette description'
],
'tx_mask_field2' => [
'config' => [
'type' => 'input',
],
'key' => 'field2',
'type' => 'string',
'inPalette' => 1,
'description' => 'Description in Palette',
'inlineParent' => [
'element1' => 'tx_mask_palette'
]
],
'tx_mask_field3' => [
'config' => [
'type' => 'input',
],
'key' => 'field3',
'type' => 'string',
'inPalette' => 1,
'inlineParent' => [
'element1' => 'tx_mask_palette'
]
],
'tx_mask_inline' => [
'config' => [
'type' => 'inline',
],
'key' => 'inline',
'type' => 'inline',
'description' => 'Inline Field Description'
],
],
'palettes' => [
'tx_mask_palette' => [
'label' => 'Palette 1',
'description' => '',
'showitem' => [
'tx_mask_field2',
'tx_mask_field3',
]
]
]
],
];

$expected = [
'tt_content' => [
'elements' => [
'element1' => [
'key' => 'element1',
'label' => 'Element 1',
'labels' => [
'Field 1',
'Field direct description',
'Palette 1',
'Inline Field',
],
'columns' => [
'tx_mask_field',
'tx_mask_direct',
'tx_mask_palette',
'tx_mask_inline',
],
'descriptions' => [
'',
'Direct description',
'Palette description',
'Inline Field Description',
],
'description' => '',
'shortLabel' => '',
'color' => '',
'icon' => '',
'sorting' => 0,
],
],
'tca' => [
'tx_mask_field' => [
'config' => [
'type' => 'input',
],
'key' => 'field',
'fullKey' => 'tx_mask_field',
'type' => 'string',
],
'tx_mask_direct' => [
'config' => [
'type' => 'input',
],
'key' => 'direct',
'fullKey' => 'tx_mask_direct',
'type' => 'string',
],
'tx_mask_palette' => [
'config' => [
'type' => 'palette',
],
'key' => 'palette',
'fullKey' => 'tx_mask_palette',
'type' => 'palette',
],
'tx_mask_field2' => [
'config' => [
'type' => 'input',
],
'key' => 'field2',
'fullKey' => 'tx_mask_field2',
'type' => 'string',
'inPalette' => 1,
'inlineParent' => [
'element1' => 'tx_mask_palette'
],
'description' => [
'element1' => 'Description in Palette'
]
],
'tx_mask_field3' => [
'config' => [
'type' => 'input',
],
'key' => 'field3',
'fullKey' => 'tx_mask_field3',
'type' => 'string',
'inPalette' => 1,
'inlineParent' => [
'element1' => 'tx_mask_palette'
],
'description' => [
'element1' => ''
]
],
'tx_mask_inline' => [
'config' => [
'type' => 'inline',
],
'key' => 'inline',
'fullKey' => 'tx_mask_inline',
'type' => 'inline',
],
],
'palettes' => [
'tx_mask_palette' => [
'label' => 'Palette 1',
'description' => 'Palette description',
'showitem' => [
'tx_mask_field2',
'tx_mask_field3',
]
]
],
'sql' => []
],
];

$tableDefinitionCollection = TableDefinitionCollection::createFromArray($input);
$loader->addMissingDescriptionsByElement($tableDefinitionCollection);
self::assertEquals($expected, $tableDefinitionCollection->toArray());
}
}
7 changes: 5 additions & 2 deletions Tests/Unit/Loader/JsonLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ protected function getExpectedConfigurationArray(): array
],
'fullKey' => 'tx_mask_a',
'type' => 'string',
'key' => 'a'
'key' => 'a',
],
'tx_mask_b' => [
'config' => [
'type' => 'input'
],
'fullKey' => 'tx_mask_b',
'type' => 'string',
'key' => 'b'
'key' => 'b',
],
'tx_mask_repeat1' => [
'config' => [
Expand Down Expand Up @@ -315,6 +315,9 @@ protected function getExpectedConfigurationArray(): array
],
'order' => [
'b' => 1
],
'description' => [
'b' => ''
]
],
'tx_mask_file' => [
Expand Down

0 comments on commit 93fdf3d

Please sign in to comment.