diff --git a/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizard.php b/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizard.php index b89c2b12e464cea24169d282d11c23771b5babe5..868f728c533e161890c9f1e71b418b359129bdb1 100644 --- a/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizard.php +++ b/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizard.php @@ -260,9 +260,9 @@ class SuggestWizard $maxItems = isset($config['maxItemsInResultList']) ? $config['maxItemsInResultList'] : 10; $maxItems = min(count($resultRows), $maxItems); - $listItems = $this->createListItemsFromResultRow($resultRows, $maxItems); + array_splice($resultRows, $maxItems); - $response->getBody()->write(json_encode($listItems)); + $response->getBody()->write(json_encode(array_values($resultRows))); return $response; } @@ -444,35 +444,6 @@ class SuggestWizard return $config; } - /** - * Creates a list of <li> elements from a list of results returned by the receiver. - * - * @param array $resultRows - * @param int $maxItems - * @return array - */ - protected function createListItemsFromResultRow(array $resultRows, $maxItems) - { - if (empty($resultRows)) { - return []; - } - $listItems = []; - - // traverse all found records and sort them - $rowsSort = []; - foreach ($resultRows as $key => $row) { - $rowsSort[$key] = $row['text']; - } - asort($rowsSort); - $rowsSort = array_keys($rowsSort); - - // put together the selector entries - for ($i = 0; $i < $maxItems; ++$i) { - $listItems[] = $resultRows[$rowsSort[$i]]; - } - return $listItems; - } - /** * Checks the given field configuration for the tables that should be used for querying and returns them as an * array. diff --git a/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizardDefaultReceiver.php b/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizardDefaultReceiver.php index 5ca700fe94a7f3c4c1b3771b8475dc2ecfc8c4f6..03beae1ef70df6d04a9b85a7d1a119ec2ba7e10b 100644 --- a/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizardDefaultReceiver.php +++ b/typo3/sysext/backend/Classes/Form/Wizard/SuggestWizardDefaultReceiver.php @@ -50,13 +50,6 @@ class SuggestWizardDefaultReceiver */ protected $mmForeignTable = ''; - /** - * The statement by which records will be ordered - * - * @var string - */ - protected $orderByStatement = ''; - /** * Configuration for this selector from TSconfig * @@ -158,7 +151,6 @@ class SuggestWizardDefaultReceiver $this->prepareOrderByStatement(); $result = $this->queryBuilder->select('*') ->from($this->table) - ->orderBy($this->orderByStatement) ->setFirstResult($start) ->setMaxResults(50) ->execute(); @@ -306,8 +298,13 @@ class SuggestWizardDefaultReceiver */ protected function prepareOrderByStatement() { - if ($GLOBALS['TCA'][$this->table]['ctrl']['label']) { - $this->orderByStatement = $GLOBALS['TCA'][$this->table]['ctrl']['label']; + if (empty($this->config['orderBy'])) { + $this->queryBuilder->addOrderBy($GLOBALS['TCA'][$this->table]['ctrl']['label']); + } else { + foreach (QueryHelper::parseOrderBy($this->config['orderBy']) as $orderPair) { + list($fieldName, $order) = $orderPair; + $this->queryBuilder->addOrderBy($fieldName, $order); + } } } diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-78523-SuggestWizardProvidesOptionToDefineOrderingOfResults.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-78523-SuggestWizardProvidesOptionToDefineOrderingOfResults.rst new file mode 100644 index 0000000000000000000000000000000000000000..a5072f0f40ce4dc23b8732c11fae1b8d3ec30717 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-78523-SuggestWizardProvidesOptionToDefineOrderingOfResults.rst @@ -0,0 +1,42 @@ +.. include:: ../../Includes.txt + +============================================================================== +Feature: #78523 - Suggest wizard provides option to define ordering of results +============================================================================== + +See :issue:`78523` + +Description +=========== + +It is now possible to set the ordering of results delivered by the suggest wizard. + +The new option is called php:`orderBy => 'somefield ASC'` and can hold the usual SQL order by definition. + +Example TCA configuration for ext:news suggest wizard returning the related articles sorted by datetime: + +.. code-block:: php + + 'config' => [ + 'type' => 'group', + 'internal_type' => 'db', + 'allowed' => 'tx_news_domain_model_news', + 'foreign_table' => 'tx_news_domain_model_news', + 'MM_opposite_field' => 'related_from', + 'size' => 5, + 'minitems' => 0, + 'maxitems' => 100, + 'MM' => 'tx_news_domain_model_news_related_mm', + 'wizards' => [ + 'suggest' => [ + 'type' => 'suggest', + 'default' => [ + 'searchWholePhrase' => true, + 'addWhere' => ' AND tx_news_domain_model_news.uid != ###THIS_UID###', + 'orderBy => 'datetime DESC', + ] + ], + ], + ] + +.. index:: Backend, TCA