I want to get content elements via ajax only of a certain type, e.g. image, text or a custom element.
My first try was to create a typeNum to be able to fire ajax requests. From the then called action in my controller I wanted to get content elements of some page, render them and return them in a response. I'm totally lost how to do that. How to I fetch content elements when I have a page ID and how do I render them afterwards?
Another idea I had was to create a typeNum in which I filter the content elements to the types I need, but I don't know if that is even possible to do in TypoScript.
CodePudding user response:
In your action, you can execute a query in order to retrieve the content elements of a page and filter them by CType ('text', 'textpic', 'image', ...) :
/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
/** @var \Doctrine\DBAL\Driver\Statement $statement */
$statement = $queryBuilder->select('uid', 'title')
->from('tt_content')
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, Connection::PARAM_INT)),
$queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter($cType, Connection::PARAM_STR)),
)
->execute()
$listCE = $statement->fetchAll();
And then use a stand alone view to render the content elements :
/** @var StandaloneView $view */
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:my_ext/Resources/Private/Layouts')]);
$view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:my_ext/Resources/Private/Partials')]);
$view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:my_ext/Resources/Private/Templates')]);
$view->setTemplate('MyContentElementTemplate.html');
$view->assign('contentElements', $listCE);
// Render the content
$myContent = $view->render();