diff --git a/config/v11/typo3-113.php b/config/v11/typo3-113.php index 5877b0f2b..3a926a12c 100644 --- a/config/v11/typo3-113.php +++ b/config/v11/typo3-113.php @@ -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); }; diff --git a/src/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector.php b/src/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector.php new file mode 100644 index 000000000..832865b9d --- /dev/null +++ b/src/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector.php @@ -0,0 +1,139 @@ + 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; + } +} diff --git a/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/skip_language_type.php.inc b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/skip_language_type.php.inc new file mode 100644 index 000000000..9683f71c6 --- /dev/null +++ b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/skip_language_type.php.inc @@ -0,0 +1,29 @@ + [ + '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] + + ], + ], + ], + ], +]; + +?> diff --git a/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/use_language_type.php.inc b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/use_language_type.php.inc new file mode 100644 index 000000000..a5c77c7d8 --- /dev/null +++ b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/Fixture/use_language_type.php.inc @@ -0,0 +1,48 @@ + [ + '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] + + ], + ], + ], + ], +]; + +?> +----- + [ + 'languageField' => 'sys_language_uid', + ], + 'columns' => [ + 'sys_language_uid' => [ + 'exclude' => 1, + 'label' => $_LLL_general . ':LGL.language', + 'config' => ['type' => 'language'], + ], + ], +]; + +?> diff --git a/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/UseLanguageTypeForLanguageFieldColumnRectorTest.php b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/UseLanguageTypeForLanguageFieldColumnRectorTest.php new file mode 100644 index 000000000..04803f36b --- /dev/null +++ b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/UseLanguageTypeForLanguageFieldColumnRectorTest.php @@ -0,0 +1,33 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/config/configured_rule.php b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/config/configured_rule.php new file mode 100644 index 000000000..34daa4304 --- /dev/null +++ b/tests/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector/config/configured_rule.php @@ -0,0 +1,12 @@ +import(__DIR__ . '/../../../../../../config/config_test.php'); + + $services = $containerConfigurator->services(); + $services->set(\Ssch\TYPO3Rector\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector::class); +}; diff --git a/utils/generator/src/Command/Typo3GenerateCommand.php b/utils/generator/src/Command/Typo3GenerateCommand.php index 319731b7c..6bc1e8492 100644 --- a/utils/generator/src/Command/Typo3GenerateCommand.php +++ b/utils/generator/src/Command/Typo3GenerateCommand.php @@ -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();