For coders TYPO3 Tech Corner

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.

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

Lesson cancellation - and now? New portal for the Bavarian school system.

For the Bavarian school system, we developed a new web app based on an existing but very outdated portal. The portal reports lesson cancellations throughout Bavaria and registered users are informed...

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

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news