[BUGFIX] Add FieldWizard for other language thumbnails 33/65533/4
authorOliver Bartsch <bo@cedev.de>
Tue, 1 Sep 2020 22:34:35 +0000 (00:34 +0200)
committerDaniel Goerz <daniel.goerz@posteo.de>
Sun, 20 Sep 2020 15:59:33 +0000 (17:59 +0200)
Instead of displaying the JSON value of the `crop` field, which
has no value for editors, the otherLanguageContent wizard is
replaced with a new wizard which renders cropped thumbnails
for other languages by respecting the corresponding cropping
configuration.

Resolves: #77815
Releases: master, 10.4
Change-Id: I23ed0df13593330b303af1ec71f5a1c49476cf1e
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65533
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Jörg Bösche <typo3@joergboesche.de>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
typo3/sysext/backend/Classes/Form/Element/ImageManipulationElement.php
typo3/sysext/backend/Classes/Form/FieldWizard/OtherLanguageThumbnails.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Form/NodeFactory.php

index ddb001a..926dd66 100644 (file)
@@ -105,8 +105,8 @@ class ImageManipulationElement extends AbstractFormElement
         'localizationStateSelector' => [
             'renderType' => 'localizationStateSelector',
         ],
-        'otherLanguageContent' => [
-            'renderType' => 'otherLanguageContent',
+        'otherLanguageThumbnails' => [
+            'renderType' => 'otherLanguageThumbnails',
             'after' => [
                 'localizationStateSelector'
             ],
@@ -114,7 +114,7 @@ class ImageManipulationElement extends AbstractFormElement
         'defaultLanguageDifferences' => [
             'renderType' => 'defaultLanguageDifferences',
             'after' => [
-                'otherLanguageContent',
+                'otherLanguageThumbnails',
             ],
         ],
     ];
diff --git a/typo3/sysext/backend/Classes/Form/FieldWizard/OtherLanguageThumbnails.php b/typo3/sysext/backend/Classes/Form/FieldWizard/OtherLanguageThumbnails.php
new file mode 100644 (file)
index 0000000..8a6b092
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+namespace TYPO3\CMS\Backend\Form\FieldWizard;
+
+use TYPO3\CMS\Backend\Form\AbstractNode;
+use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
+use TYPO3\CMS\Core\Imaging\Icon;
+use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
+use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
+use TYPO3\CMS\Core\Resource\ProcessedFile;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Render cropped thumbnails for default and additional preview
+ * languages by respecting the corresponding cropping configuration.
+ *
+ * @internal
+ */
+class OtherLanguageThumbnails extends AbstractNode
+{
+    /**
+     * Render cropped thumbnails from other language rows
+     *
+     * @return array
+     */
+    public function render(): array
+    {
+        $result = $this->initializeResultArray();
+        $fieldConfig = $this->data['parameterArray']['fieldConf'];
+        $l10nDisplay = $fieldConfig['l10n_display'] ?? '';
+        $cropVariants = $fieldConfig['config']['cropVariants'] ?? ['default' => []];
+        $defaultLanguageRow = $this->data['defaultLanguageRow'] ?? null;
+
+        if (!is_array($defaultLanguageRow)
+            || !is_array($cropVariants)
+            || $cropVariants === []
+            || $fieldConfig['config']['type'] !== 'imageManipulation'
+            || GeneralUtility::inList($l10nDisplay, 'hideDiff')
+            || GeneralUtility::inList($l10nDisplay, 'defaultAsReadonly')
+        ) {
+            return $result;
+        }
+
+        $html = [];
+        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
+        $languages = [$defaultLanguageRow['sys_language_uid'] => $defaultLanguageRow] + ($this->data['additionalLanguageRows'] ?? []);
+
+        foreach ($languages as $sysLanguageUid => $languageRow) {
+            $file = null;
+            $fileUid = (int)($languageRow['uid_local'] ?? 0);
+
+            if (!$fileUid || $languageRow['table_local'] !== 'sys_file') {
+                continue;
+            }
+
+            try {
+                $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileUid);
+            } catch (FileDoesNotExistException|\InvalidArgumentException $e) {
+                continue;
+            }
+
+            $processedImages = [];
+            $cropVariantCollection = CropVariantCollection::create((string)($languageRow['crop'] ?? ''), $cropVariants);
+
+            foreach (array_keys($cropVariants) as $variant) {
+                $processedImages[] = FormEngineUtility::getIconHtml(
+                    $file
+                        ->process(
+                            ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
+                            [
+                                'maxWidth' => '145',
+                                'maxHeight' => '45',
+                                'crop' => $cropVariantCollection->getCropArea($variant)->makeAbsoluteBasedOnFile($file)
+                            ]
+                        )
+                        ->getPublicUrl(),
+                    $languageRow['title'] ?? $file->getProperty('title') ?? '',
+                    $languageRow['alternative'] ?? $file->getProperty('alternative') ?? ''
+                );
+            }
+
+            if ($processedImages !== []) {
+                $iconIdentifier = $this->data['systemLanguageRows'][(int)$sysLanguageUid]['flagIconIdentifier'] ?? 'flags-multiple';
+                $html[] = '<div class="t3-form-original-language">';
+                $html[] =   $iconFactory->getIcon($iconIdentifier, Icon::SIZE_SMALL)->render();
+                $html[] =   implode(LF, $processedImages);
+                $html[] = '</div>';
+            }
+        }
+
+        $result['html'] = implode(LF, $html);
+        return $result;
+    }
+}
index d0da7cf..e4bf7a7 100644 (file)
@@ -109,6 +109,7 @@ class NodeFactory
         'defaultLanguageDifferences' => FieldWizard\DefaultLanguageDifferences::class,
         'localizationStateSelector' => FieldWizard\LocalizationStateSelector::class,
         'otherLanguageContent' => FieldWizard\OtherLanguageContent::class,
+        'otherLanguageThumbnails' => FieldWizard\OtherLanguageThumbnails::class,
         'recordsOverview' => FieldWizard\RecordsOverview::class,
         'selectIcons' => FieldWizard\SelectIcons::class,
         'tableList' => FieldWizard\TableList::class,