FlexForm data sorted output from a select field

FlexForm data sorted output from a select field

FlexForm makes it easy to extend plugins with fields to control output. Whether these are simple input fields, checkboxes, or child elements using IRRE, it doesn't matter. Everything that goes via TCA configuration goes with FlexForms.

But if you render a select box via selectMultipleSideBySide to give the plugin a selection of records, the value of this field is stored in a string. This string consists of comma separated UIDs of the selected records in the order they were sorted in the select box (for example "4,2,1,3").
Now you want to have these records read out in exactly this order in the frontend and encounter the problem that there is no suitable query in Extbase to do this.

Fortunately, TYPO3 provides an API for Doctrine DBAL that allows us to use the QueryBuilder to unleash appropriate queries on the database. Sure, you could also split the string into an array using explode and then create a single findByUid query for each contained UID, but performance would suffer.

<?php declare(strict_types=1); namespace Vendor\Package\Domain\Repository; use Vendor\Package\Domain\Model\Item; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper; /** * ItemRepository */ class ItemRepository { private const TABLE = 'tx_package_domain_model_item'; /** * @var DataMapper */ protected DataMapper $dataMapper; /** * @var QueryBuilder|null */ protected ?QueryBuilder $queryBuilder = null; /** * Constructor */ public function __construct(DataMapper $dataMapper) { $this->dataMapper = $dataMapper; $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE); } /** * @param string $uidList * @return array */ public function findByUidList(string $uidList): array { $uids = GeneralUtility::intExplode(',', $uidList, true); if (count($uids) === 0) { return []; } $records = $this->queryBuilder ->select('*') ->from(self::TABLE) ->where($this->queryBuilder->expr()->in('uid', $uids)) ->add('orderBy', 'FIELD(uid,' . implode(',', $uids) . ')') ->execute() ->fetchAll(); return $this->dataMapper->map(Item::class, $records); } }

In the findByUidList function, a relatively simple query is extended by an orderBy statement in which the UIDs are passed.

Don't be surprised that at the beginning of the function we first take the string apart via explode, but then for the orderBy we combine it into a similar string via implode. With GeneralUtility::intExplode we not only perform an explode, but cast the values occurring there directly to integers, so we can be sure that there are only UIDs in the string. With the third parameter of intExplode even "empty" values are removed directly.

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news