[TASK] Use dedicated database field for `textmedia` media references 28/44228/6
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 23 Oct 2015 14:31:11 +0000 (16:31 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 24 Oct 2015 16:07:19 +0000 (18:07 +0200)
To avoid side effects and make maintenance easier the textmedia content
element from EXT:fluid_styled_content uses a dedicated field for media
file references This avoids side effects with EXT:css_styled_content as
the child TCA of the file references is no longer depended on the parent
CType configuration.

This also fixes the wrong media palette being shown in the backend as
the AJAX call to fetch the inline record does not have access to th
parent record.

An upgrade wizard is provided to migrate the file references to the new
assets database field.

Resolves: #70957
Releases: master
Change-Id: I32d7284e2098b4ab74f10f1fc8c7d07a7553511d
Reviewed-on: https://review.typo3.org/44228
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/fluid_styled_content/Classes/Hooks/TextmediaPreviewRenderer.php
typo3/sysext/fluid_styled_content/Configuration/TCA/Overrides/tt_content.php
typo3/sysext/fluid_styled_content/Configuration/TypoScript/Static/Setup/lib.fluidContent.ts
typo3/sysext/fluid_styled_content/Configuration/TypoScript/Static/setup.txt
typo3/sysext/fluid_styled_content/Resources/Private/Language/Database.xlf
typo3/sysext/fluid_styled_content/ext_tables.sql
typo3/sysext/install/Classes/Updates/ContentTypesToTextMediaUpdate.php
typo3/sysext/install/Classes/Updates/MigrateMediaToAssetsForTextMediaCe.php [new file with mode: 0644]
typo3/sysext/install/ext_localconf.php

index 8e5bf7e..4975587 100644 (file)
@@ -46,10 +46,10 @@ class TextmediaPreviewRenderer implements PageLayoutViewDrawItemHookInterface
                 $itemContent .= $parentObject->linkEditContent($parentObject->renderText($row['bodytext']), $row) . '<br />';
             }
 
