Commit ad3267ec authored by Christian Eßl's avatar Christian Eßl Committed by Daniel Goerz
Browse files

[FEATURE] Provide a way to sort form lists in ext:form

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's avatarSusanne Moog <look@susi.dev>
Reviewed-by: Daniel Goerz's avatarDaniel Goerz <daniel.goerz@posteo.de>
Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
Tested-by: Daniel Goerz's avatarDaniel Goerz <daniel.goerz@posteo.de>
parent c13aa8be
.. 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
......@@ -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);
}
}
......@@ -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/
......
......@@ -15,6 +15,8 @@ Full default configuration
10: '1:/form_definitions/'
allowSaveToExtensionPaths: false
allowDeleteFromExtensionPaths: false
sortByKeys: ['name', 'fileUid']
sortAscending: true
prototypes:
standard:
formElementsDefinition:
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment