Skip to content

Commit

Permalink
[TASK:T12] Migrate PageModuleSummary to PageContentPreviewRendering
Browse files Browse the repository at this point in the history
  • Loading branch information
dkd-kaehm committed Feb 17, 2023
1 parent 633ff35 commit 1aed01f
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,72 @@
* The TYPO3 project - inspiring people to share!
*/

namespace ApacheSolrForTypo3\Solr\Controller\Backend;
namespace ApacheSolrForTypo3\Solr\Backend;

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\Event\PageContentPreviewRenderingEvent;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
use TYPO3\CMS\Fluid\View\StandaloneView;

use function str_starts_with;

/**
* Summary to display flexform settings in the page layout backend module.
*
* @author Ingo Renner <ingo@typo3.org>
* @author Timo Hund <timo.hund@dkd.de>
* Summary to display flexform settings of EXT:solr plugin in BE page module.
*/
class PageModuleSummary
class SettingsPreviewOnPlugins
{
/**
* PageLayoutView
*
* @var PageLayoutView
*/
protected $pageLayoutView;
protected array $pluginsTtContentRecord;
private array $flexformData;

/**
* @var array
*/
protected $pluginContentElement = [];
protected array $settings = [];

/**
* @var array
*/
protected $flexformData = [];
public function __construct(
protected FlexFormService $flexFormService
) {
}

public function __invoke(PageContentPreviewRenderingEvent $event): void
{
$this->pluginsTtContentRecord = $event->getRecord();
if (
$event->getTable() !== 'tt_content'
|| !str_starts_with($this->pluginsTtContentRecord['list_type'], 'solr_pi_')
) {
return;
}
$this->flexformData = $this->flexFormService->convertFlexFormContentToArray($this->pluginsTtContentRecord['pi_flexform'] ?? '');
$event->setPreviewContent($this->getPreviewContent());
}

/**
* @var array
* @return string
*/
protected $settings = [];
protected function getPreviewContent(): string
{
$this->collectSummary();

/* @var StandaloneView $standaloneView */
$standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneView->setTemplatePathAndFilename(
GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Backend/PageModule/Summary.html')
);

$standaloneView->assignMultiple([
'pluginLabel' => $this->getPluginLabel(),
'hidden' => $this->pluginsTtContentRecord['hidden'] ?? 0,
'settings' => $this->settings,
]);
return $standaloneView->render();
}

/**
* Returns information about a plugin's flexform configuration
*
* @param array $parameters Parameters to the hook
* @return string Plugin configuration information
*/
public function getSummary(array $parameters)
public function collectSummary(): void
{
$this->initialize($parameters['row'], $parameters['pObj']);

$this->addTargetPage();
$this->addSettingFromFlexForm('Filter', 'search.query.filter');
$this->addSettingFromFlexForm('Sorting', 'search.query.sortBy');
Expand All @@ -71,31 +87,17 @@ public function getSummary(array $parameters)
$this->addSettingFromFlexForm('Boost Query', 'search.query.boostQuery');
$this->addSettingFromFlexForm('Tie Breaker', 'search.query.tieParameter');
$this->addSettingFromFlexForm('Template', 'view.templateFiles.results');
return $this->render();
}

/**
* @param array $contentElement
* @param PageLayoutView $pObj
*/
protected function initialize(array $contentElement, PageLayoutView $pObj)
{
$this->pageLayoutView = $pObj;

/** @var $service \TYPO3\CMS\Core\Service\FlexFormService::class */
$service = GeneralUtility::makeInstance(FlexFormService::class);
$this->flexformData = $service->convertFlexFormContentToArray($contentElement['pi_flexform']);
$this->pluginContentElement = $contentElement;
}

/**
* Adds the target page to the settings.
*/
protected function addTargetPage()
protected function addTargetPage(): void
{
$targetPageId = $this->getFieldFromFlexform('search.targetPage');
if (!empty($targetPageId)) {
$page = BackendUtility::getRecord('pages', $targetPageId, 'title');
$page = BackendUtility::getRecord('pages', $targetPageId, 'title')
?? ['title' => 'ERROR: page is gone'];
$this->settings['Target Page'] = '[' . (int)$targetPageId . '] ' . $page['title'];
}
}
Expand All @@ -104,22 +106,22 @@ protected function addTargetPage()
* @param string $settingName
* @param string $flexFormField
*/
protected function addSettingFromFlexForm($settingName, $flexFormField)
protected function addSettingFromFlexForm(string $settingName, string $flexFormField): void
{
$value = $this->getFieldFromFlexform($flexFormField);

if (is_array($value)) {
$value = $this->addSettingFromFlexFormArray($settingName, $value);
$this->addSettingFromFlexFormArray($settingName, $value);
return;
}
$this->addSettingIfNotEmpty($settingName, (string)$value);
}

/**
* @param string $settingName
* @param array $values
* @return bool
*/
protected function addSettingFromFlexFormArray($settingName, $values)
protected function addSettingFromFlexFormArray(string $settingName, array $values): void
{
foreach ($values as $item) {
if (!isset($item['field'])) {
Expand All @@ -128,8 +130,8 @@ protected function addSettingFromFlexFormArray($settingName, $values)
$field = $item['field'];

$label = $settingName . ' ';
$label .= isset($field['field']) ? $field['field'] : '';
$fieldValue = isset($field['value']) ? $field['value'] : '';
$label .= $field['field'] ?? '';
$fieldValue = $field['value'] ?? '';
$this->addSettingIfNotEmpty($label, (string)$fieldValue);
}
}
Expand All @@ -138,7 +140,7 @@ protected function addSettingFromFlexFormArray($settingName, $values)
* @param string $settingName
* @param string $value
*/
protected function addSettingIfNotEmpty($settingName, $value)
protected function addSettingIfNotEmpty(string $settingName, string $value): void
{
if (!empty($value)) {
$this->settings[$settingName] = $value;
Expand All @@ -152,45 +154,26 @@ protected function addSettingIfNotEmpty($settingName, $value)
* @param string $path name of the field
* @return mixed|null if nothing found, value if found
*/
protected function getFieldFromFlexform(string $path)
protected function getFieldFromFlexform(string $path): mixed
{
return ObjectAccess::getPropertyPath($this->flexformData, $path);
}

/**
* @return string
*/
protected function render()
{
/** @var $standaloneView StandaloneView */
$standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneView->setTemplatePathAndFilename(
GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Backend/PageModule/Summary.html')
);

$standaloneView->assignMultiple([
'pluginLabel' => $this->getPluginLabel(),
'hidden' => $this->pluginContentElement['hidden'],
'settings' => $this->settings,
]);
return $standaloneView->render();
}

/**
* Returns the plugin label
*
* @return string
*/
protected function getPluginLabel()
protected function getPluginLabel(): string
{
$label = BackendUtility::getLabelFromItemListMerged($this->pluginContentElement['pid'], 'tt_content', 'list_type', $this->pluginContentElement['list_type']);
$label = BackendUtility::getLabelFromItemListMerged($this->pluginsTtContentRecord['pid'], 'tt_content', 'list_type', $this->pluginsTtContentRecord['list_type']);
if (!empty($label)) {
$label = $this->getLanguageService()->sL($label);
} else {
$label = sprintf($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue'), $this->pluginContentElement['list_type']);
$label = sprintf($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue'), $this->pluginsTtContentRecord['list_type']);
}

return $this->pageLayoutView->linkEditContent(htmlspecialchars($label), $this->pluginContentElement);
return $label;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected function getRootPageIdByTableAndUid(string $table, int $uid): int
protected function getRecordPageId(string $table, int $uid): int
{
$record = BackendUtility::getRecord($table, $uid, 'pid');
return $record['pid'] ? (int)$record['pid'] : 0;
return $record['pid'] ? (int)$record['pid'] : 0;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ services:
autowire: true
tags: ['backend.controller']

# BE modules, plugins
ApacheSolrForTypo3\Solr\Backend\SettingsPreviewOnPlugins:
arguments:
$flexFormService: '@TYPO3\CMS\Core\Service\FlexFormService'
tags:
- name: event.listener
identifier: 'solr.plugin.be.settings.preview'
event: TYPO3\CMS\Backend\View\Event\PageContentPreviewRenderingEvent
# END: BE modules

viewhelper_backend:
namespace: ApacheSolrForTypo3\Solr\ViewHelpers\Backend\
resource: '../Classes/ViewHelpers/Backend/*'
Expand Down
2 changes: 2 additions & 0 deletions Configuration/TCA/Overrides/tt_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
'pi_results',
'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:tt_content.list_type_pi_results'
);

$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_excludelist'][$pluginSignature]
= 'layout,select_key,pages,recursive';
$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist'][$pluginSignature]
= 'pi_flexform';

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
$pluginSignature,
'FILE:EXT:solr/Configuration/FlexForms/Results.xml'
Expand Down
12 changes: 10 additions & 2 deletions Resources/Private/Templates/Backend/PageModule/Summary.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<table class="typo3-dblist" style="width:100%; {f:if(condition: hidden, then: 'hidden')}">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xmlns:f="http://typo3.org/ns/TYPO3/Fluid/ViewHelpers"
xmlns:s="http://typo3.org/ns/ApacheSolrForTypo3/Solr/ViewHelpers"
data-namespace-typo3-fluid="true"
>

<table class="table" style="{f:if(condition: hidden, then: 'hidden')}">
<thead>
<tr>
<th colspan="2" style="padding-bottom: 0.5em;"><f:format.raw>{pluginLabel}</f:format.raw></th>
Expand All @@ -11,4 +17,6 @@
<td>{settingValue}</td>
</tr>
</f:for>
</table>
</table>

</html>
Loading

0 comments on commit 1aed01f

Please sign in to comment.