Skip to content

Instantly share code, notes, and snippets.

@helhum
Last active January 24, 2021 14:08
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helhum/58a406fbb846b56a8b50 to your computer and use it in GitHub Desktop.
Save helhum/58a406fbb846b56a8b50 to your computer and use it in GitHub Desktop.
Extbase TypeConverter to fetch hidden records from persistence (Using this will *always* fetch hidden models of the specified type)
<?php
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter('MyVendor\\MyExtension\\Property\\TypeConverters\\MyPersistentObjectConverter');
<?php
namespace MyVendor\MyExtension\Property\TypeConverters
use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter
class MyPersistentObjectConverter extends PersistentObjectConverter {
/**
* @var string
*/
protected $targetType = 'MyVendor\\MyExtension\\Domain\\Model\\MyModel';
/**
* @var int
*/
protected $priority = 2;
/**
* Fetch an object from persistence layer.
*
* @param mixed $identity
* @param string $targetType
* @throws \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
* @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
* @return object
*/
protected function fetchObjectFromPersistence($identity, $targetType) {
if (ctype_digit((string)$identity)) {
$query = $this->persistenceManager->createQueryForType($targetType);
$query->getQuerySettings()->setIgnoreEnableFields(TRUE);
$constraints = $query->equals('uid', $identity);
$object = $query->matching($constraints)->execute()->getFirst();
} else {
throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
}
if ($object === NULL) {
throw new \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException('Object with identity "' . print_r($identity, TRUE) . '" not found.', 1297933823);
}
return $object;
}
}
@widlewski
Copy link

Thank you very much! Works great ...

@potofcoffee
Copy link

Very useful hint, thank you!

Just nitpickin': Your class name MyPersistenObjectConverter is missing a "t" at the end of "Persistent" ;-).

@daCyberpunk
Copy link

Thanks a lot!

@zoranilic
Copy link

Excellent!!! I think this is the only way to include hidden objects when, for example, "Save and View" is triggered for a hidden record.

@fnagel
Copy link

fnagel commented Aug 19, 2019

Thanks a lot! Very helpful.

Is there any way to add this only for specific routes (e.g. controller & actions)?

@helhum
Copy link
Author

helhum commented Aug 20, 2019

Is there any way to add this only for specific routes (e.g. controller & actions)?

Well, since the TypeConverter is PHP code, you can add any applicable if/switch in there to only apply it for certain conditions :)

@Teisi
Copy link

Teisi commented Aug 22, 2020

Thanks a lot!

@moe2k
Copy link

moe2k commented Oct 19, 2020

Thanks a lot! Very helpful.

Is there any way to add this only for specific routes (e.g. controller & actions)?

Actually yes! :-) An approach I found quite useful if you have scenarios where the type converter shall depend on the action or argument and not on source and target type. That can be done by not registering the typeConverter but instead to configure property mapper in your specific ation to use that specific type converter for a specific argument. You can do so in the initalize(Foo)Action methods.

Example:
$this->arguments->getArgument('myargument')->getPropertyMappingConfiguration()->setTypeConverter($this->objectManager->get(MyTypeConverter::class));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment