|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\Typo3Fractor\TYPO3v12\FlexForm; |
| 6 | + |
| 7 | +use a9f\Fractor\Application\ValueObject\File; |
| 8 | +use a9f\Fractor\Helper\ArrayUtility; |
| 9 | +use a9f\Typo3Fractor\AbstractFlexformFractor; |
| 10 | +use a9f\Typo3Fractor\Helper\FlexFormHelperTrait; |
| 11 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 12 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 13 | + |
| 14 | +/** |
| 15 | + * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97271-NewTCATypeColor.html |
| 16 | + * @see \a9f\Typo3Fractor\Tests\TYPO3v12\FlexForm\MigrateRenderTypeColorpickerToTypeColorFlexFormFractor\MigrateRenderTypeColorpickerToTypeColorFlexFormFractorTest |
| 17 | + */ |
| 18 | +final class MigrateRenderTypeColorpickerToTypeColorFlexFormFractor extends AbstractFlexformFractor |
| 19 | +{ |
| 20 | + use FlexFormHelperTrait; |
| 21 | + |
| 22 | + private \DOMDocument $domDocument; |
| 23 | + |
| 24 | + public function canHandle(\DOMNode $node): bool |
| 25 | + { |
| 26 | + return parent::canHandle($node) && $node->nodeName === 'config'; |
| 27 | + } |
| 28 | + |
| 29 | + public function beforeTraversal(File $file, \DOMDocument $rootNode): void |
| 30 | + { |
| 31 | + $this->file = $file; |
| 32 | + $this->domDocument = $rootNode; |
| 33 | + } |
| 34 | + |
| 35 | + public function refactor(\DOMNode $node): \DOMNode|int|null |
| 36 | + { |
| 37 | + if (! $node instanceof \DOMElement) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + if (! $this->isConfigType($node, 'input')) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + if (! $this->configIsOfRenderType($node, 'colorpicker')) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + // Set the TCA type to "color" |
| 50 | + $this->changeTcaType($this->domDocument, $node, 'color'); |
| 51 | + |
| 52 | + // Remove 'max' and 'renderType' config |
| 53 | + $this->removeChildElementFromDomElementByKey($node, 'max'); |
| 54 | + $this->removeChildElementFromDomElementByKey($node, 'renderType'); |
| 55 | + |
| 56 | + $evalDomElement = $this->extractDomElementByKey($node, 'eval'); |
| 57 | + if (! $evalDomElement instanceof \DOMElement) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + $evalListValue = $evalDomElement->nodeValue; |
| 62 | + if (! is_string($evalListValue)) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $evalList = ArrayUtility::trimExplode(',', $evalListValue, true); |
| 67 | + |
| 68 | + if (in_array('null', $evalList, true)) { |
| 69 | + // Set "eval" to "null", since it's currently defined and the only allowed "eval" for type=color |
| 70 | + $evalDomElement->nodeValue = ''; |
| 71 | + $evalDomElement->appendChild($this->domDocument->createTextNode('null')); |
| 72 | + } elseif ($evalDomElement->parentNode instanceof \DOMElement) { |
| 73 | + // 'eval' is empty, remove whole configuration |
| 74 | + $evalDomElement->parentNode->removeChild($evalDomElement); |
| 75 | + } |
| 76 | + |
| 77 | + return $node; |
| 78 | + } |
| 79 | + |
| 80 | + public function getRuleDefinition(): RuleDefinition |
| 81 | + { |
| 82 | + return new RuleDefinition('Migrate renderType colorpicker to type color', [new CodeSample( |
| 83 | + <<<'CODE_SAMPLE' |
| 84 | +<T3DataStructure> |
| 85 | + <ROOT> |
| 86 | + <sheetTitle>aTitle</sheetTitle> |
| 87 | + <type>array</type> |
| 88 | + <el> |
| 89 | + <a_color_field> |
| 90 | + <label>Color field</label> |
| 91 | + <config> |
| 92 | + <type>input</type> |
| 93 | + <renderType>colorpicker</renderType> |
| 94 | + <required>1</required> |
| 95 | + <size>20</size> |
| 96 | + <max>1234</max> |
| 97 | + <eval>trim,null</eval> |
| 98 | + <valuePicker> |
| 99 | + <items type="array"> |
| 100 | + <numIndex index="0" type="array"> |
| 101 | + <numIndex index="0">typo3 orange</numIndex> |
| 102 | + <numIndex index="1">#FF8700</numIndex> |
| 103 | + </numIndex> |
| 104 | + </items> |
| 105 | + </valuePicker> |
| 106 | + </config> |
| 107 | + </a_color_field> |
| 108 | + </el> |
| 109 | + </ROOT> |
| 110 | +</T3DataStructure> |
| 111 | +CODE_SAMPLE |
| 112 | + , |
| 113 | + <<<'CODE_SAMPLE' |
| 114 | +<T3DataStructure> |
| 115 | + <ROOT> |
| 116 | + <sheetTitle>aTitle</sheetTitle> |
| 117 | + <type>array</type> |
| 118 | + <el> |
| 119 | + <a_color_field> |
| 120 | + <label>Color field</label> |
| 121 | + <config> |
| 122 | + <type>color</type> |
| 123 | + <required>1</required> |
| 124 | + <size>20</size> |
| 125 | + <valuePicker> |
| 126 | + <items type="array"> |
| 127 | + <numIndex index="0" type="array"> |
| 128 | + <numIndex index="0">typo3 orange</numIndex> |
| 129 | + <numIndex index="1">#FF8700</numIndex> |
| 130 | + </numIndex> |
| 131 | + </items> |
| 132 | + </valuePicker> |
| 133 | + </config> |
| 134 | + </a_color_field> |
| 135 | + </el> |
| 136 | + </ROOT> |
| 137 | +</T3DataStructure> |
| 138 | +CODE_SAMPLE |
| 139 | + )]); |
| 140 | + } |
| 141 | +} |
0 commit comments