Skip to content

Instantly share code, notes, and snippets.

@kitzberger
Forked from helhum/PersistentObjectConverter.php
Last active September 19, 2025 11:24
Show Gist options
  • Save kitzberger/33c3c767de87e8c8abbd1767bcc01ebd to your computer and use it in GitHub Desktop.
Save kitzberger/33c3c767de87e8c8abbd1767bcc01ebd to your computer and use it in GitHub Desktop.
TYPO3 12+13: Extbase TypeConverter to fetch hidden records from persistence (Using this will *always* fetch hidden models of the specified type)
<?php
namespace MyVendor\MyExtension\Property\TypeConverter;
use MyVendor\MyExtension\Domain\Model\MyModel;
use TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException;
use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter;
/**
* TypeConverter to fetch hidden MyModel records from persistence
* This will fetch hidden MyModel models during property mapping
*/
class HiddenMyModelConverter extends PersistentObjectConverter
{
/**
* @var string
*/
protected $targetType = MyModel::class;
/**
* @var int
*/
protected $priority = 2;
/**
* Fetch an object from persistence layer.
* This override ignores enable fields (including hidden flag)
*
* @throws \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
* @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
*/
protected function fetchObjectFromPersistence(mixed $identity, string $targetType): object
{
if (ctype_digit((string)$identity)) {
$query = $this->persistenceManager->createQueryForType($targetType);
$query->getQuerySettings()->setIgnoreEnableFields(true);
$query->getQuerySettings()->setRespectStoragePage(false);
$constraints = $query->equals('uid', $identity);
$object = $query->matching($constraints)->execute()->getFirst();
} else {
throw new InvalidSourceException(
'The identity property "' . $identity . '" is no UID.',
1297931020
);
}
if ($object === null) {
throw new TargetNotFoundException(
'Object with identity "' . print_r($identity, true) . '" not found.',
1297933823
);
}
return $object;
}
}
<?php
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
class MyModelController extends ActionController
{
public function initializeCreateAction(): void
{
$propertyMappingConfig = $this->arguments['myModel']->getPropertyMappingConfiguration();
// Use custom HiddenMyModelConverter to handle hidden records
$propertyMappingConfig
->setTypeConverter(
GeneralUtility::makeInstance(HiddenMyModelConverter::class)
);
}
public function createAction(MyModel $myModel): ResponseInterface
{
// Should be a hidden myModel entity
dd($myModel);
}
}
<?php
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
class MyOtherModelController extends ActionController
{
public function initializeCreateAction(): void
{
$propertyMappingConfig = $this->arguments['myOtherModel']->getPropertyMappingConfiguration();
// Allow all properties to be set
$propertyMappingConfig->allowAllProperties();
// Set explicit type converter for 'date' property
$propertyMappingConfig->forProperty('date')
->setTypeConverterOption(
DateTimeConverter::class,
DateTimeConverter::CONFIGURATION_DATE_FORMAT,
'Y-m-d'
);
// Use custom HiddenMyModelConverter for 'myModel' property to handle hidden records
$propertyMappingConfig->forProperty('myModel')
->setTypeConverter(
GeneralUtility::makeInstance(HiddenMyModelConverter::class)
);
}
public function createAction(MyOtherModel $myOtherModel): ResponseInterface
{
// Should be a record containing a hidden myModel property
dd($myOtherModel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment