[FEATURE] Provide a way to sort form lists in ext:form 96/61896/5
authorChristian Eßl <indy.essl@gmail.com>
Sun, 6 Oct 2019 09:04:51 +0000 (11:04 +0200)
committerDaniel Goerz <daniel.goerz@posteo.de>
Sat, 9 Nov 2019 13:58:45 +0000 (14:58 +0100)
Forms in ext:form were previously not sorted in any manner, but just
outputted in the order they were read from the filesystem's directories.

Forms can now be sorted by multiple keys in either ascending or
descending order.
Two new yaml settings were introduced: `sortByKeys` and `sortAscending`.

Resolves: #87798
Releases: master
Change-Id: I2aae2e79cbd39394040a5b1280469a756bf0c7ca
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61896
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
typo3/sysext/core/Documentation/Changelog/master/Feature-87798-ProvideAWayToSortFormListsInExtform.rst [new file with mode: 0644]
typo3/sysext/form/Classes/Mvc/Persistence/FormPersistenceManager.php
typo3/sysext/form/Configuration/Yaml/BaseSetup.yaml
typo3/sysext/form/Documentation/I/Config/configuration/Index.rst
typo3/sysext/form/Documentation/I/Config/persistenceManager/Index.rst

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-87798-ProvideAWayToSortFormListsInExtform.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-87798-ProvideAWayToSortFormListsInExtform.rst
new file mode 100644 (file)
index 0000000..87acc66
--- /dev/null
@@ -0,0 +1,66 @@
+.. include:: ../../Includes.txt
+
+==============================================================
+Feature: #87798 - Provide a way to sort form lists in ext:form
+==============================================================
+
+See :issue:`87798`
+
+Description
+===========
+
+Forms in ext:form were previously not sorted in any manner,
+but just outputted in the order they were read from the filesystem's directories.
+
+Forms can now be sorted by multiple keys in either ascending or descending order.
+Two new settings were introduced: ``sortByKeys`` and ``sortAscending``.
+
+Here is an example configuration,
+that will sort forms by their name first and by their file uid second:
+
+.. code-block:: yaml
+
+   TYPO3:
+        CMS:
+          Form:
+            persistenceManager:
+              sortByKeys: ['name', 'fileUid']
+              sortAscending: true
+
+Valid keys, by which the forms can be sorted, are:
+
+``name``
+   The forms name.
+
+``identifier``
+   The filename.
+
+``fileUid``
+   The files uid.
+
+``persistenceIdentifier``
+   The files location.
+
+   Example: ``1:/form_definitions/contact.form.yaml``
+
+``readOnly``
+   Is the form readonly?
+
+``removable``
+   Is the form removable?
+
+``location``
+   Either `storage` or `extension`
+
+``invalid``
+   Does the form have an error?
+
+Impact
+======
+
+Forms will now initially be sorted by their name first and their file uid second in an ascending order.
+This affects both the form list shown in the form module as well as the ordering of the available select options when creating a new form content element.
+
+To change the sorting, you can override the configuration via YAML as described by the example above.
+
+.. index:: Backend, ext:form
index 8ae91e4..d9003ab 100644 (file)
@@ -331,7 +331,7 @@ class FormPersistenceManager implements FormPersistenceManagerInterface
             }
         }
 
-        return $forms;
+        return $this->sortForms($forms);
     }
 
     /**
@@ -771,4 +771,27 @@ class FormPersistenceManager implements FormPersistenceManagerInterface
     {
         return isset($data['identifier'], $data['type']) && !empty($data['identifier']) && trim($data['type']) === 'Form';
     }
+
+    /**
+     * @param array $forms
+     * @return array
+     */
+    protected function sortForms(array $forms): array
+    {
+        $keys = $this->formSettings['persistenceManager']['sortByKeys'] ?? ['name', 'fileUid'];
+        $ascending = $this->formSettings['persistenceManager']['sortAscending'] ?? true;
+
+        usort($forms, function (array $a, array $b) use ($keys) {
+            foreach ($keys as $key) {
+                if (isset($a[$key]) && isset($b[$key])) {
+                    $diff = strcasecmp((string)$a[$key], (string)$b[$key]);
+                    if ($diff) {
+                        return $diff;
+                    }
+                }
+            }
+        });
+
+        return ($ascending) ? $forms : array_reverse($forms);
+    }
 }
index 4cfe3e8..fdbf691 100644 (file)
@@ -6,6 +6,8 @@ TYPO3:
           10: 1:/form_definitions/
         allowSaveToExtensionPaths: false
         allowDeleteFromExtensionPaths: false
+        sortByKeys: ['name', 'fileUid']
+        sortAscending: true
         #allowedExtensionPaths:
           #10: EXT:example/Resources/Private/Forms/
 
index a32883a..1101c8d 100644 (file)
@@ -15,6 +15,8 @@ Full default configuration
         10: '1:/form_definitions/'
       allowSaveToExtensionPaths: false
       allowDeleteFromExtensionPaths: false
+      sortByKeys: ['name', 'fileUid']
+      sortAscending: true
     prototypes:
       standard:
         formElementsDefinition:
index 079caa3..f94a4e2 100644 (file)
@@ -113,6 +113,94 @@ allowDeleteFromExtensionPaths
       Set this to ``true`` if you want to allow backend users to **delete** forms stored within your own extension.
 
 
+.. _typo3.cms.form.persistencemanager.sortByKeys:
+
+sortByKeys
+-----------------------------
+
+:aspect:`Option path`
+      TYPO3.CMS.Form.persistenceManager.sortByKeys
+
+:aspect:`Data type`
+      array
+
+:aspect:`Needed by`
+      Backend (form manager)
+
+:aspect:`Mandatory`
+      Yes
+
+:aspect:`Default value`
+      .. code-block:: yaml
+
+          persistenceManager:
+             sortByKeys: ['name', 'fileUid']
+
+:aspect:`Good to know`
+      :ref:`Form/ File storages<concepts-form-file-storages>`
+
+:aspect:`Description`
+      The keys by which the forms should be sorted in the Form module and in the form plugin select.
+
+      Valid keys, by which the forms can be sorted, are:
+
+      ``name``
+         The forms name.
+
+      ``identifier``
+         The filename.
+
+      ``fileUid``
+         The files uid.
+
+      ``persistenceIdentifier``
+         The files location.
+
+         Example: ``1:/form_definitions/contact.form.yaml``
+
+      ``readOnly``
+         Is the form readonly?
+
+      ``removable``
+         Is the form removable?
+
+      ``location``
+         Either `storage` or `extension`
+
+      ``invalid``
+         Does the form have an error?
+
+
+.. _typo3.cms.form.persistencemanager.sortAscending:
+
+sortAscending
+-----------------------------
+
+:aspect:`Option path`
+      TYPO3.CMS.Form.persistenceManager.sortAscending
+
+:aspect:`Data type`
+      bool
+
+:aspect:`Needed by`
+      Backend (form manager)
+
+:aspect:`Mandatory`
+      Yes
+
+:aspect:`Default value`
+      .. code-block:: yaml
+
+         persistenceManager:
+           sortAscending: true
+
+:aspect:`Good to know`
+      :ref:`Form/ File storages<concepts-form-file-storages>`
+
+:aspect:`Description`
+      If set to ``true``, the forms will be sorted in ascending, otherwise in descending order.
+
+
 .. _typo3.cms.form.persistencemanager.allowedExtensionPaths:
 
 allowedExtensionPaths