Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Classes/Hooks/Datahandler/CommandMapBeforeStartHook.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 Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;

#[Autoconfigure(public: true)]
Expand All @@ -36,6 +37,9 @@ public function processCmdmap_beforeStart(DataHandler $dataHandler): void
$dataHandler->cmdmap = $this->rewriteSimpleCommandMap($dataHandler->cmdmap);
$dataHandler->cmdmap = $this->setContainerIdToZeroIfNotSetOnUpdate($dataHandler->cmdmap);
$this->unsetInconsistentCopyOrMoveCommands($dataHandler);
// children are copied/moved together with their container (CommandMapPostProcessingHook),
// an additional command for a child would duplicate it (e.g. "select all" in Record module)
$this->unsetCopyOrMoveCommandsForChildrenOfContainersInCmdmap($dataHandler);
// previously page id is used for copy/moving element at top of a container colum
// but this leeds to wrong sorting in page context (e.g. List-Module)
$dataHandler->cmdmap = $this->rewriteCommandMapTargetForTopAtContainer($dataHandler->cmdmap);
Expand Down Expand Up @@ -89,6 +93,62 @@ protected function unsetInconsistentCopyOrMoveCommands(DataHandler $dataHandler)
}
}

protected function unsetCopyOrMoveCommandsForChildrenOfContainersInCmdmap(DataHandler $dataHandler): void
{
$commands = $dataHandler->cmdmap['tt_content'] ?? [];
if (count($commands) < 2) {
return;
}
$workspaceId = (int)($dataHandler->BE_USER->workspace ?? 0);
foreach ($commands as $id => $cmds) {
if (!is_array($cmds)) {
continue;
}
foreach (['copy', 'move'] as $operation) {
if (!isset($cmds[$operation])) {
continue;
}
if ($this->hasAncestorContainerWithSameCommand((int)$id, $operation, $cmds[$operation], $commands, $workspaceId)) {
unset($dataHandler->cmdmap['tt_content'][$id][$operation]);
}
}
if (empty($dataHandler->cmdmap['tt_content'][$id])) {
unset($dataHandler->cmdmap['tt_content'][$id]);
}
}
}

/**
* a child command is only redundant if it is identical to the command of one of its containers,
* a child with an own target (e.g. pasted into another container) is kept
*/
protected function hasAncestorContainerWithSameCommand(int $id, string $operation, mixed $command, array $commands, int $workspaceId): bool
{
$visited = [$id => true];
$parentId = $this->fetchContainerParent($id, $workspaceId);
while ($parentId > 0 && !isset($visited[$parentId])) {
if (isset($commands[$parentId][$operation]) && $commands[$parentId][$operation] == $command) {
return true;
}
$visited[$parentId] = true;
$parentId = $this->fetchContainerParent($parentId, $workspaceId);
}
return false;
}

protected function fetchContainerParent(int $uid, int $workspaceId): int
{
$record = $this->database->fetchOneRecord($uid);
if ($record !== null && $workspaceId > 0) {
// the child may have been moved in/out of a container in the workspace
BackendUtility::workspaceOL('tt_content', $record, $workspaceId, true);
}
if (!is_array($record)) {
return 0;
}
return (int)($record['tx_container_parent'] ?? 0);
}

