Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rector for language TCA type #2782

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};
144 changes: 144 additions & 0 deletions src/Rector/v11/v3/UseLanguageTypeForLanguageFieldColumnRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v11\v3;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\String_;
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
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector\UseLanguageTypeForLanguageFieldColumnRectorTest
*/
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 (! $ctrl instanceof Array_) {
return null;
}

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

if (! $value instanceof 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 (! $configuration instanceof ArrayItem) {
return;
}

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

if (! $foreignTable instanceof ArrayItem) {
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,14 @@
<?php

declare(strict_types=1);

use Ssch\TYPO3Rector\Rector\v11\v3\UseLanguageTypeForLanguageFieldColumnRector;

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(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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* @changelog __Rst_File__
* @see Ssch\TYPO3Rector\Tests\Rector\__Major__\__Minor__\__Test_Directory__\__Name__Test__
*/
final class __Name__ extends AbstractRector
{
Expand Down