Last active
January 24, 2021 14:08
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter('MyVendor\\MyExtension\\Property\\TypeConverters\\MyPersistentObjectConverter'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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));