[BUGFIX] Use localize command when translating page record 20/54220/2
authorFrans Saris <franssaris@gmail.com>
Fri, 26 May 2017 13:51:03 +0000 (15:51 +0200)
committerBenni Mack <benni@typo3.org>
Wed, 27 Sep 2017 06:48:56 +0000 (08:48 +0200)
Currently, when using the dropdown to create a page translation within
the Page Module, DataHandler is called with just a simple "new" record
of type "pages_language_overlay".

However, a classic "localize" DataHandler command is more appropriate,
allowing to utilize several localization functionalities built in.

In order to achieve this, the option tags in the selector contain the full
URL to the DataHandler call, as well as the redirect to FormEngine (which
in turn redirects back to the current page module URL).

Resolves: #81345
Releases: master, 8.7
Change-Id: I740c2d1d11494963283bc14b1f534bd344d0764a
Reviewed-on: https://review.typo3.org/54220
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Classes/Controller/EditDocumentController.php
typo3/sysext/backend/Classes/View/PageLayoutView.php

index e03f709..32bd555 100644 (file)
@@ -1636,6 +1636,7 @@ class EditDocumentController extends AbstractModule
     public function localizationRedirect($justLocalized)
     {
         list($table, $origUid, $language) = explode(':', $justLocalized);
+        $table = $table === 'pages' ? 'pages_language_overlay' : $table;
         if ($GLOBALS['TCA'][$table]
             && $GLOBALS['TCA'][$table]['ctrl']['languageField']
             && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
index e48ef28..07e012f 100644 (file)
@@ -2089,13 +2089,10 @@ class PageLayoutView extends \TYPO3\CMS\Recordlist\RecordList\AbstractDatabaseRe
                 ->from('sys_language')
                 ->orderBy('sorting')
                 ->execute();
-            $langSelItems = [];
-            $langSelItems[0] = '
-                                               <option value="0"></option>';
+            $availableTranslations = [];
             while ($row = $statement->fetch()) {
                 if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
-                    $langSelItems[$row['uid']] = '
-                                                       <option value="' . $row['uid'] . '">' . htmlspecialchars($row['title']) . '</option>';
+                    $availableTranslations[(int)$row['uid']] = $row['title'];
                 }
             }
             // Then, subtract the languages which are already on the page:
@@ -2155,18 +2152,18 @@ class PageLayoutView extends \TYPO3\CMS\Recordlist\RecordList\AbstractDatabaseRe
             }
             $statement = $queryBuilder->execute();
             while ($row = $statement->fetch()) {
-                unset($langSelItems[$row['uid']]);
+                unset($availableTranslations[(int)$row['uid']]);
             }
             // Remove disallowed languages
-            if (count($langSelItems) > 1
+            if (!empty($availableTranslations)
                 && !$this->getBackendUser()->user['admin']
                 && $this->getBackendUser()->groupData['allowed_languages'] !== ''
             ) {
                 $allowed_languages = array_flip(explode(',', $this->getBackendUser()->groupData['allowed_languages']));
                 if (!empty($allowed_languages)) {
-                    foreach ($langSelItems as $key => $value) {
+                    foreach ($availableTranslations as $key => $value) {
                         if (!isset($allowed_languages[$key]) && $key != 0) {
-                            unset($langSelItems[$key]);
+                            unset($availableTranslations[$key]);
                         }
                     }
                 }
@@ -2176,29 +2173,41 @@ class PageLayoutView extends \TYPO3\CMS\Recordlist\RecordList\AbstractDatabaseRe
             $disableLanguages = isset($modSharedTSconfig['properties']['disableLanguages'])
                 ? GeneralUtility::trimExplode(',', $modSharedTSconfig['properties']['disableLanguages'], true)
                 : [];
-            if (!empty($langSelItems) && !empty($disableLanguages)) {
+            if (!empty($availableTranslations) && !empty($disableLanguages)) {
                 foreach ($disableLanguages as $language) {
-                    if ($language != 0 && isset($langSelItems[$language])) {
-                        unset($langSelItems[$language]);
+                    if ($language != 0 && isset($availableTranslations[$language])) {
+                        unset($availableTranslations[$language]);
                     }
                 }
             }
             // If any languages are left, make selector:
-            if (count($langSelItems) > 1) {
-                $url = BackendUtility::getModuleUrl('record_edit', [
-                    'edit[pages_language_overlay][' . $id . ']' => 'new',
-                    'overrideVals[pages_language_overlay][doktype]' => (int)$this->pageRecord['doktype'],
-                    'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
-                ]);
-                $onChangeContent = 'window.location.href=' . GeneralUtility::quoteJSvalue($url . '&overrideVals[pages_language_overlay][sys_language_uid]=') . '+this.options[this.selectedIndex].value';
+            if (!empty($availableTranslations)) {
+                $output = '<option value=""></option>';
+                foreach ($availableTranslations as $languageUid => $languageTitle) {
+                    // Build localize command URL to DataHandler (tce_db)
+                    // which redirects to FormEngine (record_edit)
+                    // which, when finished editing should return back to the current page (returnUrl)
+                    $parameters = [
+                        'justLocalized' => 'pages:' . $id . ':' . $languageUid,
+                        'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
+                    ];
+                    $redirectUrl = BackendUtility::getModuleUrl('record_edit', $parameters);
+                    $targetUrl = BackendUtility::getLinkToDataHandlerAction(
+                        '&cmd[pages][' . $id . '][localize]=' . $languageUid,
+                        $redirectUrl
+                    );
+
+                    $output .= '<option value="' . htmlspecialchars($targetUrl) . '">' . htmlspecialchars($languageTitle) . '</option>';
+                }
+
                 return '<div class="form-inline form-inline-spaced">'
-                . '<div class="form-group">'
-                . '<label for="createNewLanguage">'
-                . htmlspecialchars($this->getLanguageService()->getLL('new_language'))
-                . '</label>'
-                . '<select class="form-control input-sm" name="createNewLanguage" onchange="' . htmlspecialchars($onChangeContent) . '">'
-                . implode('', $langSelItems)
-                . '</select></div></div>';
+                    . '<div class="form-group">'
+                    . '<label for="createNewLanguage">'
+                    . htmlspecialchars($this->getLanguageService()->getLL('new_language'))
+                    . '</label>'
+                    . '<select class="form-control input-sm" name="createNewLanguage" onchange="window.location.href=this.options[this.selectedIndex].value">'
+                    . $output
+                    . '</select></div></div>';
             }
         }
         return '';