TYPO3 Snippets

Some snippets that I use in TYPO3

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 = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\ObjectStorage');

    if (NULL!==$queryResult) {
        foreach($queryResult AS $object) {
            $objectStorage->attach($object);
        }
    }

    return $objectStorage;
}

Trim Explode

$professorIdsArrray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $professorIds, true);

Default orderings in Repository

protected $defaultOrderings = array(
    'number' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
);

Do not respect storage pid

public function initializeObject() 
{
    $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
    $querySettings->setRespectStoragePage(FALSE);
    $this->setDefaultQuerySettings($querySettings);
}

Get Extension Path

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('YOUR_EXTENSION_KEY');

Debug SQL Query

TYPO3 7

// Debug code
$parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser');
$queryParts = $parser->parseQuery($query);
\TYPO3\CMS\Core\Utility\DebugUtility::debug($queryParts, 'query');
// Debug code end

TYPO3 8

$queryParser = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getSQL());
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getParameters());

Convert date from submitted form

Note the argument newJob replace with the controller's argument name.

function initializeCreateAction()
{
    $dateformat = 'Y-m-d';
    $this->arguments->getArgument('newJob')->getPropertyMappingConfiguration()->forProperty('expiryDate')->setTypeConverterOption(
        'TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', 
        \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, 
        $dateformat
    );
}