Some Snippets
Some common code snippets
1. Debugging
\TYPO3\CMS\Core\Utility\DebugUtility::debug()
// or you can use
// debug(); if you are in a development mode
2. TYPO3 Annotations
/**
* Some of the annotations used in TYPO3
*
* @TYPO3\CMS\Extbase\Annotation\ORM\Lazy
* @TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
* @TYPO3\CMS\Extbase\Annotation\Validate("NotEmpty")
* @TYPO3\CMS\Extbase\Annotation\Validate("EmailAddress")
* @TYPO3\CMS\Extbase\Annotation\Inject
* @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("domainObject")
*/
3. Fill ObjectStorage from QueryResult
/**
* Fill objectStorage from QueryResult
*
* @param \TYPO3\CMS\Extbase\Persistence\QueryResultInterface $queryResult
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
protected function fillOjectStorageFromQueryResult(
\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $queryResult=NULL
)
{
/* @var $objectStorage \TYPO3\CMS\Extbase\Persistence\ObjectStorage */
$objectStorage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class
);
if (NULL!==$queryResult) {
foreach($queryResult AS $object) {
$objectStorage->attach($object);
}
}
return $objectStorage;
}
4. Default ordering in repository
protected $defaultOrderings = array(
'number' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
);
5. Do not respect storage pid
public function initializeObject()
{
$querySettings = $this->objectManager->get(
\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class
);
$querySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($querySettings);
}
6. Get Path
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('YOUR_EXTENSION_KEY'); //Extension' apth
\TYPO3\CMS\Core\Core\Environment::getPublicPath(); // Public path
7. Set TypeConverter for date
function initializeCreateAction()
{
$dateformat = 'Y-m-d';
$this->arguments->getArgument('newJob')
->getPropertyMappingConfiguration()
->forProperty('expiryDate')
->setTypeConverterOption(
\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class,
\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
$dateformat
);
}
8. Get Extension Configuration
/**
* function to get extension configuration
*
* @param string $configVar
* @return mixed
*/
public function getExtensionConfiguration(string $configVar)
{
$extensionConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class
);
return $extensionConfiguration->get('YOU_EXTENSION_NAME', $configVar);
}
9. Get Logger
protected static function getLogger()
{
return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Log\LogManager::class
)->getLogger(__CLASS__);
}