[BUGFIX] Render slug dependent fields in single table view 17/61317/7
authorGeorg Ringer <georg.ringer@gmail.com>
Thu, 18 Jul 2019 06:35:46 +0000 (08:35 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 2 Aug 2019 12:50:28 +0000 (14:50 +0200)
If only the slug field should be rendered in the record list, all fields
which are required to generate the slug are added as well.

This makes it possible to have a simple mass update option for slug fields.

Resolves: #88156
Releases: master, 9.5
Change-Id: Ie8463691bc715c92668f65170948ad2518520528
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61317
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Josef Glatz <josefglatz@gmail.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Josef Glatz <josefglatz@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Controller/EditDocumentController.php
typo3/sysext/backend/Tests/Unit/Controller/EditDocumentControllerTest.php

index 6e0e417..6b3e4d8 100644 (file)
@@ -457,6 +457,7 @@ class EditDocumentController
         if (!is_array($this->defVals) && is_array($this->overrideVals)) {
             $this->defVals = $this->overrideVals;
         }
+        $this->addSlugFieldsToColumnsOnly($queryParams);
 
         // Set final return URL
         $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
@@ -494,6 +495,28 @@ class EditDocumentController
     }
 
     /**
+     * Always add required fields of slug field
+     *
+     * @param array $queryParams
+     */
+    protected function addSlugFieldsToColumnsOnly(array $queryParams): void
+    {
+        $data = $queryParams['edit'] ?? [];
+        $data = array_keys($data);
+        $table = reset($data);
+        if ($this->columnsOnly && $table !== false && isset($GLOBALS['TCA'][$table])) {
+            $fields = GeneralUtility::trimExplode(',', $this->columnsOnly, true);
+            foreach ($fields as $field) {
+                if (isset($GLOBALS['TCA'][$table]['columns'][$field]) && $GLOBALS['TCA'][$table]['columns'][$field]['config']['type'] === 'slug') {
+                    foreach ($GLOBALS['TCA'][$table]['columns'][$field]['config']['generatorOptions']['fields'] as $fields) {
+                        $this->columnsOnly .= ',' . (is_array($fields) ? implode(',', $fields) : $fields);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
      * Do processing of data, submitting it to DataHandler. May return a RedirectResponse
      *
      * @param ServerRequestInterface $request
index b53ce3f..175a2f6 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 namespace TYPO3\CMS\Backend\Tests\Unit\Controller;
 
 /*
@@ -46,4 +47,77 @@ class EditDocumentControllerTest extends UnitTestCase
         $mock->_callRef('parseAdditionalGetParameters', $result, $typoScript);
         $this->assertSame($expectedParameters, $result);
     }
+
+    /**
+     * @test
+     * @dataProvider slugDependendFieldsAreAddedToColumnsOnlyDataProvider
+     */
+    public function slugDependendFieldsAreAddedToColumnsOnly(string $result, string $selectedFields, string $tableName, array $configuration): void
+    {
+        $GLOBALS['TCA'][$tableName]['columns'] = $configuration;
+
+        $editDocumentControllerMock = $this->getAccessibleMock(EditDocumentController::class, ['dummy'], [], '', false);
+        $editDocumentControllerMock->_set('columnsOnly', $selectedFields);
+        $queryParams = [
+            'edit' => [
+                $tableName => [
+                    '123,456' => 'edit'
+                ]
+            ],
+        ];
+        $editDocumentControllerMock->_call('addSlugFieldsToColumnsOnly', $queryParams);
+
+        $this->assertEquals($result, $editDocumentControllerMock->_get('columnsOnly'));
+    }
+
+    public function slugDependendFieldsAreAddedToColumnsOnlyDataProvider(): array
+    {
+        return [
+            'fields in string' => [
+                'fo,bar,slug,title',
+                'fo,bar,slug',
+                'fake',
+                [
+                    'slug' => [
+                        'config' => [
+                            'type' => 'slug',
+                            'generatorOptions' => [
+                                'fields' => ['title'],
+                            ],
+                        ]
+                    ],
+                ]
+            ],
+            'fields in string and array' => [
+                'slug,fo,title,nav_title,title,other_field',
+                'slug,fo,title',
+                'fake',
+                [
+                    'slug' => [
+                        'config' => [
+                            'type' => 'slug',
+                            'generatorOptions' => [
+                                'fields' => [['nav_title', 'title'], 'other_field']
+                            ],
+                        ]
+                    ],
+                ]
+            ],
+            'no slug field given' => [
+                'slug,fo',
+                'slug,fo',
+                'fake',
+                [
+                    'slug' => [
+                        'config' => [
+                            'type' => 'input',
+                            'generatorOptions' => [
+                                'fields' => [['nav_title', 'title'], 'other_field']
+                            ],
+                        ]
+                    ],
+                ]
+            ],
+        ];
+    }
 }