For coders TYPO3 Tech Corner

Pass hidden objects to Extbase actions

Pass hidden objects to Extbase actions

Surely you have also wanted to edit, save or manage a hidden Extbase object in some other way? Unfortunately this no longer works as usual in the Extbase Actions if hidden = 1 is set in the table. We'll show you how to get around the problem.

In the following example in the Extbase controller, it should be possible to edit and save objects of the type event. The problem with this is that these objects are hidden in this example (the field hidden is defined by TCA - the value is then 1).

Pass an Integer Value to the action

Some of you are probably familiar with this workaround. Instead of an object, you can also transfer an integer and then convert this manually into an object using a separate repository function.

<?php declare(strict_types=1); namespace UniKn\UknCalendarize\Controller; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException; use UniKn\UknCalendarize\Domain\Model\Event; use UniKn\UknCalendarize\Property\TypeConverters\HiddenEventConverter; /** * Class FrontendController */ class FrontendController extends ActionController { ... /** * @param int $event * @return void */ public function editAction(int $event): void { $event = $this->eventRepository->findHiddenByUid($event); $this->view->assignMultiple([ 'event' => $event ]); } /** * @param int $event * @return void */ public function updateAction(int $event): void { $event = $this->eventRepository->findHiddenByUid($event); // Do something } }

The repository for this could look something like this:

<?php declare(strict_types=1); namespace UniKn\UknCalendarize\Domain\Repository; use HDNET\Calendarize\Domain\Repository\AbstractRepository; use UniKn\UknCalendarize\Domain\Model\Event; /** * Class EventRepository */ class EventRepository extends AbstractRepository { /** * @param int $uid * @return Event|null */ public function findHiddenByUid(int $uid): ?Event { $query = $this->createQuery(); $query->getQuerySettings()->setIgnoreEnableFields(true); $query->matching( $query->logicalAnd( [ $query->equals('uid', $uid), $query->equals('hidden', true), $query->equals('deleted', false), ] ) ); /** @var Event $event */ $event = $query->execute()->getFirst(); return $event; } }
Solution with own TypeConverter

You can also transfer hidden objects to an action using your own TypeConverter. The whole thing is more elegant then - example controller:

<?php declare(strict_types=1); namespace UniKn\UknCalendarize\Controller; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException; use UniKn\UknCalendarize\Domain\Model\Event; use UniKn\UknCalendarize\Property\TypeConverters\HiddenEventConverter; /** * Class FrontendController */ class FrontendController extends ActionController { ... /** * @return void * @throws NoSuchArgumentException */ public function initializeEditAction(): void { $this->arguments->getArgument('event')->getPropertyMappingConfiguration()->setTypeConverter( $this->objectManager->get(HiddenEventConverter::class) ); } /** * @param Event $event * @return void */ public function editAction(Event $event): void { $this->view->assignMultiple([ 'event' => $event ]); } /** * @return void * @throws NoSuchArgumentException */ public function initializeUpdateAction(): void { $this->arguments->getArgument('event')->getPropertyMappingConfiguration()->setTypeConverter( $this->objectManager->get(HiddenEventConverter::class) ); } /** * @param Event $event * @param array $uploads * @param array $links * @param bool $confirmation * @return void */ public function updateAction(Event $event): void { // Do something } }

Der TypeConverter:

<?php declare(strict_types=1); namespace UniKn\UknCalendarize\Property\TypeConverters; use TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException; use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException; use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter; use UniKn\UknCalendarize\Domain\Model\Event; /** * Class HiddenEventConverter */ class HiddenEventConverter extends PersistentObjectConverter { /** * @var string */ protected $targetType = Event::class; /** * @var int */ protected $priority = 2; /** * @param mixed $identity * @param string $targetType * @throws TargetNotFoundException * @throws InvalidSourceException * @return Event */ protected function fetchObjectFromPersistence($identity, string $targetType): object { if (ctype_digit((string)$identity)) { $query = $this->persistenceManager->createQueryForType($targetType); $query->getQuerySettings()->setIgnoreEnableFields(true); $object = $query->matching($query->equals('uid', $identity))->execute()->getFirst(); } else { throw new InvalidSourceException( 'The identity property "' . $identity . '" is no UID.', 1641904861 ); } if ($object === null) { throw new TargetNotFoundException( sprintf('Object of type %s with identity "%s" not found.', $targetType, print_r($identity, true)), 1641904896 ); } return $object; } }

The original idea for this comes from Helmut in his Gist.

"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