Add preview renderers for custom content elements

This commit is contained in:
Björn Fromme
2021-09-27 18:26:56 +02:00
parent beb9fbae04
commit 0262738c91
26 changed files with 596 additions and 141 deletions
@@ -0,0 +1,146 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use EP\EpTheme\Exception\ConfigurationMissingException;
use EP\EpTheme\Exception\TemplateFileMissingException;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Class AbstractPreviewRenderer
*/
abstract class AbstractPreviewRenderer implements PageLayoutViewDrawItemHookInterface
{
/**
* @var array tt_content.*
*/
protected $data = [];
/**
* Define a CType
*
* @var string
*/
protected $cType = '';
/**
* @var string
*/
protected $templatePath = 'EXT:ep_theme/Resources/Private/Templates/PreviewRenderer/';
public function __construct()
{
if (empty($this->cType)) {
throw new ConfigurationMissingException('Property cType must not be empty', 1586703436);
}
}
public function preProcess(
PageLayoutView &$parentObject,
&$drawItem,
&$headerContent,
&$itemContent,
array &$row
) {
$this->data = &$row;
if ($this->isCtypeMatching() && $this->checkTemplateFile()) {
$drawItem = false;
$headerContent = $this->getHeaderContent();
$itemContent .= $this->getBodytext();
}
}
protected function getHeaderContent(): string
{
return '<div id="element-tt_content-' . (int)$this->data['uid']
. '" class="t3-ctype-identifier " data-ctype="' . $this->cType . '"></div>';
}
protected function getBodytext(): string
{
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$standaloneView = $objectManager->get(StandaloneView::class);
$standaloneView->setTemplatePathAndFilename($this->getTemplateFile());
$standaloneView->assignMultiple($this->getAssignmentsForTemplate() + [
'data' => $this->data,
'flexForm' => $this->getFlexForm(),
'firstImage' => count($this->getImages()) > 0 ? $this->getImages()[0] : null,
'images' => $this->getImages()
]);
return $standaloneView->render();
}
protected function getAssignmentsForTemplate(): array
{
return [];
}
protected function getFlexForm(): array
{
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$flexFormService = $objectManager->get(FlexFormService::class);
return $flexFormService->convertFlexFormContentToArray($this->data['pi_flexform']);
}
protected function getImages(int $uid = null, string $tablenames = 'tt_content', string $fieldname = 'image'): array
{
$uid = $uid ?? $this->data['uid'];
$references = [];
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$fileRepository = $objectManager->get(FileRepository::class);
/** @var ConnectionPool $connectionPool */
$connectionPool = $objectManager->get(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('sys_file_reference');
$identifiers = (array)$queryBuilder
->select('uid')
->from('sys_file_reference')
->where('uid_foreign=' . $uid . ' and tablenames="' . $tablenames . '" and fieldname="' . $fieldname . '"')
->execute()
->fetch(\PDO::FETCH_COLUMN);
foreach ($identifiers as $identifier) {
if ($identifier > 0) {
$reference = $fileRepository->findFileReferenceByUid($identifier);
if ($reference !== null) {
$references[] = $reference;
}
}
}
return $references;
}
/**
* @return bool
*/
protected function isCtypeMatching(): bool
{
return $this->data['CType'] === $this->cType;
}
protected function checkTemplateFile(): bool
{
if (is_file($this->getTemplateFile()) === false) {
throw new TemplateFileMissingException(
'Expected template file for preview rendering for CType ' . $this->cType . ' is missing',
1586703260
);
}
return true;
}
protected function getTemplateFile(): string
{
return GeneralUtility::getFileAbsFileName(
$this->templatePath . GeneralUtility::underscoredToUpperCamelCase($this->data['CType']) . '.html'
);
}
}
@@ -0,0 +1,20 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class DisrupterPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'disrupter';
protected function getAssignmentsForTemplate(): array
{
$uid = $this->data['tx_eptheme_disrupter'];
$content = BackendUtility::getRecord('tx_eptheme_domain_model_disrupter', $uid);
return [
'content' => $content,
];
}
}
@@ -0,0 +1,8 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
class DisturberPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'disturber';
}
@@ -0,0 +1,26 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class GmapPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'gmap';
protected function getAssignmentsForTemplate(): array
{
$uid = $this->data['tx_eptheme_context_region'];
if ((int)$uid > 0) {
$content = BackendUtility::getRecord('tx_epproducts_domain_model_region', $uid);
} else {
$uid = $this->data['tx_eptheme_context_hotel'];
$content = BackendUtility::getRecord('tx_epproducts_domain_model_hotel', $uid);
}
return [
'content' => $content,
];
}
}
@@ -0,0 +1,31 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
class LineupPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'lineup';
protected function getAssignmentsForTemplate(): array
{
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var ConnectionPool $connectionPool */
$connectionPool = $objectManager->get(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_eptheme_domain_model_lineup');
$rows = $queryBuilder->select('*')
->from('tx_eptheme_domain_model_lineup')
->where('tt_content='.$this->data['uid'])
->orderBy('sorting', 'ASC')
->execute()
->fetchAllAssociative();
return [
'rows' => $rows,
];
}
}
@@ -0,0 +1,20 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class TeaserCityPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'teaser_city';
protected function getAssignmentsForTemplate(): array
{
$uid = $this->data['tx_eptheme_context_city'];
$images = $this->getImages($uid, 'tx_epproducts_domain_model_city', 'teaser_images');
return [
'images' => $images,
'city' => BackendUtility::getRecord('tx_epproducts_domain_model_city', $uid),
];
}
}
@@ -0,0 +1,20 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class TeaserConceptPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'teaser_concept';
protected function getAssignmentsForTemplate(): array
{
$uid = $this->data['tx_eptheme_context_concept'];
$images = $this->getImages($uid, 'tx_epproducts_domain_model_concept', 'teaser_images');
return [
'images' => $images,
'concept' => BackendUtility::getRecord('tx_epproducts_domain_model_concept', $uid),
];
}
}
@@ -0,0 +1,20 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class TeaserCountryPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'teaser_country';
protected function getAssignmentsForTemplate(): array
{
$countryUid = $this->data['tx_eptheme_context_country'];
$images = $this->getImages($countryUid, 'tx_epproducts_domain_model_country', 'teaser_images');
return [
'images' => $images,
'country' => BackendUtility::getRecord('tx_epproducts_domain_model_country', $countryUid),
];
}
}
@@ -0,0 +1,20 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class TeaserHotelPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'teaser_hotel';
protected function getAssignmentsForTemplate(): array
{
$uid = $this->data['tx_eptheme_context_hotel'];
$images = $this->getImages($uid, 'tx_epproducts_domain_model_hotel', 'teaser_images');
return [
'images' => $images,
'hotel' => BackendUtility::getRecord('tx_epproducts_domain_model_hotel', $uid),
];
}
}
@@ -0,0 +1,24 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class TeaserProductPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'teaser_product';
protected function getAssignmentsForTemplate(): array
{
$productUid = $this->data['tx_eptheme_context_product'];
$hotelUid = $this->data['tx_eptheme_context_hotel'];
$images = $this->getImages($productUid, 'tx_epproducts_domain_model_product', 'teaser_images');
$product = BackendUtility::getRecord('tx_epproducts_domain_model_product', $productUid);
$hotel = $hotelUid ? BackendUtility::getRecord('tx_epproducts_domain_model_hotel', $hotelUid) : null;
return [
'images' => $images,
'product' => $product,
'hotel' => $hotel,
];
}
}
@@ -0,0 +1,20 @@
<?php
namespace EP\EpTheme\Hooks\PageLayoutView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
class TeaserRegionPreviewRenderer extends AbstractPreviewRenderer
{
protected $cType = 'teaser_region';
protected function getAssignmentsForTemplate(): array
{
$uid = $this->data['tx_eptheme_context_region'];
$images = $this->getImages($uid, 'tx_epproducts_domain_model_region', 'teaser_images');
return [
'images' => $images,
'region' => BackendUtility::getRecord('tx_epproducts_domain_model_region', $uid),
];
}
}