[BUGFIX] Fix language of placeholder for relations 88/54888/2
authorNicole Cordes <typo3@cordes.co>
Wed, 22 Nov 2017 11:28:37 +0000 (12:28 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Thu, 30 Nov 2017 14:24:07 +0000 (15:24 +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 first one.

Furthermore the language field for sys_file_metadata is removed from
editor form as the language is set by GET parameter already.

Change-Id: I64c97c8080196599e85b95e09e7eb89701670fa4
Resolves: #75040
Releases: master, 8.7
Reviewed-on: https://review.typo3.org/54831
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
Reviewed-on: https://review.typo3.org/54888

typo3/sysext/backend/Classes/Form/FormDataProvider/TcaInputPlaceholders.php
typo3/sysext/core/Configuration/TCA/sys_file_metadata.php
typo3/sysext/filemetadata/Tests/Functional/Tca/FileMetadataVisibleFieldsTest.php

index 4ab3582..8a9d0f2 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\Utility\GeneralUtility;
 use TYPO3\CMS\Lang\LanguageService;
 
@@ -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,53 @@ 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)
+    {
+        $languageField = $GLOBALS['TCA'][$foreignTableName]['ctrl']['languageField'];
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($foreignTableName);
+        $possibleRecords = $queryBuilder->select('uid', $languageField)
+            ->from($foreignTableName)
+            ->where(
+                $queryBuilder->expr()->in(
+                    'uid',
+                    $queryBuilder->createNamedParameter($possibleUids, Connection::PARAM_INT_ARRAY)
+                ),
+                $queryBuilder->expr()->in(
+                    $languageField,
+                    $queryBuilder->createNamedParameter([$currentLanguage, 0], Connection::PARAM_INT_ARRAY)
+                )
+            )
+            ->groupBy($languageField)
+            ->execute()
+            ->fetchAll();
+
+        if (!empty($possibleRecords)) {
+            // Either only one record or first record matches language
+            if (count($possibleRecords) === 1
+                || (int)$possibleRecords[0][$languageField] === (int)$currentLanguage
+            ) {
+                return [$possibleRecords[0]['uid']];
+            }
+
+            // Language of second record matches language
+            return [$possibleRecords[1]['uid']];
+        }
+
+        return $possibleUids;
+    }
+
+    /**
      * @return LanguageService
      */
     protected function getLanguageService()
index 4803d81..a0ea62c 100644 (file)
@@ -153,15 +153,16 @@ return [
     'types' => [
         '1' => ['showitem' => '
             --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
-                fileinfo, title, description, alternative,
-            --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
-                --palette--;;language,
+                fileinfo, title, description, alternative, --palette--;;language,
             --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:categories,
                 categories,
             --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:extended,
         ']
     ],
     'palettes' => [
-        'language' => ['showitem' => 'sys_language_uid, l10n_parent'],
+        'language' => [
+            'showitem' => 'sys_language_uid, l10n_parent',
+            'isHiddenPalette' => true,
+        ],
     ]
 ];
index a4f7891..485c8b4 100644 (file)
@@ -31,7 +31,6 @@ class FileMetadataVisibleFieldsTest extends \TYPO3\TestingFramework\Core\Functio
      */
     protected static $fileMetadataFields = [
         File::FILETYPE_UNKNOWN => [
-            'sys_language_uid',
             'title',
             'description',
             'ranking',
@@ -52,7 +51,6 @@ class FileMetadataVisibleFieldsTest extends \TYPO3\TestingFramework\Core\Functio
             'categories',
         ],
         File::FILETYPE_TEXT => [
-            'sys_language_uid',
             'title',
             'description',
             'ranking',
@@ -74,7 +72,6 @@ class FileMetadataVisibleFieldsTest extends \TYPO3\TestingFramework\Core\Functio
             'categories',
         ],
         File::FILETYPE_IMAGE => [
-            'sys_language_uid',
             'title',
             'description',
             'ranking',
@@ -101,7 +98,6 @@ class FileMetadataVisibleFieldsTest extends \TYPO3\TestingFramework\Core\Functio
             'categories',
         ],
         File::FILETYPE_AUDIO => [
-            'sys_language_uid',
             'title',
             'description',
             'ranking',
@@ -123,7 +119,6 @@ class FileMetadataVisibleFieldsTest extends \TYPO3\TestingFramework\Core\Functio
             'categories',
         ],
         File::FILETYPE_VIDEO => [
-            'sys_language_uid',
             'title',
             'description',
             'ranking',