-            if ($row['media']) {
-                $itemContent .= $parentObject->linkEditContent($parentObject->getThumbCodeUnlinked($row, 'tt_content', 'media'), $row) . '<br />';
+            if ($row['assets']) {
+                $itemContent .= $parentObject->linkEditContent($parentObject->getThumbCodeUnlinked($row, 'tt_content', 'assets'), $row) . '<br />';
 
-                $fileReferences = BackendUtility::resolveFileReferences('tt_content', 'media', $row);
+                $fileReferences = BackendUtility::resolveFileReferences('tt_content', 'assets', $row);
 
                 if (!empty($fileReferences)) {
                     $linkedContent = '';
index 6f17548..18789b5 100644 (file)
@@ -40,7 +40,7 @@ call_user_func(function () {
                                --palette--;' . $frontendLanguageFilePrefix . 'palette.header;header,
                                bodytext;' . $frontendLanguageFilePrefix . 'bodytext_formlabel;;richtext:rte_transform[mode=ts_css],
                        --div--;' . $frontendLanguageFilePrefix . 'tabs.media,
-                               media,
+                               assets,
                                --palette--;' . $frontendLanguageFilePrefix . 'palette.imagelinks;imagelinks,
                        --div--;' . $frontendLanguageFilePrefix . 'tabs.appearance,
                                layout;' . $frontendLanguageFilePrefix . 'layout_formlabel,
@@ -52,19 +52,6 @@ call_user_func(function () {
                                --palette--;' . $frontendLanguageFilePrefix . 'palette.access;access,
                        --div--;' . $frontendLanguageFilePrefix . 'tabs.extended
                ',
-        'columnsOverrides' => [
-            'media' => [
-                'label' => $languageFilePrefix . 'tt_content.media_references',
-                'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('media', [
-                    'appearance' => [
-                        'createNewRelationLinkTitle' => $languageFilePrefix . 'tt_content.media_references.addFileReference'
-                    ],
-                    // custom configuration for displaying fields in the overlay/reference table
-                    // behaves the same as the image field.
-                    'foreign_types' => $GLOBALS['TCA']['tt_content']['columns']['image']['config']['foreign_types']
-                ], $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'])
-            ]
-        ]
     ];
 
     // Add category tab when categories column exits
@@ -117,7 +104,18 @@ call_user_func(function () {
                 ],
                 'default' => 0
             ]
-        ]
+        ],
+        'assets' => [
+            'label' => $languageFilePrefix . 'tt_content.asset_references',
+            'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('assets', [
+                'appearance' => [
+                    'createNewRelationLinkTitle' => $languageFilePrefix . 'tt_content.asset_references.addFileReference'
+                ],
+                // custom configuration for displaying fields in the overlay/reference table
+                // behaves the same as the image field.
+                'foreign_types' => $GLOBALS['TCA']['tt_content']['columns']['image']['config']['foreign_types']
+            ], $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'])
+        ],
     ];
 
     \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('tt_content', $additionalColumns);
index 260d191..9e8e0f8 100644 (file)
@@ -174,7 +174,7 @@ tt_content {
                dataProcessing {
                        10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
                        10 {
-                               references.fieldName = media
+                               references.fieldName = assets
                        }
                        20 = TYPO3\CMS\Frontend\DataProcessing\GalleryProcessor
                        20 {
@@ -187,7 +187,7 @@ tt_content {
                }
                stdWrap {
                        # Setup the edit icon for content element "textmedia"
-                       editIcons = tt_content: header [header_layout], bodytext, media [imageorient|imagewidth|imageheight], [imagecols|imageborder], image_zoom
+                       editIcons = tt_content: header [header_layout], bodytext, assets [imageorient|imagewidth|imageheight], [imagecols|imageborder], image_zoom
                        editIcons {
                                iconTitle.data = LLL:EXT:fluid_styled_content/Resources/Private/Language/FrontendEditing.xlf:editIcon.textmedia
                        }
index 1b3e6e8..138c578 100644 (file)
                        <trans-unit id="tt_content.palette.mediaAdjustments">
                                <source>Media Adjustments</source>
                        </trans-unit>
-                       <trans-unit id="tt_content.media_references">
+                       <trans-unit id="tt_content.asset_references">
                                <source>Media elements</source>
                        </trans-unit>
-                       <trans-unit id="tt_content.media_references.addFileReference">
+                       <trans-unit id="tt_content.asset_references.addFileReference">
                                <source>Add media file</source>
                        </trans-unit>
                </body>
index 163cc3f..a1c5df0 100644 (file)
@@ -4,5 +4,6 @@
 CREATE TABLE tt_content (
        bullets_type tinyint(3) unsigned DEFAULT '0' NOT NULL,
        uploads_description tinyint(1) unsigned DEFAULT '0' NOT NULL,
-       uploads_type tinyint(3) unsigned DEFAULT '0' NOT NULL
+       uploads_type tinyint(3) unsigned DEFAULT '0' NOT NULL,
+       assets int(11) unsigned DEFAULT '0' NOT NULL,
 );
index 3aefbe1..ccb1ee1 100644 (file)
@@ -97,9 +97,9 @@ class ContentTypesToTextMediaUpdate extends AbstractUpdate
                        AND sys_file_reference.tablenames =\'tt_content\'
                        AND sys_file_reference.fieldname = \'image\'
                        SET tt_content.CType = \'textmedia\',
-                       tt_content.media = image,
-                       tt_content.image = \'\',
-                       sys_file_reference.fieldname = \'media\'
+                       tt_content.assets = image,
+                       tt_content.image = 0,
+                       sys_file_reference.fieldname = \'assets\'
                        WHERE
                        tt_content.CType = \'textpic\'
                        OR tt_content.CType = \'image\'
diff --git a/typo3/sysext/install/Classes/Updates/MigrateMediaToAssetsForTextMediaCe.php b/typo3/sysext/install/Classes/Updates/MigrateMediaToAssetsForTextMediaCe.php
new file mode 100644 (file)
index 0000000..d389279
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+namespace TYPO3\CMS\Install\Updates;
+
+/*
+ * 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!
+ */
+
+/**
+ * Migrate CTypes 'textmedia' to use 'assets' field instead of 'media'
+ */
+class MigrateMediaToAssetsForTextMediaCe extends AbstractUpdate
+{
+    /**
+     * @var string
+     */
+    protected $title = 'Migrate CTypes textmedia database field "media" to "assets"';
+
+    /**
+     * Checks if an update is needed
+     *
+     * @param string &$description The description for the update
+     * @return bool Whether an update is needed (TRUE) or not (FALSE)
+     */
+    public function checkForUpdate(&$description)
+    {
+        $updateNeeded = true;
+
+        if ($this->isWizardDone()) {
+            $updateNeeded = false;
+        } else {
+            // No need to join the sys_file_references table here as we can rely on the reference
+            // counter to check if the wizards has any textmedia content elements to upgrade.
+            $textmediaCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
+                'uid',
+                'tt_content',
+                'CType = \'textmedia\' AND media > 0'
+            );
+
+            if ($textmediaCount === 0) {
+                $updateNeeded = false;
+                $this->markWizardAsDone();
+            }
+        }
+
+        $description = 'The extension "fluid_styled_content" is using a new database field for mediafile references. ' .
+            'This update wizard migrates these old references to use the new database field.';
+
+        return $updateNeeded;
+    }
+
+    /**
+     * Performs the database update if old mediafile references are available
+     *
+     * @param array &$databaseQueries Queries done in this update
+     * @param mixed &$customMessages Custom messages
+     * @return bool
+     */
+    public function performUpdate(array &$databaseQueries, &$customMessages)
+    {
+        $databaseConnection = $this->getDatabaseConnection();
+
+        // Update 'textmedia'
+        $query = '
+                       UPDATE sys_file_reference
+                       LEFT JOIN tt_content
+                       ON sys_file_reference.uid_foreign = tt_content.uid
+                       AND sys_file_reference.tablenames =\'tt_content\'
+                       AND sys_file_reference.fieldname = \'media\'
+                       SET tt_content.assets = tt_content.media,
+                       tt_content.media = 0,
+                       sys_file_reference.fieldname = \'assets\'
+                       WHERE
+                       tt_content.CType = \'textmedia\'
+                       AND tt_content.media > 0
+               ';
+        $databaseConnection->sql_query($query);
+
+        // Store last executed query
+        $databaseQueries[] = str_replace(chr(10), ' ', $query);
+        // Check for errors
+        if ($databaseConnection->sql_error()) {
+            $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
+            return false;
+        }
+
+        $this->markWizardAsDone();
+
+        return true;
+    }
+}
index a1e3c93..622d23b 100644 (file)
@@ -12,6 +12,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['tableCType']
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\FileListIsStartModuleUpdate::class] = \TYPO3\CMS\Install\Updates\FileListIsStartModuleUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['textmediaCType'] = \TYPO3\CMS\Install\Updates\ContentTypesToTextMediaUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\WorkspacesNotificationSettingsUpdate::class] = \TYPO3\CMS\Install\Updates\WorkspacesNotificationSettingsUpdate::class;
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['textmediaAssets'] = \TYPO3\CMS\Install\Updates\MigrateMediaToAssetsForTextMediaCe::class;
 
 $signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
 $signalSlotDispatcher->connect(