Skip to content

Commit

Permalink
[TASK] prevent changing language fields for container children
Browse files Browse the repository at this point in the history
Fixes: #499
  • Loading branch information
achimfritz committed Sep 11, 2024
1 parent 8d1036b commit 8377274
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Classes/Hooks/Datahandler/DatamapBeforeStartHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use B13\Container\Domain\Service\ContainerService;
use B13\Container\Tca\Registry;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\MathUtility;

class DatamapBeforeStartHook
{
Expand Down Expand Up @@ -56,6 +57,49 @@ public function processDatamap_beforeStart(DataHandler $dataHandler): void
{
$dataHandler->datamap = $this->datamapForChildLocalizations($dataHandler->datamap);
$dataHandler->datamap = $this->datamapForChildrenChangeContainerLanguage($dataHandler->datamap);
$dataHandler->datamap = $this->denyChangingOfLanguageFieldsForContainerChildren($dataHandler->datamap, $dataHandler);
}

protected function denyChangingOfLanguageFieldsForContainerChildren(array $datamap, DataHandler $dataHandler):array
{
foreach ($datamap['tt_content'] ?? [] as $uid => &$datas) {
if (!MathUtility::canBeInterpretedAsInteger($uid)) {
continue;
}
if (!isset($datas['sys_language_uid']) && !isset($datas['l18n_parent'])) {
continue;
}
$record = $this->database->fetchOneRecord((int)$uid);
if ($record === null || (int)($record['tx_container_parent'] ?? 0) === 0) {
continue;
}
if (isset($datas['sys_language_uid'])) {
$dataHandler->log(
'tt_content',
$uid,
2,
0,
1,
'cannot change language of container child record',
28
);
unset($datas['sys_language_uid']);
}
if (isset($datas['l18n_parent'])) {
unset($datas['l18n_parent']);
$dataHandler->log(
'tt_content',
$uid,
2,
0,
1,
'cannot change parent of container child record',
28
);
}
$datamap['tt_content'][$uid] = $datas;
}
return $datamap;
}

protected function datamapForChildLocalizations(array $datamap): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace B13\Container\Tests\Functional\Datahandler\Localization\ConnectedMode;

/*
* This file is part of TYPO3 CMS-based extension "container" by b13.
*
* 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.
*/

use B13\Container\Tests\Functional\Datahandler\AbstractDatahandler;

class ContainerChildrenTest extends AbstractDatahandler
{
/**
* @test
*/
public function cannotChangeLanguageOfTranslatedChild(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/ContainerChildren/translated_container_with_children.csv');
$datamap = [
'tt_content' => [
22 => [
'sys_language_uid' => 0,
],
],
];
$this->dataHandler->start($datamap, [], $this->backendUser);
$this->dataHandler->process_datamap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/ContainerChildren/translated_container_with_children.csv');
self::assertNotEmpty($this->dataHandler->errorLog, 'dataHander error log is empty');
}

/**
* @test
*/
public function cannotChangeL18nParentOfTranslatedChild(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/ContainerChildren/translated_container_with_children.csv');
$datamap = [
'tt_content' => [
22 => [
'l18n_parent' => 1,
],
],
];
$this->dataHandler->start($datamap, [], $this->backendUser);
$this->dataHandler->process_datamap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/ContainerChildren/translated_container_with_children.csv');
self::assertNotEmpty($this->dataHandler->errorLog, 'dataHander error log is empty');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"pages"
,"uid","pid","title","slug","sys_language_uid","l10n_parent","l10n_source"
,1,0,"page-1","/",0,0,0
,2,0,"page-1-language-1","/",1,1,1
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent"
,1,1,"b13-2cols-with-header-container","container-default",256,0,0,0,0
,2,1,"header","header-default",128,0,200,1,0
,21,1,"b13-2cols-with-header-container","container-language-1",256,1,0,0,1
,22,1,"header","header-language-1",128,1,200,1,2

0 comments on commit 8377274

Please sign in to comment.