Skip to content
Snippets Groups Projects
Commit 160b888e authored by Matthias Adrowski's avatar Matthias Adrowski
Browse files

[TASK] Move to central previewservice, fix php81 issues

parent 4d33051e
No related branches found
No related tags found
1 merge request!38Feature upgrade to typo3 11
......@@ -20,6 +20,7 @@ namespace SGalinski\SgNews\Hooks\PageLayoutView;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Preview\PreviewService;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
......@@ -34,6 +35,13 @@ use TYPO3\CMS\Fluid\View\StandaloneView;
* @package SGalinski\SgNews\Hook\PagelayoutView\PluginRenderer
*/
class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
protected PreviewService $previewService;
public function init() {
$this->previewService = GeneralUtility::makeInstance(PreviewService::class);
}
/**
* Preprocesses the preview rendering of an sg_news content element plugin.
*
......@@ -51,33 +59,13 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
&$itemContent,
array &$row
): void {
$this->init();
switch ($row['list_type']) {
case 'sgnews_overview':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('Overview');
$view->assign('uid', $row['uid']);
$this->adaptPluginHeaderContent($headerContent, $row);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$templateData = [
'groupBy' => $pluginConfiguration['settings.groupBy']['vDEF'],
'enableFilter' => $pluginConfiguration['settings.enableFilter']['vDEF'],
'newsLimit' => $pluginConfiguration['settings.newsLimit']['vDEF'],
'onlyNewsWithinThisPageSection' => $pluginConfiguration['settings.onlyNewsWithinThisPageSection']['vDEF'],
'starttime' => $pluginConfiguration['settings.starttime']['vDEF'],
'endtime' => $pluginConfiguration['settings.endtime']['vDEF'],
'sortBy' => $pluginConfiguration['settings.sortBy']['vDEF'],
'sortDirection' => $pluginConfiguration['settings.sortDirection']['vDEF']
];
$view->assign('data', $templateData);
$view = $this->previewService->getOverviewView($row);
$itemContent .= $view->render();
break;
......@@ -85,35 +73,8 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
case 'sgnews_latest':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('Latest');
$view->assign('uid', $row['uid']);
$this->adaptPluginHeaderContent($headerContent, $row);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$categories = $pluginConfiguration['settings.categories']['vDEF'];
$tags = $pluginConfiguration['settings.tags']['vDEF'];
$templateData = [
'limit' => $pluginConfiguration['settings.limit']['vDEF'],
'categories' => is_string($categories) ? $this->addFieldContentsToRecordIdList(
'pages',
$categories
) : '',
'tags' => is_string($tags) ? $this->addFieldContentsToRecordIdList(
'sys_category',
$tags
) : '',
'starttime' => $pluginConfiguration['settings.starttime']['vDEF'],
'endtime' => $pluginConfiguration['settings.endtime']['vDEF'],
'sortBy' => $pluginConfiguration['settings.sortBy']['vDEF']
];
$view->assign('data', $templateData);
$view = $this->previewService->getLatestView($row);
$itemContent .= $view->render();
break;
......@@ -121,33 +82,8 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
case 'sgnews_listbycategory':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('ListByCategory');
$view->assign('uid', $row['uid']);
$this->adaptPluginHeaderContent($headerContent, $row);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$categories = $pluginConfiguration['settings.categories']['vDEF'];
$tags = $pluginConfiguration['settings.tags']['vDEF'];
$templateData = [
'newsLimitPerPage' => $pluginConfiguration['settings.newsLimitPerPage']['vDEF'],
'categories' => is_string($categories) ? $this->addFieldContentsToRecordIdList(
'pages',
$categories
) : '',
'tags' => is_string($tags) ? $this->addFieldContentsToRecordIdList('sys_category', $tags) : '',
'starttime' => $pluginConfiguration['settings.starttime']['vDEF'],
'endtime' => $pluginConfiguration['settings.endtime']['vDEF'],
'sortBy' => $pluginConfiguration['settings.sortBy']['vDEF'],
'sortDirection' => $pluginConfiguration['settings.sortDirection']['vDEF']
];
$view->assign('data', $templateData);
$view = $this->previewService->getListByCategoryView($row);
$itemContent .= $view->render();
break;
......@@ -155,44 +91,9 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
case 'sgnews_newsbyauthor':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('NewsByAuthor');
$view->assign('uid', $row['uid']);
$this->adaptPluginHeaderContent($headerContent, $row);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$newsAuthors = $pluginConfiguration['settings.newsAuthors']['vDEF'];
$templateData = [
'showDetails' => $pluginConfiguration['settings.showDetails']['vDEF'],
'newsAuthors' => is_string($newsAuthors) ? $this->addFieldContentsToRecordIdList(
'tx_sgnews_domain_model_author',
$newsAuthors,
'name'
) : ''
];
// Not using addFieldContentsToRecordIdList to avoid repetitive imploding and exploding.
$excludedNewsIds = GeneralUtility::intExplode(
',',
$pluginConfiguration['settings.excludedNews']['vDEF'],
TRUE
);
$excludedNewsListWithTitles = [];
foreach ($excludedNewsIds as $excludedNewsId) {
$excludedNewsListWithTitles[] = BackendUtility::getRecord(
'pages',
$excludedNewsId,
'title'
)['title'] . ' [' . $excludedNewsId . ']';
}
$templateData['excludedNews'] = $excludedNewsListWithTitles;
$view->assign('data', $templateData);
$view = $this->previewService->getNewsByAuthorView($row);
$itemContent .= $view->render();
break;
......@@ -202,24 +103,6 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
}
}
/**
* Creates a new StandaloneView object with template and partial rot paths,
* attaches the template with the given name to it and returns it.
*
* @param string $templateName
* @return StandaloneView
*/
protected function createViewWithTemplate(string $templateName): StandaloneView {
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplateRootPaths(['EXT:sg_news/Resources/Private/Templates/Backend']);
$view->setPartialRootPaths(['EXT:sg_news/Resources/Private/Partials/Backend']);
if (!str_ends_with($templateName, '.html')) {
$templateName .= '.html';
}
$view->setTemplate($templateName);
return $view;
}
/**
* Adapts the given $headerContent.
* To be used in all plugin previews so the Header Contents appear similarly.
......@@ -229,9 +112,9 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
*/
protected function adaptPluginHeaderContent(&$headerContent, $row): void {
$headerContent = '<h4>' . $this->getPluginNameForHeaderContent(
(int) $row['pid'],
$row['list_type']
) . $headerContent . '</h4>';
(int) $row['pid'],
$row['list_type']
) . $headerContent . '</h4>';
}
/**
......@@ -255,32 +138,4 @@ class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
);
return '<span class="label label-primary">' . $pluginName . '</span>&nbsp;';
}
/**
* Takes a comma-separated list of record IDs, the corresponding table and optionally the field to look up.
* Returns another comma-space-separated list of the same records with the content of the field added.
* The returned list should look nice enough to be rendered in the backend preview directly.
*
* @param string $table
* @param string $recordIdList
* @param string $field
* @return string
*/
protected function addFieldContentsToRecordIdList(
string $table,
string $recordIdList,
string $field = 'title'
): string {
$recordIdsArray = GeneralUtility::intExplode(',', $recordIdList, TRUE);
$recordsWithTitlesArray = [];
foreach ($recordIdsArray as $recordId) {
$recordsWithTitlesArray[] = BackendUtility::getRecord(
$table,
$recordId,
$field
)[$field] . ' [' . $recordId . ']';
}
return implode(', ', $recordsWithTitlesArray);
}
}
......@@ -36,7 +36,10 @@ class PreviewRenderer implements PreviewRendererInterface {
*/
protected LanguageService $languageService;
public function __construct(LanguageService $languageService) {
protected PreviewService $previewService;
public function __construct(LanguageService $languageService, PreviewService $previewService) {
$this->previewService = $previewService;
$this->languageService = $languageService;
}
......@@ -69,132 +72,19 @@ class PreviewRenderer implements PreviewRendererInterface {
$row = $item->getRecord();
switch ($row['list_type']) {
case 'sgnews_overview':
$view = $this->createViewWithTemplate('Overview');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$templateData = [
'groupBy' => $pluginConfiguration['settings.groupBy']['vDEF'],
'enableFilter' => $pluginConfiguration['settings.enableFilter']['vDEF'],
'newsLimit' => $pluginConfiguration['settings.newsLimit']['vDEF'],
'onlyNewsWithinThisPageSection' => $pluginConfiguration['settings.onlyNewsWithinThisPageSection']['vDEF'],
'starttime' => $pluginConfiguration['settings.starttime']['vDEF'],
'endtime' => $pluginConfiguration['settings.endtime']['vDEF'],
'sortBy' => $pluginConfiguration['settings.sortBy']['vDEF'],
'sortDirection' => $pluginConfiguration['settings.sortDirection']['vDEF']
];
$view->assign('data', $templateData);
$view = $this->previewService->getOverviewView($row);
break;
case 'sgnews_latest':
$view = $this->createViewWithTemplate('Latest');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$categories = $pluginConfiguration['settings.categories']['vDEF'];
$tags = $pluginConfiguration['settings.tags']['vDEF'];
$templateData = [
'limit' => $pluginConfiguration['settings.limit']['vDEF'],
'categories' => is_string($categories) ? $this->addFieldContentsToRecordIdList(
'pages',
$categories
) : '',
'tags' => is_string($tags) ? $this->addFieldContentsToRecordIdList(
'sys_category',
$tags
) : '',
'starttime' => $pluginConfiguration['settings.starttime']['vDEF'],
'endtime' => $pluginConfiguration['settings.endtime']['vDEF'],
'sortBy' => $pluginConfiguration['settings.sortBy']['vDEF']
];
$view->assign('data', $templateData);
$view = $this->previewService->getLatestView($row);
break;
case 'sgnews_listbycategory':
$view = $this->createViewWithTemplate('ListByCategory');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$categories = $pluginConfiguration['settings.categories']['vDEF'];
$tags = $pluginConfiguration['settings.tags']['vDEF'];
$templateData = [
'newsLimitPerPage' => $pluginConfiguration['settings.newsLimitPerPage']['vDEF'],
'categories' => is_string($categories) ? $this->addFieldContentsToRecordIdList(
'pages',
$categories
) : '',
'tags' => is_string($tags) ? $this->addFieldContentsToRecordIdList('sys_category', $tags) : '',
'starttime' => $pluginConfiguration['settings.starttime']['vDEF'],
'endtime' => $pluginConfiguration['settings.endtime']['vDEF'],
'sortBy' => $pluginConfiguration['settings.sortBy']['vDEF'],
'sortDirection' => $pluginConfiguration['settings.sortDirection']['vDEF']
];
$view->assign('data', $templateData);
$view = $this->previewService->getListByCategoryView($row);
break;
case 'sgnews_newsbyauthor':
$view = $this->createViewWithTemplate('NewsByAuthor');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$newsAuthors = $pluginConfiguration['settings.newsAuthors']['vDEF'];
$templateData = [
'showDetails' => $pluginConfiguration['settings.showDetails']['vDEF'],
'newsAuthors' => is_string($newsAuthors) ? $this->addFieldContentsToRecordIdList(
'tx_sgnews_domain_model_author',
$newsAuthors,
'name'
) : ''
];
// Not using addFieldContentsToRecordIdList to avoid repetitive imploding and exploding.
$excludedNewsIds = GeneralUtility::intExplode(
',',
$pluginConfiguration['settings.excludedNews']['vDEF'],
TRUE
);
$excludedNewsListWithTitles = [];
foreach ($excludedNewsIds as $excludedNewsId) {
$excludedNewsListWithTitles[] = BackendUtility::getRecord(
'pages',
$excludedNewsId,
'title'
)['title'] . ' [' . $excludedNewsId . ']';
}
$templateData['excludedNews'] = $excludedNewsListWithTitles;
$view->assign('data', $templateData);
$view = $this->previewService->getNewsByAuthorView($row);
break;
default:
......@@ -226,49 +116,4 @@ class PreviewRenderer implements PreviewRendererInterface {
return $previewHeader . $previewContent;
}
/**
* Creates a new StandaloneView object with template and partial root paths,
* attaches the template with the given name to it and returns it.
*
* @param string $templateName
* @return StandaloneView
*/
protected function createViewWithTemplate(string $templateName): StandaloneView {
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplateRootPaths(['EXT:sg_news/Resources/Private/Templates/Backend']);
$view->setPartialRootPaths(['EXT:sg_news/Resources/Private/Partials/Backend']);
if (!str_ends_with($templateName, '.html')) {
$templateName .= '.html';
}
$view->setTemplate($templateName);
return $view;
}
/**
* Takes a comma-separated list of record IDs, the corresponding table and optionally the field to look up.
* Returns another comma-space-separated list of the same records with the content of the field added.
* The returned list should look nice enough to be rendered in the backend preview directly.
*
* @param string $table
* @param string $recordIdList
* @param string $field
* @return string
*/
protected function addFieldContentsToRecordIdList(
string $table,
string $recordIdList,
string $field = 'title'
): string {
$recordIdsArray = GeneralUtility::intExplode(',', $recordIdList, TRUE);
$recordsWithTitlesArray = [];
foreach ($recordIdsArray as $recordId) {
$recordsWithTitlesArray[] = BackendUtility::getRecord(
$table,
$recordId,
$field
)[$field] . ' [' . $recordId . ']';
}
return implode(', ', $recordsWithTitlesArray);
}
}
<?php
namespace SGalinski\SgNews\Preview;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* small helper class, aslong as we use PageLayout hook + Previewrenderer....
*/
class PreviewService {
public const RETURNTYPE_ARR = 'array';
/**
* returns a Standalone view for SgNews Overview Plugin
*
* @param array $row
* @return StandaloneView
*/
public function getOverviewView(array $row): StandaloneView {
$view = $this->createViewWithTemplate('Overview');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$templateData = [
'groupBy' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.groupBy'),
'enableFilter' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.enableFilter'),
'newsLimit' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.newsLimit'),
'onlyNewsWithinThisPageSection' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.onlyNewsWithinThisPageSection'),
'starttime' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.starttime'),
'endtime' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.endtime'),
'sortBy' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.sortBy'),
'sortDirection' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.sortDirection'),
];
$view->assign('data', $templateData);
return $view;
}
/**
* Returns LatestView for Previews of sg_news
*
* @param array $row
* @return StandaloneView
*/
public function getLatestView(array $row): StandaloneView {
$view = $this->createViewWithTemplate('Latest');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$categories = $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.categories');
$tags = $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.tags');
$templateData = [
'limit' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.limit'),
'categories' => is_string($categories) ? $this->addFieldContentsToRecordIdList(
'pages',
$categories
) : '',
'tags' => is_string($tags) ? $this->addFieldContentsToRecordIdList(
'sys_category',
$tags
) : '',
'starttime' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.starttime'),
'endtime' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.endtime'),
'sortBy' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.sortBy'),
];
$view->assign('data', $templateData);
return $view;
}
/**
* Returns ListByCategory View for Previews of sg_news
*
* @param array $row
* @return StandaloneView
*/
public function getListByCategoryView(array $row): StandaloneView {
$view = $this->createViewWithTemplate('ListByCategory');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$categories = $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.categories');
$tags = $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.tags');
$templateData = [
'newsLimitPerPage' =>$this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.newsLimitPerPage'),
'categories' => is_string($categories) ? $this->addFieldContentsToRecordIdList(
'pages',
$categories
) : '',
'tags' => is_string($tags) ? $this->addFieldContentsToRecordIdList(
'sys_category',
$tags
) : '',
'starttime' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.starttime'),
'endtime' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.endtime'),
'sortBy' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.sortBy'),
'sortDirection' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.sortDirection')
];
$view->assign('data', $templateData);
return $view;
}
/**
* Returns News by author View for Previews of sg_news
*
* @param array $row
* @return StandaloneView
*/
public function getNewsByAuthorView(array $row): StandaloneView {
$view = $this->createViewWithTemplate('NewsByAuthor');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'],
'T3DataStructure'
)['data']['main']['lDEF'];
$newsAuthors = $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.newsAuthors');
$templateData = [
'showDetails' => $this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.showDetails'),
'newsAuthors' => is_string($newsAuthors) ? $this->addFieldContentsToRecordIdList(
'tx_sgnews_domain_model_author',
$newsAuthors,
'name'
) : ''
];
// Not using addFieldContentsToRecordIdList to avoid repetitive imploding and exploding.
$excludedNewsIds = GeneralUtility::intExplode(
',',
$this->passVDefOnKeyToTemplate($pluginConfiguration, 'settings.excludedNews'),
TRUE
);
$excludedNewsListWithTitles = [];
foreach ($excludedNewsIds as $excludedNewsId) {
$excludedNewsListWithTitles[] = BackendUtility::getRecord(
'pages',
$excludedNewsId,
'title'
)['title'] . ' [' . $excludedNewsId . ']';
}
$templateData['excludedNews'] = $excludedNewsListWithTitles;
$view->assign('data', $templateData);
return $view;
}
/**
* @param array $conf
* @param string $key
* @param string $returnType
* @return array|mixed|string
*/
private function passVDefOnKeyToTemplate(array $conf, string $key, string $returnType = '') {
if(isset($conf[$key])) {
return $conf[$key]['vDEF'];
}
// check if we got a possible returntype:
if($returnType === self::RETURNTYPE_ARR) {
return [];
}
return '';
}
/**
* Creates a new StandaloneView object with template and partial root paths,
* attaches the template with the given name to it and returns it.
*
* @param string $templateName
* @return StandaloneView
*/
protected function createViewWithTemplate(string $templateName): StandaloneView {
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplateRootPaths(['EXT:sg_news/Resources/Private/Templates/Backend']);
$view->setPartialRootPaths(['EXT:sg_news/Resources/Private/Partials/Backend']);
if (!str_ends_with($templateName, '.html')) {
$templateName .= '.html';
}
$view->setTemplate($templateName);
return $view;
}
/**
* Takes a comma-separated list of record IDs, the corresponding table and optionally the field to look up.
* Returns another comma-space-separated list of the same records with the content of the field added.
* The returned list should look nice enough to be rendered in the backend preview directly.
*
* @param string $table
* @param string $recordIdList
* @param string $field
* @return string
*/
protected function addFieldContentsToRecordIdList(
string $table,
string $recordIdList,
string $field = 'title'
): string {
$recordIdsArray = GeneralUtility::intExplode(',', $recordIdList, TRUE);
$recordsWithTitlesArray = [];
foreach ($recordIdsArray as $recordId) {
$recordsWithTitlesArray[] = BackendUtility::getRecord(
$table,
$recordId,
$field
)[$field] . ' [' . $recordId . ']';
}
return implode(', ', $recordsWithTitlesArray);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment