forked from Gernott/mask
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Add missing descriptions by element
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
Showing
6 changed files
with
335 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
Tests/Unit/Loader/DescriptionsByElementCompatibilityTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters