-
-
Save helhum/58a406fbb846b56a8b50 to your computer and use it in GitHub Desktop.
<?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; | |
} | |
} |
Thank you very much! Works great ...
Very useful hint, thank you!
Just nitpickin': Your class name MyPersistenObjectConverter
is missing a "t" at the end of "Persistent" ;-).
Thanks a lot!
Excellent!!! I think this is the only way to include hidden objects when, for example, "Save and View" is triggered for a hidden record.
Thanks a lot! Very helpful.
Is there any way to add this only for specific routes (e.g. controller & actions)?
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 :)
Thanks a lot!
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));
Thank you, very useful!