[BUGFIX] Fix language of placeholder for relations 22/54722/3
authorNicole Cordes <typo3@cordes.co>
Wed, 22 Nov 2017 11:28:37 +0000 (12:28 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 28 Nov 2017 10:08:43 +0000 (11:08 +0100)
When a parent record (e.g. tt_content) has inline relations
(e.g. sys_file_reference)and these relations show a placeholder
for records related to them (e.g. sys_file and sys_file_metadata)
then we need to pass the context of the language so that
the placeholders are shown in the correct language.

We now do so by putting the value of the language field
of the parent record into the data set and update it
in case child records are also translatable.

This way when going down the list of children, we always
know the language id of the last applicable parent element,
even if one element in between was not translatable (e.g. sys_file)

If we know the language, we can pick the correct record uid
out of the list of possible related uids, instead of
just picking the last one.

Resolves: #75040
Releases: master, 8.7
Change-Id: Iff1b5350138d3043df470d2bdbdfebc83ec8cc5a
Reviewed-on: https://review.typo3.org/54722
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaInputPlaceholders.php

index b3e932e..bd220e4 100644 (file)
@@ -14,9 +14,11 @@ namespace TYPO3\CMS\Backend\Form\FormDataProvider;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Doctrine\DBAL\Connection;
 use TYPO3\CMS\Backend\Form\FormDataCompiler;
 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaInputPlaceholderRecord;
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -119,7 +121,18 @@ class TcaInputPlaceholders implements FormDataProviderInterface
         }
 
         if (!empty($possibleUids) && !empty($fieldNameArray)) {
+            if (count($possibleUids) > 1
+                && !empty($GLOBALS['TCA'][$foreignTableName]['ctrl']['languageField'])
+                && isset($result['currentSysLanguage'])
+            ) {
+                $possibleUids = $this->getPossibleUidsByCurrentSysLanguage($possibleUids, $foreignTableName, $result['currentSysLanguage']);
+            }
             $relatedFormData = $this->getRelatedFormData($foreignTableName, $possibleUids[0], $fieldNameArray[0]);
+            if (!empty($GLOBALS['TCA'][$result['tableName']]['ctrl']['languageField'])
+                && (isset($result['databaseRow'][$GLOBALS['TCA'][$result['tableName']]['ctrl']['languageField']]))
+            ) {
+                $relatedFormData['currentSysLanguage'] = $result['databaseRow'][$GLOBALS['TCA'][$result['tableName']]['ctrl']['languageField']][0];
+            }
             $value = $this->getPlaceholderValue($fieldNameArray, $relatedFormData, $recursionLevel + 1);
         }
 
@@ -203,6 +216,51 @@ class TcaInputPlaceholders implements FormDataProviderInterface
     }
 
     /**
+     * E.g. sys_file is not translatable, thus the uid of the translation of it's metadata has to be retrieved here.
+     *
+     * Get the uid of e.g. a file metadata entry for a given sys_language_uid and the possible translated data.
+     * If there is no translation available, return the uid of default language.
+     * If there is no value at all, return the "possible uids".
+     *
+     * @param array $possibleUids
+     * @param string $foreignTableName
+     * @param int $currentLanguage
+     * @return array
+     */
+    protected function getPossibleUidsByCurrentSysLanguage(array $possibleUids, $foreignTableName, $currentLanguage)
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($foreignTableName);
+        $newUids = $queryBuilder->select('uid')
+            ->from($foreignTableName)
+            ->where(
+                $queryBuilder->expr()->in(
+                    'uid',
+                    $queryBuilder->createNamedParameter($possibleUids, Connection::PARAM_INT_ARRAY)
+                ),
+                $queryBuilder->expr()->in(
+                    $GLOBALS['TCA'][$foreignTableName]['ctrl']['languageField'],
+                    $queryBuilder->createNamedParameter([$currentLanguage, 0], Connection::PARAM_INT_ARRAY)
+                )
+            )
+            ->orderBy($GLOBALS['TCA'][$foreignTableName]['ctrl']['languageField'], 'ASC')
+            ->setMaxResults(2)
+            ->execute()
+            ->fetchAll(\PDO::FETCH_NUM);
+
+        if (!empty($newUids)) {
+            // translation available
+            if (!empty($newUids[1])) {
+                return $newUids[1];
+            }
+
+            // no translation available, use default
+            return $newUids[0];
+        }
+
+        return $possibleUids;
+    }
+
+    /**
      * @return LanguageService
      */
     protected function getLanguageService()