From 0554135a1d26487814ecbd011fe1d2222b292b35 Mon Sep 17 00:00:00 2001 From: Claus Due <claus@namelesscoder.net> Date: Sat, 4 Apr 2020 15:45:08 +0200 Subject: [PATCH] [FEATURE] Implement event for LocalizationController column building MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements a PSR-14 event dispatched after LocalizationController has built the manifest of columns and column numbers, allowing third parties to change this manifest without XCLASS'ing LocalizationController. Releases: master Resolves: #90945 Change-Id: I5a7929512c498329841c51ceceba7b0d4c156594 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64071 Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Benni Mack <benni@typo3.org> Tested-by: Frank Nägler <frank.naegler@typo3.org> Reviewed-by: Benni Mack <benni@typo3.org> Reviewed-by: Frank Nägler <frank.naegler@typo3.org> --- ...ageColumnsSelectedForLocalizationEvent.php | 108 ++++++++++++++++++ .../Page/LocalizationController.php | 23 +++- ...henReadingRecordscolumnsToBeTranslated.rst | 33 ++++++ 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 typo3/sysext/backend/Classes/Controller/Event/AfterPageColumnsSelectedForLocalizationEvent.php create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Feature-90945-PSR-14EventForLocalizationControllerWhenReadingRecordscolumnsToBeTranslated.rst diff --git a/typo3/sysext/backend/Classes/Controller/Event/AfterPageColumnsSelectedForLocalizationEvent.php b/typo3/sysext/backend/Classes/Controller/Event/AfterPageColumnsSelectedForLocalizationEvent.php new file mode 100644 index 000000000000..ca61790259b7 --- /dev/null +++ b/typo3/sysext/backend/Classes/Controller/Event/AfterPageColumnsSelectedForLocalizationEvent.php @@ -0,0 +1,108 @@ +<?php + +declare(strict_types=1); + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +namespace TYPO3\CMS\Backend\Controller\Event; + +use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout; + +/** + * This event triggers after the LocalizationController (AJAX) has + * selected page columns to be translated. Allows third parties to + * add to or change the columns and content elements withing those + * columns which will be available for localization through the + * "translate" modal in the page module. + */ +final class AfterPageColumnsSelectedForLocalizationEvent +{ + /** + * List of columns, indexed by column position number, value is label (either LLL: or hardcoded). + * + * @var array + */ + private $columns; + + /** + * Array of records which were used when building the original column + * manifest and column position numbers list. + * + * @var array + */ + private $records; + + /** + * Request parameters passed to LocalizationController. + * + * @var array + */ + private $parameters; + + /** + * @var BackendLayout + */ + private $backendLayout; + + /** + * List of integer column position numbers used in the BackendLayout. + * + * @var array + */ + private $columnList; + + public function __construct(array $columns, array $columnList, BackendLayout $backendLayout, array $records, array $parameters) + { + $this->columns = $columns; + $this->columnList = $columnList; + $this->backendLayout = $backendLayout; + $this->records = $records; + $this->parameters = $parameters; + } + + public function getColumns(): array + { + return $this->columns; + } + + public function setColumns(array $columns): void + { + $this->columns = $columns; + } + + public function getColumnList(): array + { + return $this->columnList; + } + + public function setColumnList(array $columnList): void + { + $this->columnList = $columnList; + } + + public function getBackendLayout(): BackendLayout + { + return $this->backendLayout; + } + + public function getRecords(): array + { + return $this->records; + } + + public function getParameters(): array + { + return $this->parameters; + } +} diff --git a/typo3/sysext/backend/Classes/Controller/Page/LocalizationController.php b/typo3/sysext/backend/Classes/Controller/Page/LocalizationController.php index e669334dc2ba..5b04784668ef 100644 --- a/typo3/sysext/backend/Classes/Controller/Page/LocalizationController.php +++ b/typo3/sysext/backend/Classes/Controller/Page/LocalizationController.php @@ -17,9 +17,11 @@ declare(strict_types=1); namespace TYPO3\CMS\Backend\Controller\Page; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider; +use TYPO3\CMS\Backend\Controller\Event\AfterPageColumnsSelectedForLocalizationEvent; use TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Backend\View\BackendLayoutView; @@ -58,6 +60,11 @@ class LocalizationController */ protected $localizationRepository; + /** + * @var EventDispatcherInterface + */ + protected $eventDispatcher; + /** * Constructor */ @@ -65,6 +72,7 @@ class LocalizationController { $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class); $this->localizationRepository = GeneralUtility::makeInstance(LocalizationRepository::class); + $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); } /** @@ -141,6 +149,7 @@ class LocalizationController '*' ); + $flatRecords = []; while ($row = $result->fetch()) { BackendUtility::workspaceOL('tt_content', $row, -99, true); if (!$row || VersionState::cast($row['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) { @@ -155,11 +164,12 @@ class LocalizationController 'title' => $row[$GLOBALS['TCA']['tt_content']['ctrl']['label']], 'uid' => $row['uid'] ]; + $flatRecords[] = $row; } return (new JsonResponse())->setPayload([ 'records' => $records, - 'columns' => $this->getPageColumns($pageId), + 'columns' => $this->getPageColumns($pageId, $flatRecords, $params), ]); } @@ -255,9 +265,11 @@ class LocalizationController /** * @param int $pageId + * @param array $flatRecords + * @param array $params * @return array */ - protected function getPageColumns(int $pageId): array + protected function getPageColumns(int $pageId, array $flatRecords, array $params): array { $columns = []; $backendLayoutView = GeneralUtility::makeInstance(BackendLayoutView::class); @@ -267,9 +279,12 @@ class LocalizationController $columns[$columnPos] = $GLOBALS['LANG']->sL($columnLabel); } + $event = GeneralUtility::makeInstance(AfterPageColumnsSelectedForLocalizationEvent::class, $columns, array_values($backendLayout->getColumnPositionNumbers()), $backendLayout, $flatRecords, $params); + $this->eventDispatcher->dispatch($event); + return [ - 'columns' => $columns, - 'columnList' => array_values($backendLayout->getColumnPositionNumbers()), + 'columns' => $event->getColumns(), + 'columnList' => $event->getColumnList() ]; } } diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-90945-PSR-14EventForLocalizationControllerWhenReadingRecordscolumnsToBeTranslated.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-90945-PSR-14EventForLocalizationControllerWhenReadingRecordscolumnsToBeTranslated.rst new file mode 100644 index 000000000000..01e7b96f4fba --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-90945-PSR-14EventForLocalizationControllerWhenReadingRecordscolumnsToBeTranslated.rst @@ -0,0 +1,33 @@ +.. include:: ../../Includes.txt + +======================================================================================================= +Feature: #90945 - PSR-14 event for LocalizationController when reading records/columns to be translated +======================================================================================================= + +See :issue:`90945` + +Description +=========== + +A new PSR-14 event ``\TYPO3\CMS\Backend\Controller\Event\AfterPageColumnsSelectedForLocalizationEvent`` +has been added and will be dispatched after records and columns are collected in the ``LocalizationController``. + +The event receives: + +* The default columns and columnsList built by ``LocalizationController``. +* The list of records that were analyzed to create the columns manifest. +* The parameters received by the ``LocalizationController``. + +The event allows changes to: + +* The columns and the columnsList. + + +Impact +====== + +This allows third party code to read or manipulate the "columns manifest" that gets displayed in the +translation modal when a user has clicked the "Translate" button in the page module, by implementing +a listener for the ``\TYPO3\CMS\Backend\Controller\Event\AfterPageColumnsSelectedForLocalizationEvent`` event. + +.. index:: Backend, ext:backend -- GitLab