[FEATURE] Introduce hook in BackendUserAuthentication::getDefaultUploadFolder 70/42470/7
authorFrans Saris <franssaris@gmail.com>
Thu, 1 Oct 2015 12:56:52 +0000 (14:56 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Tue, 6 Oct 2015 11:24:53 +0000 (13:24 +0200)
A hook is introduced to manipulate the default upload folder of FAL
inline upload and RTE text fields.

Change-Id: Ib497b02c90d3217062d2abb55eef693e5c445c46
Resolves: #68895
Releases: master
Reviewed-on: http://review.typo3.org/42470
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/backend/Classes/Form/Container/InlineControlContainer.php
typo3/sysext/core/Classes/Authentication/BackendUserAuthentication.php
typo3/sysext/core/Classes/Html/RteHtmlParser.php
typo3/sysext/core/Documentation/Changelog/master/Feature-68895-IntroducedHookInBackendUserAuthenticationgetDefaultUploadFolder.rst [new file with mode: 0644]

index 05ec7c5..66be425 100644 (file)
@@ -550,7 +550,11 @@ class InlineControlContainer extends AbstractContainer {
                        $onlineMediaAllowed = array_intersect($allowedArray, $onlineMediaAllowed);
                }
                if ($showUpload && $isDirectFileUploadEnabled) {
-                       $folder = $backendUser->getDefaultUploadFolder();
+                       $folder = $backendUser->getDefaultUploadFolder(
+                               $this->data['parentPageRow']['uid'],
+                               $this->data['tableName'],
+                               $this->data['fieldName']
+                       );
                        if (
                                $folder instanceof Folder
                                && $folder->checkActionPermission('add')
index 5e81df3..e40368c 100644 (file)
@@ -1771,7 +1771,7 @@ class BackendUserAuthentication extends \TYPO3\CMS\Core\Authentication\AbstractU
         * Returns a \TYPO3\CMS\Core\Resource\Folder object that is used for uploading
         * files by default.
         * This is used for RTE and its magic images, as well as uploads
-        * in the TCEforms fields, unless otherwise configured (will be added in the future)
+        * in the TCEforms fields.
         *
         * The default upload folder for a user is the defaultFolder on the first
         * filestorage/filemount that the user can access and to which files are allowed to be added
@@ -1779,9 +1779,12 @@ class BackendUserAuthentication extends \TYPO3\CMS\Core\Authentication\AbstractU
         *
         * options.defaultUploadFolder = 3:myfolder/yourfolder/
         *
+        * @param int $pid PageUid
+        * @param string $table Table name
+        * @param string $field Field name
         * @return \TYPO3\CMS\Core\Resource\Folder|boolean The default upload folder for this user
         */
-       public function getDefaultUploadFolder() {
+       public function getDefaultUploadFolder($pid = NULL, $table = NULL, $field = NULL) {
                $uploadFolder = $this->getTSConfigVal('options.defaultUploadFolder');
                if ($uploadFolder) {
                        $uploadFolder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($uploadFolder);
@@ -1817,6 +1820,20 @@ class BackendUserAuthentication extends \TYPO3\CMS\Core\Authentication\AbstractU
                                }
                        }
                }
+
+               // HOOK: getDefaultUploadFolder
+               if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['getDefaultUploadFolder'])) {
+                       foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['getDefaultUploadFolder'] as $_funcRef) {
+                               $_params = array(
+                                       'uploadFolder' => $uploadFolder,
+                                       'pid' => $pid,
+                                       'table' => $table,
+                                       'field' => $field,
+                               );
+                               $uploadFolder = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+                       }
+               }
+
                if ($uploadFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
                        return $uploadFolder;
                } else {
index aeb66e1..f26f880 100644 (file)
@@ -408,7 +408,8 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                                        if (GeneralUtility::inList('gif,png,jpeg,jpg', strtolower($pI['extension']))) {
                                                                $fileName = GeneralUtility::shortMD5($absoluteUrl) . '.' . $pI['extension'];
                                                                // We insert this image into the user default upload folder
-                                                               $folder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
+                                                               list($table, $field) = explode(':', $this->elRef);
+                                                               $folder = $GLOBALS['BE_USER']->getDefaultUploadFolder($this->recPid, $table, $field);
                                                                $fileObject = $folder->createFile($fileName)->setContents($externalFile);
                                                                $imageConfiguration = array(
                                                                        'width' => $attribArray['width'],
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-68895-IntroducedHookInBackendUserAuthenticationgetDefaultUploadFolder.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-68895-IntroducedHookInBackendUserAuthenticationgetDefaultUploadFolder.rst
new file mode 100644 (file)
index 0000000..d8a3b22
--- /dev/null
@@ -0,0 +1,67 @@
+======================================================================================
+Feature: #68895 - Introduced hook in BackendUserAuthentication::getDefaultUploadFolder
+======================================================================================
+
+Description
+===========
+
+It is now possible to change the upload folder returned by ``BackendUserAuthentication::getDefaultUploadFolder()`` by
+registering a hook. This makes it possible to set a different upload folder for fields with direct upload enabled in the
+backend.
+
+
+Register own getDefaultUploadFolder hook
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+To use your own hook to manipulate the upload folder you need to register the function in ``ext_localconf.php`` of
+your extension.
+
+.. code-block:: php
+
+       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['getDefaultUploadFolder'][] =
+               \Vendor\MyExtension\Hooks\DefaultUploadFolder::class . '->getDefaultUploadFolder';
+
+
+Example getDefaultUploadFolder hook
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+.. code-block:: php
+
+       <?php
+       namespace Vendor\MyExtension\Hooks;
+
+       use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+       use TYPO3\CMS\Core\Resource\Folder;
+
+       /**
+        * Class DefaultUploadFolder
+        */
+       class DefaultUploadFolder {
+
+               /**
+                * Get default upload folder
+                *
+                * If there is a folder present with the same name as the last part of the table name use that folder.
+                *
+                * @param array $params
+                * @param BackendUserAuthentication $backendUserAuthentication
+                * @return Folder
+                */
+               public function getDefaultUploadFolder($params, BackendUserAuthentication $backendUserAuthentication) {
+
+                       /** @var Folder $uploadFolder */
+                       $uploadFolder = $params['uploadFolder'];
+                       $pid = $params['pid'];
+                       $table = $params['table'];
+                       $field = $params['field'];
+
+                       $matches = [];
+                       if (!empty($uploadFolder) && preg_match('/_([a-z]+)$/', $table, $matches)) {
+                               $folderName = $matches[1];
+                               if ($uploadFolder->hasFolder($folderName)) {
+                                       $uploadFolder = $uploadFolder->getSubfolder($folderName);
+                               }
+                       }
+                       return $uploadFolder;
+               }
+       }
\ No newline at end of file