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.

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