From 969c2a9edd5786c9de9d5ff65d38c52944da08aa Mon Sep 17 00:00:00 2001
From: Georg Ringer <georg.ringer@gmail.com>
Date: Mon, 20 Jun 2022 15:38:30 +0200
Subject: [PATCH] [FEATURE] Set content language direction in RichText elements

Set the content language of RichText elements based on the direction
of the site language of the given element.

Resolves: #97778
Releases: main
Change-Id: Iaf95886d82a417df5e9149d0d3296baaf6fb3b9c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74966
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Guido Schmechel <guido.schmechel@brandung.de>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Guido Schmechel <guido.schmechel@brandung.de>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
---
 .../DatabaseSystemLanguageRows.php            |  1 +
 .../DatabaseSystemLanguageRowsTest.php        |  6 ++++
 ...8-SupportOfLanguageDirectionInCkeditor.rst | 28 +++++++++++++++++++
 .../Classes/Form/Element/RichTextElement.php  | 14 ++++++++++
 4 files changed, 49 insertions(+)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/12.0/Feature-97778-SupportOfLanguageDirectionInCkeditor.rst

diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseSystemLanguageRows.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseSystemLanguageRows.php
index bd9a59efc9c5..e6de1296a1b4 100644
--- a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseSystemLanguageRows.php
+++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseSystemLanguageRows.php
@@ -61,6 +61,7 @@ class DatabaseSystemLanguageRows implements FormDataProviderInterface
                 'title' => $language->getTitle(),
                 'iso' => $iso,
                 'flagIconIdentifier' => $language->getFlagIdentifier(),
+                'direction' => $language->getDirection(),
             ];
 
             if (empty($iso)) {
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseSystemLanguageRowsTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseSystemLanguageRowsTest.php
index 6976fecf0175..9cbdf93501bb 100644
--- a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseSystemLanguageRowsTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseSystemLanguageRowsTest.php
@@ -59,15 +59,18 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
         $siteLanguageMinusOne->getLanguageId()->willReturn(-1);
         $siteLanguageMinusOne->getTitle()->willReturn('All');
         $siteLanguageMinusOne->getFlagIdentifier()->willReturn('flags-multiple');
+        $siteLanguageMinusOne->getDirection()->willReturn('');
         $siteLanguageZero = $this->prophesize(SiteLanguage::class);
         $siteLanguageZero->getLanguageId()->willReturn(0);
         $siteLanguageZero->getTitle()->willReturn('English');
         $siteLanguageZero->getFlagIdentifier()->willReturn('empty-empty');
+        $siteLanguageZero->getDirection()->willReturn('ltr');
         $siteLanguageOne = $this->prophesize(SiteLanguage::class);
         $siteLanguageOne->getLanguageId()->willReturn(1);
         $siteLanguageOne->getTitle()->willReturn('Dutch');
         $siteLanguageOne->getFlagIdentifier()->willReturn('flag-nl');
         $siteLanguageOne->getTwoLetterIsoCode()->willReturn('NL');
+        $siteLanguageOne->getDirection()->willReturn('rtl');
         $siteLanguages = [
             $siteLanguageMinusOne->reveal(),
             $siteLanguageZero->reveal(),
@@ -85,18 +88,21 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
                     'title' => 'All',
                     'iso' => 'DEF',
                     'flagIconIdentifier' => 'flags-multiple',
+                    'direction' => '',
                 ],
                 0 => [
                     'uid' => 0,
                     'title' => 'English',
                     'iso' => 'DEF',
                     'flagIconIdentifier' => 'empty-empty',
+                    'direction' => 'ltr',
                 ],
                 1 => [
                     'uid' => 1,
                     'title' => 'Dutch',
                     'iso' => 'NL',
                     'flagIconIdentifier' => 'flag-nl',
+                    'direction' => 'rtl',
                 ],
             ],
         ];
diff --git a/typo3/sysext/core/Documentation/Changelog/12.0/Feature-97778-SupportOfLanguageDirectionInCkeditor.rst b/typo3/sysext/core/Documentation/Changelog/12.0/Feature-97778-SupportOfLanguageDirectionInCkeditor.rst
new file mode 100644
index 000000000000..0d2a2aae6189
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/12.0/Feature-97778-SupportOfLanguageDirectionInCkeditor.rst
@@ -0,0 +1,28 @@
+.. include:: /Includes.rst.txt
+
+.. _feature-97778-1655732248:
+
+===========================================================
+Feature: #97778 - Support of language direction in ckeditor
+===========================================================
+
+See :issue:`97778`
+
+Description
+===========
+
+The configuration `contentsLangDirection` of the ckeditor is used to define the
+direction of the content. It is now filled by the direction defined in the site
+language of the current element.
+
+As fallback the Page TsConfig configuration :typoscript:`RTE.config.contentsLanguageDirection = rtl`
+can be used.
+
+
+Impact
+======
+
+The direction of the content inside the RichText element is defined by the
+language of record.
+
+.. index:: Backend, RTE, ext:rte_ckeditor
diff --git a/typo3/sysext/rte_ckeditor/Classes/Form/Element/RichTextElement.php b/typo3/sysext/rte_ckeditor/Classes/Form/Element/RichTextElement.php
index fba817c69eb1..d83a00680b07 100644
--- a/typo3/sysext/rte_ckeditor/Classes/Form/Element/RichTextElement.php
+++ b/typo3/sysext/rte_ckeditor/Classes/Form/Element/RichTextElement.php
@@ -195,6 +195,19 @@ class RichTextElement extends AbstractFormElement
         return $contentLanguage;
     }
 
+    /**
+     * Determine the language direction
+     */
+    protected function getLanguageDirectionOfContent(): string
+    {
+        $currentLanguageUid = ($this->data['databaseRow']['sys_language_uid'] ?? 0);
+        if (is_array($currentLanguageUid)) {
+            $currentLanguageUid = $currentLanguageUid[0];
+        }
+        $contentLanguageUid = (int)max($currentLanguageUid, 0);
+        return $this->data['systemLanguageRows'][$contentLanguageUid]['direction'] ?? '';
+    }
+
     /**
      * Gets the JavaScript code for CKEditor module
      * Compiles the configuration, and then adds plugins
@@ -364,6 +377,7 @@ class RichTextElement extends AbstractFormElement
             $configuration['language'] = $userLang === 'default' ? 'en' : $userLang;
         }
         $configuration['contentsLanguage'] = $this->getLanguageIsoCodeOfContent();
+        $configuration['contentsLangDirection'] = $this->getLanguageDirectionOfContent();
 
         // Replace all label references
         $configuration = $this->replaceLanguageFileReferences($configuration);
-- 
GitLab