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

[TASK] Add new PreviewRenderer

parent d1b439bb
No related branches found
No related tags found
1 merge request!35Feature upgrade to typo3 11
<?php
/***************************************************************
* Copyright notice
* (c) sgalinski Internet Services (https://www.sgalinski.de)
* All rights reserved
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace SGalinski\SgJobs\Preview;
use TYPO3\CMS\Backend\Preview\PreviewRendererInterface;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
*
*/
class PreviewRenderer implements PreviewRendererInterface {
/**
* @var LanguageService $languageService
*/
protected LanguageService $languageService;
public function __construct(LanguageService $languageService) {
$this->languageService = $languageService;
}
/**
* Dedicated method for rendering preview header HTML for
* the page module only. Receives $item which is an instance of
* GridColumnItem which has a getter method to return the record.
*
* @param GridColumnItem $item
* @return string
*/
public function renderPageModulePreviewHeader(GridColumnItem $item): string {
$label = BackendUtility::getLabelFromItemListMerged(
$item->getRecord()['pid'],
'tt_content',
'list_type',
$item->getRecord()['list_type']
);
return '<h4><span class="label label-primary">' . $this->languageService->sL($label) . '</span>&nbsp;</h4>';
}
/**
* Dedicated method for rendering preview body HTML for
* the page module only.
*
* @param GridColumnItem $item
* @return string
*/
public function renderPageModulePreviewContent(GridColumnItem $item): string {
$row = $item->getRecord();
switch ($row['list_type']) {
case 'sgjobs_jobapplication':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('JobApplication');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'], 'T3DataStructure'
)['data']['sDEF']['lDEF'];
$redirectPage = $pluginConfiguration['settings.redirectPage']['vDEF'];
$templateData = [
'redirectPage' => is_string($redirectPage) ? $this->addFieldContentsToRecordIdList(
'pages', $redirectPage
) : '',
'privacyPolicyPage' => $pluginConfiguration['settings.privacyPolicyPage']['vDEF']
];
$view->assign('data', $templateData);
break;
case 'sgjobs_joblist':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('Joblist');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'], 'T3DataStructure'
)['data']['sDEF']['lDEF'];
$applyPage = $pluginConfiguration['settings.applyPage']['vDEF'];
$filterByExperienceLevel = $pluginConfiguration['settings.filterByExperienceLevel']['vDEF'];
$templateData = [
'applyPage' => is_string($applyPage) ? $this->addFieldContentsToRecordIdList(
'pages', $applyPage
) : '',
'jobLimit' => $pluginConfiguration['settings.jobLimit']['vDEF'],
'orderBy' => $pluginConfiguration['settings.orderBy']['vDEF'],
'filterByExperienceLevel' => is_string(
$filterByExperienceLevel
) ? $this->addFieldContentsToRecordIdList(
'tx_sgjobs_domain_model_experience_level', $filterByExperienceLevel
) : ''
];
$view->assign('data', $templateData);
// Also assign the standard plugin settings to the template
// as they are actually used in the corresponding controller action.
$view->assign('storagePages', is_string($row['pages']) ? $this->addFieldContentsToRecordIdList(
'pages', $row['pages']
) : '');
$view->assign('recursive', $row['recursive']);
break;
case 'sgjobs_jobteaser':
$drawItem = FALSE;
$view = $this->createViewWithTemplate('JobTeaser');
$view->assign('uid', $row['uid']);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'], 'T3DataStructure'
)['data']['sDEF']['lDEF'];
$offersPage = $pluginConfiguration['settings.offersPage']['vDEF'];
$applyPage = $pluginConfiguration['settings.applyPage']['vDEF'];
$locations = $pluginConfiguration['settings.locations']['vDEF'];
$templateData = [
'offersPage' => is_string($offersPage) ? $this->addFieldContentsToRecordIdList(
'pages', $offersPage
) : '',
'applyPage' => is_string($applyPage) ? $this->addFieldContentsToRecordIdList(
'pages', $applyPage
) : '',
'locations' => is_string($locations) ? $this->addFieldContentsToRecordIdList(
'tx_sgjobs_domain_model_company', $locations, 'name'
) : '',
];
$view->assign('data', $templateData);
break;
default:
// No need to do anything
}
return $view->render();
}
/**
* Render a footer for the record to display in page module below
* the body of the item's preview.
*
* @param GridColumnItem $item
* @return string
*/
public function renderPageModulePreviewFooter(GridColumnItem $item): string {
return '';
}
/**
* Dedicated method for wrapping a preview header and body HTML.
*
* @param string $previewHeader
* @param string $previewContent
* @param GridColumnItem $item
* @return string
*/
public function wrapPageModulePreview(string $previewHeader, string $previewContent, GridColumnItem $item): string {
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_jobs/Resources/Private/Templates/Backend']);
$view->setPartialRootPaths(['EXT:sg_jobs/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);
}
}
......@@ -13,6 +13,9 @@ services:
SGalinski\SgJobs\Service\BackendService:
public: true
SGalinski\SgJobs\Preview\PreviewRenderer:
public: true
SGalinski\SgJobs\Event\Listener\AccessPageListEventListener:
tags:
- name: event.listener
......
......@@ -59,3 +59,7 @@ $GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist']['sgjobs_jobte
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
'sgjobs_jobteaser', 'FILE:EXT:sg_jobs/Configuration/FlexForms/JobTeaser.xml'
);
$GLOBALS['TCA']['tt_content']['types']['list']['previewRenderer']['sgjobs_joblist'] = \SGalinski\SgJobs\Preview\PreviewRenderer::class;
$GLOBALS['TCA']['tt_content']['types']['list']['previewRenderer']['sgjobs_jobapplication'] = \SGalinski\SgJobs\Preview\PreviewRenderer::class;
$GLOBALS['TCA']['tt_content']['types']['list']['previewRenderer']['sgjobs_jobteaser'] = \SGalinski\SgJobs\Preview\PreviewRenderer::class;
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