protected function setContainerIdToZeroIfNotSetOnUpdate(array $cmdmap): array
{
if (!empty($cmdmap['tt_content'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace B13\Container\Tests\Functional\Datahandler\DefaultLanguage;

/*
* 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;
use PHPUnit\Framework\Attributes\Test;

/**
* container and its children are part of the same cmdmap,
* e.g. "select all" and paste in the Record module (clipboard reverses the order)
*/
class ContainerWithChildrenInCmdmapTest extends AbstractDatahandler
{
#[Test]
public function copyContainerWithChildrenInCmdmapCopiesChildrenOnlyOnce(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/ContainerWithChildrenInCmdmap/Container.csv');
$cmdmap = [
'tt_content' => [
3 => ['copy' => 3],
2 => ['copy' => 3],
1 => ['copy' => 3],
],
];
$this->dataHandler->start([], $cmdmap, $this->backendUser);
$this->dataHandler->process_cmdmap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/ContainerWithChildrenInCmdmap/CopyContainerWithChildrenInCmdmapCopiesChildrenOnlyOnceResult.csv');
}

#[Test]
public function moveContainerWithChildrenInCmdmapKeepsChildrenInContainer(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/ContainerWithChildrenInCmdmap/Container.csv');
$cmdmap = [
'tt_content' => [
3 => ['move' => 3],
2 => ['move' => 3],
1 => ['move' => 3],
],
];
$this->dataHandler->start([], $cmdmap, $this->backendUser);
$this->dataHandler->process_cmdmap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/ContainerWithChildrenInCmdmap/MoveContainerWithChildrenInCmdmapKeepsChildrenInContainerResult.csv');
}

#[Test]
public function copyNestedContainerWithChildrenInCmdmapCopiesChildrenOnlyOnce(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/ContainerWithChildrenInCmdmap/NestedContainer.csv');
$cmdmap = [
'tt_content' => [
3 => ['copy' => 3],
2 => ['copy' => 3],
1 => ['copy' => 3],
],
];
$this->dataHandler->start([], $cmdmap, $this->backendUser);
$this->dataHandler->process_cmdmap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/ContainerWithChildrenInCmdmap/CopyNestedContainerWithChildrenInCmdmapCopiesChildrenOnlyOnceResult.csv');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"pages"
,"uid","pid","title"
,1,0,"page-1"
,3,0,"page-2"
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent"
,1,1,"b13-2cols-with-header-container","container",64,0,0,0,0
,2,1,"header","col-200",128,0,200,1,0
,3,1,"header","col-201",192,0,201,1,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"pages"
,"uid","pid","title"
,1,0,"page-1"
,3,0,"page-2"
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent"
,1,1,"b13-2cols-with-header-container","container",64,0,0,0,0
,2,1,"header","col-200",128,0,200,1,0
,3,1,"header","col-201",192,0,201,1,0
,4,3,"b13-2cols-with-header-container","container",256,0,0,0,0
,5,3,"header","col-200",512,0,200,4,0
,6,3,"header","col-201",768,0,201,4,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"pages"
,"uid","pid","title"
,1,0,"page-1"
,3,0,"page-2"
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent"
,1,1,"b13-2cols-with-header-container","container",64,0,0,0,0
,2,1,"b13-2cols-with-header-container","child-container",128,0,200,1,0
,3,1,"header","content-in-child-container",192,0,200,2,0
,4,3,"b13-2cols-with-header-container","container",256,0,0,0,0
,5,3,"b13-2cols-with-header-container","child-container",512,0,200,4,0
,6,3,"header","content-in-child-container",768,0,200,5,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"pages"
,"uid","pid","title"
,1,0,"page-1"
,3,0,"page-2"
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent"
,1,3,"b13-2cols-with-header-container","container",256,0,0,0,0
,2,3,"header","col-200",512,0,200,1,0
,3,3,"header","col-201",768,0,201,1,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"pages"
,"uid","pid","title"
,1,0,"page-1"
,3,0,"page-2"
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent"
,1,1,"b13-2cols-with-header-container","container",64,0,0,0,0
,2,1,"b13-2cols-with-header-container","child-container",128,0,200,1,0
,3,1,"header","content-in-child-container",192,0,200,2,0
16 changes: 16 additions & 0 deletions Tests/Functional/Datahandler/Workspace/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,20 @@ public function deleteContainerWithChildHasDeletedPlaceholderInWorkspaceDoNotDis
$this->dataHandler->process_cmdmap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/DeleteContainerWithChildHasDeletedPlaceholderInWorkspaceDoNotDiscardThisChildResult.csv');
}

#[Test]
public function copyContainerWithChildrenInCmdmapCopiesChildMovedOutOfContainerInWorkspace(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/CopyContainerWithChildrenInCmdmapChildMovedOutOfContainer.csv');
$cmdmap = [
'tt_content' => [
3 => ['copy' => 3],
2 => ['copy' => 3],
1 => ['copy' => 3],
],
];
$this->dataHandler->start([], $cmdmap, $this->backendUser);
$this->dataHandler->process_cmdmap();
self::assertCSVDataSet(__DIR__ . '/Fixtures/CopyContainerWithChildrenInCmdmapChildMovedOutOfContainerResult.csv');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent","l10n_source","t3ver_oid","t3ver_wsid","t3ver_state","t3ver_stage"
,1,1,"b13-2cols-with-header-container","container",128,0,0,0,0,0,0,0,0,0
,2,1,"header","moved-out-in-workspace",256,0,200,1,0,0,0,0,0,0
,3,1,"header","child",512,0,201,1,0,0,0,0,0,0
,4,1,"header","moved-out-in-workspace",1024,0,0,0,0,0,2,1,4,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"tt_content"
,"uid","pid","CType","header","sorting","sys_language_uid","colPos","tx_container_parent","l18n_parent","l10n_source","t3ver_oid","t3ver_wsid","t3ver_state","t3ver_stage"
,1,1,"b13-2cols-with-header-container","container",128,0,0,0,0,0,0,0,0,0
,2,1,"header","moved-out-in-workspace",256,0,200,1,0,0,0,0,0,0
,3,1,"header","child",512,0,201,1,0,0,0,0,0,0
,4,1,"header","moved-out-in-workspace",1024,0,0,0,0,0,2,1,4,0
,5,3,"header","moved-out-in-workspace",256,0,0,0,0,0,0,1,1,0
,6,3,"b13-2cols-with-header-container","container",128,0,0,0,0,0,0,1,1,0
,7,3,"header","child",192,0,201,6,0,0,0,1,1,0
Loading