Skip to content

Commit

Permalink
[BUGFIX] Update NewContentElementWizard to event
Browse files Browse the repository at this point in the history
The NewContentElementWizard changed to use PSR-14 Event
also the item arrays do not have a 'params' section anymore.
Also processing changed a bit, so we need to crack the nut in
another way.

Resolves: #557
Related: 12.0.1
  • Loading branch information
opi99 committed Feb 19, 2024
1 parent 94972f7 commit 0508ccd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?php

namespace Tvp\TemplaVoilaPlus\Hooks;
namespace Tvp\TemplaVoilaPlus\Configuration;

use Tvp\TemplaVoilaPlus\Controller\Backend\Ajax\ExtendedNewContentElementController;
use Tvp\TemplaVoilaPlus\Service\ConfigurationService;
use Tvp\TemplaVoilaPlus\Service\ItemsProcFunc;
use Tvp\TemplaVoilaPlus\Utility\TemplaVoilaUtility;
use TYPO3\CMS\Backend\Controller\Event\ModifyNewContentElementWizardItemsEvent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\Wizard\NewContentElementWizardHookInterface;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class WizardItems implements NewContentElementWizardHookInterface
class ContentElementWizardItems
{
/**
* Modifies WizardItems of the NewContentElementWizard array
*
* @param array $wizardItems Array of Wizard Items
* @param \TYPO3\CMS\Backend\Controller\ContentElement\NewContentElementController $parentObject Parent object New Content element wizard
*/
public function manipulateWizardItems(&$wizardItems, &$parentObject)
public function processEvent(ModifyNewContentElementWizardItemsEvent $event): void
{
$wizardItems = $event->getWizardItems();

$fceWizardItems = [
'fce' => [
'header' => $this->getLanguageService()->sL('LLL:EXT:templavoilaplus/Resources/Private/Language/Backend/PageLayout.xlf:newContentElementWizard.fce'),
Expand All @@ -47,12 +43,7 @@ public function manipulateWizardItems(&$wizardItems, &$parentObject)
$wizardLabel = 'fce_' . $combinedMappingIdentifier;

// try to get pid, either from out Controller if available or from URL
$pageId = 0;
if ($parentObject instanceof ExtendedNewContentElementController) {
$pageId = $parentObject->getPageId();
} elseif ((int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id') > 0) {
$pageId = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
}
$pageId = ($event->getPageInfo()['uid'] ?? 0);

// if pid is available check PageTSconfig, if pid unavailable or pageTSconfig forbids: skip this fce
if (
Expand All @@ -73,13 +64,15 @@ public function manipulateWizardItems(&$wizardItems, &$parentObject)
'description' => /** @TODO $mappingConfiguration->getDescription() ?? */
TemplaVoilaUtility::getLanguageService()->getLL('template_nodescriptionavailable'),
'title' => $mappingConfiguration->getName(),
'params' => $this->getDataHandlerDefaultValues($combinedMappingIdentifier),
'tt_content_defValues' => $this->getDataHandlerDefaultValues($combinedMappingIdentifier),
];
}
}
}
$wizardItems = $fceWizardItems + $wizardItems;
$wizardItems = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($wizardItems);

$event->setWizardItems($wizardItems);
}

/**
Expand Down Expand Up @@ -111,10 +104,12 @@ protected function isWizardItemAvailable(int $currentPageId, string $combinedMap
* @param string $combinedMappingIdentifier
* @return string additional URL arguments with configured default values for DataHandler/TCEForms
*/
public function getDataHandlerDefaultValues(string $combinedMappingIdentifier)
public function getDataHandlerDefaultValues(string $combinedMappingIdentifier): array
{
$dsValues = '&defVals[tt_content][CType]=templavoilaplus_pi1'
. '&defVals[tt_content][tx_templavoilaplus_map]=' . $combinedMappingIdentifier;
$ttcontentDefVals = [
'CType' => 'templavoilaplus_pi1',
'tx_templavoilaplus_map' => $combinedMappingIdentifier,
];

/** @TODO We should push the DataStructure TCEForms defaults into the values? Or is this processed automagically already? */
// $dsStructure = $toObj->getLocalDataprotArray();
Expand All @@ -125,7 +120,7 @@ public function getDataHandlerDefaultValues(string $combinedMappingIdentifier)
// }
// }

return $dsValues;
return $ttcontentDefVals;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion Classes/Controller/Backend/Ajax/ContentElementWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private function modifyContentElementsConfig(array $contentElementsConfig): arra

// Do dependency ordering inside the tabs or unset tab if empty */
foreach ($newContentElementsConfig as $tabKey => $tabConfig) {
if (count($newContentElementsConfig[$tabKey]['contentElements']) === 0) {
if (count(($newContentElementsConfig[$tabKey]['contentElements'] ?? [])) === 0) {
unset($newContentElementsConfig[$tabKey]);
} else {
$newContentElementsConfig[$tabKey]['contentElements']
Expand All @@ -182,12 +182,18 @@ private function convertParamsValue(array $contentElementsConfig): array
foreach ($tabConfig['contentElements'] as $_key => $contentElement) {
$contentElement['element-row'] = [];

/** @TODO Backward compatibility, remove wit TYPO3 v13+ only */
/** Feature-97201-PSR-14EventForModifyingNewContentElementWizardItems.rst */
if (isset($contentElement['params'])) {
parse_str($contentElement['params'], $contentElementParams);
if (isset($contentElementParams['defVals']['tt_content'])) {
$contentElement['element-row'] = $contentElementParams['defVals']['tt_content'];
}
}
/** ENDE */
if (isset($contentElement['tt_content_defValues']) && is_array($contentElement['tt_content_defValues'])) {
$contentElement['element-row'] = $contentElement['tt_content_defValues'];
}
$contentElementsConfig[$tabKey]['contentElements'][$_key] = $contentElement;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Attribute\Controller;
use TYPO3\CMS\Backend\Controller\Event\ModifyNewContentElementWizardItemsEvent;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\BackendViewFactory;
use TYPO3\CMS\Backend\Wizard\NewContentElementWizardHookInterface;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;

#[Controller]
Expand All @@ -32,28 +35,37 @@ public function __construct(
*/
public function getWizardsByRequest(ServerRequestInterface $request): array
{
$this->handleRequest($request);
$wizardItems = $this->getWizards();
/** From TYPO3 core handleRequest method */
$parsedBody = $request->getParsedBody();
$queryParams = $request->getQueryParams();

// Hook for manipulating wizardItems, wrapper, onClickEvent etc.
// Yes, thats done outside the function wich gathers the wizards!
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'] ?? [] as $className) {
/** @var NewContentElementWizardHookInterface */
$hookObject = GeneralUtility::makeInstance($className);
if (!$hookObject instanceof NewContentElementWizardHookInterface) {
throw new \UnexpectedValueException(
$className . ' must implement interface ' . NewContentElementWizardHookInterface::class,
1227834741
);
}
$hookObject->manipulateWizardItems($wizardItems, $this);
$this->id = (int)($parsedBody['id'] ?? $queryParams['id'] ?? 0);
$this->sys_language = (int)($parsedBody['sys_language_uid'] ?? $queryParams['sys_language_uid'] ?? 0);
$this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
$colPos = $parsedBody['colPos'] ?? $queryParams['colPos'] ?? null;
$this->colPos = $colPos === null ? null : (int)$colPos;
$this->uid_pid = (int)($parsedBody['uid_pid'] ?? $queryParams['uid_pid'] ?? 0);
$this->pageInfo = BackendUtility::readPageAccess($this->id, $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)) ?: [];

/** From TYPO3 core wizardAction method */
if (!$this->id || $this->pageInfo === []) {
// No pageId or no access.
return [];
}
// Whether position selection must be performed (no colPos was yet defined)
$positionSelection = $this->colPos === null;

return $wizardItems;
}
// Get processed and modified wizard items
$wizardItems = $this->eventDispatcher->dispatch(
new ModifyNewContentElementWizardItemsEvent(
$this->getWizards(),
$this->pageInfo,
$this->colPos,
$this->sys_language,
$this->uid_pid,
)
)->getWizardItems();

public function getPageId()
{
return $this->id;
return $wizardItems;
}
}
7 changes: 7 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ services:
method: 'processEvent'
event: TYPO3\CMS\Core\Core\Event\BootCompletedEvent

Tvp\TemplaVoilaPlus\Configuration\ContentElementWizardItems:
tags:
- name: event.listener
identifier: 'templavoilaplus/newContentElementWizardItems/modify'
method: 'processEvent'
event: TYPO3\CMS\Backend\Controller\Event\ModifyNewContentElementWizardItemsEvent

Tvp\TemplaVoilaPlus\Controller\Backend\Ajax\ContentElementWizard:
tags: [ 'backend.controller' ]

Expand Down
3 changes: 0 additions & 3 deletions ext_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@

defined('TYPO3') || die();

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'][\Tvp\TemplaVoilaPlus\Hooks\WizardItems::class]
= \Tvp\TemplaVoilaPlus\Hooks\WizardItems::class;

$GLOBALS['TBE_STYLES']['skins']['templavoilaplus']['stylesheetDirectories'][]
= 'EXT:templavoilaplus/Resources/Public/StyleSheet/Skin';

0 comments on commit 0508ccd

Please sign in to comment.