Skip to content

Commit

Permalink
Add rector for language TCA type
Browse files Browse the repository at this point in the history
Resolves: #2590
  • Loading branch information
sabbelasichon committed Jan 11, 2022
1 parent f2eba89 commit 4ba3551
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/v11/typo3-113.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
$services = $containerConfigurator->services();
$services->set(\Ssch\TYPO3Rector\Rector\v11\v3\SubstituteMethodRmFromListOfGeneralUtilityRector::class);
$services->set(\Ssch\TYPO3Rector\Rector\v11\v3\SwitchBehaviorOfArrayUtilityMethodsRector::class);
$services->set(\Ssch\TYPO3Rector\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector::class);
};
140 changes: 140 additions & 0 deletions src/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v11\v3;

use PhpParser\Node;
use PhpParser\Node\Expr;
use Ssch\TYPO3Rector\Rector\Tca\AbstractTcaRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/11.3/Deprecation-94165-SysLanguageDatabaseTable.html
*/
final class UseLanguageTypeForLanguageFieldColumnRector extends AbstractTcaRector
{
private ?string $languageField = null;

/**
* @codeCoverageIgnore
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'use the new TCA type language instead of foreign_table => sys_language for selecting a records',
[
new CodeSample(
<<<'CODE_SAMPLE'
return [
'ctrl' => [
'languageField' => 'sys_language_uid',
],
'columns' => [
'sys_language_uid' => [
'exclude' => 1,
'label' => 'Language',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'sys_language',
'foreign_table_where' => 'ORDER BY sys_language.title',
'eval' => 'int',
'items' => [
[$_LLL_general . ':LGL.allLanguages', -1],
[$_LLL_general . ':LGL.default_value', 0]
],
],
],
],
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return [
'ctrl' => [
'languageField' => 'sys_language_uid',
],
'columns' => [
'sys_language_uid' => [
'exclude' => 1,
'label' => 'Language',
'config' => [
'type' => 'language'
],
],
],
];
CODE_SAMPLE
),

]
);
}

public function refactor(Node $node): ?Node
{
$this->hasAstBeenChanged = false;
if (! $this->isFullTcaDefinition($node)) {
return null;
}

$ctrl = $this->extractSubArrayByKey($node, 'ctrl');
if (null === $ctrl) {
return null;
}

$value = $this->extractArrayValueByKey($ctrl, 'languageField');

if (! $value instanceof Node\Scalar\String_) {
return null;
}

$this->languageField = $this->valueResolver->getValue($value);

if (null === $this->languageField) {
return null;
}

// we found a tca definition of a full table. Process it as a whole:
$columns = $this->extractSubArrayByKey($node, 'columns');
if (null !== $columns) {
$this->refactorColumnList($columns);
}

return $this->hasAstBeenChanged ? $node : null;
}

protected function refactorColumn(Expr $columnName, Expr $columnTca): void
{
$column = $this->valueResolver->getValue($columnName);

if ($column !== $this->languageField) {
return;
}

$configuration = $this->extractArrayItemByKey($columnTca, 'config');

if (null === $configuration) {
return;
}

$foreignTable = $this->extractArrayItemByKey($configuration->value, 'foreign_table');

if (null === $foreignTable) {
return;
}

if (! $this->valueResolver->isValue($foreignTable->value, 'sys_language')) {
return;
}

$configuration->value = $this->nodeFactory->createArray([
'type' => 'language',
]);

$this->hasAstBeenChanged = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$_LLL_general = 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf';

return [
'ctrl' => [
'languageField' => 'sys_language_uid',
],
'columns' => [
'other_language_field' => [
'exclude' => 1,
'label' => $_LLL_general . ':LGL.language',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'sys_language',
'foreign_table_where' => 'ORDER BY sys_language.title',
'eval' => 'int',
'items' => [
[$_LLL_general . ':LGL.allLanguages', -1],
[$_LLL_general . ':LGL.default_value', 0]

],
],
],
],
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

$_LLL_general = 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf';

return [
'ctrl' => [
'languageField' => 'sys_language_uid',
],
'columns' => [
'sys_language_uid' => [
'exclude' => 1,
'label' => $_LLL_general . ':LGL.language',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'sys_language',
'foreign_table_where' => 'ORDER BY sys_language.title',
'eval' => 'int',
'items' => [
[$_LLL_general . ':LGL.allLanguages', -1],
[$_LLL_general . ':LGL.default_value', 0]

],
],
],
],
];

?>
-----
<?php

$_LLL_general = 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf';

return [
'ctrl' => [
'languageField' => 'sys_language_uid',
],
'columns' => [
'sys_language_uid' => [
'exclude' => 1,
'label' => $_LLL_general . ':LGL.language',
'config' => ['type' => 'language'],
],
],
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class UseLanguageTypeForLanguageFieldColumnRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/../../../../../../config/config_test.php');

$services = $containerConfigurator->services();
$services->set(\Ssch\TYPO3Rector\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector::class);
};
2 changes: 1 addition & 1 deletion utils/generator/src/Command/Typo3GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'__Name__' => $recipe->getRectorName(),
'__Test_Directory__' => $recipe->getTestDirectory(),
'__Rst_File__' => $recipe->getUrlToRstFile(),
'__Description__' => $recipe->getDescription(),
'__Description__' => addslashes($recipe->getDescription()),
];

$targetDirectory = getcwd();
Expand Down

0 comments on commit 4ba3551

Please sign in to comment.