[BUGFIX] Use localize command when translating page record 45/52945/6
authorFrans Saris <franssaris@gmail.com>
Fri, 26 May 2017 13:51:03 +0000 (15:51 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Wed, 27 Sep 2017 05:23:25 +0000 (07:23 +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/52945
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: TYPO3com <no-reply@typo3.com>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/backend/Classes/Controller/EditDocumentController.php
typo3/sysext/backend/Classes/View/PageLayoutView.php

index 7b2502c..ad2b4bd 100644 (file)
@@ -1630,6 +1630,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 05bd1f3..34b1f8e 100644 (file)
@@ -2479,13 +2479,10 @@ class PageLayoutView implements LoggerAwareInterface
                 ->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:
@@ -2545,18 +2542,18 @@ class PageLayoutView implements LoggerAwareInterface
             }
             $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]);
                         }
                     }
                 }
@@ -2566,29 +2563,41 @@ class PageLayoutView implements LoggerAwareInterface
             $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 '';