Skip to content

Commit

Permalink
[TASK] Restructure grid column
Browse files Browse the repository at this point in the history
All extending properties from the grid column are moved from the class to the override property and the access in the templates are aligned.
The processing of the values is moved to BeforeContainerPreviewIsRenderedListener
  • Loading branch information
garbast committed Nov 30, 2024
1 parent 4f054a6 commit eca793c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 61 deletions.
41 changes: 33 additions & 8 deletions Classes/EventListener/BeforeContainerPreviewIsRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Evoweb\EwCollapsibleContainer\EventListener;

use B13\Container\Backend\Grid\ContainerGridColumn as BaseContainerGridColum;
use B13\Container\Backend\Grid\ContainerGridColumnItem;
use B13\Container\Events\BeforeContainerPreviewIsRenderedEvent;
use Evoweb\EwCollapsibleContainer\Xclass\ContainerGridColumn;
use TYPO3\CMS\Core\Attribute\AsEventListener;
Expand All @@ -25,25 +27,48 @@ class BeforeContainerPreviewIsRenderedListener
#[AsEventListener('collapsible-container-beforepreview', BeforeContainerPreviewIsRenderedEvent::class)]
public function __invoke(BeforeContainerPreviewIsRenderedEvent $event): void
{
$grid = $event->getGrid();
$record = $event->getItem()->getRecord();

/** @var ContainerGridColumn $columnObject */
foreach ($grid->getColumns() as $columnObject) {
$this->setColumnCollapsedState((int)$record['uid'], $columnObject);
/** @var ContainerGridColumn $column */
foreach ($event->getGrid()->getColumns() as $column) {
$countOfHiddenItems = $this->getCountOfHiddenItems($column);
$column->setOverride([
'countOfHiddenItems' => $countOfHiddenItems,
'collapsed' => $this->getColumnCollapsed((int)$record['uid'], $column),
'showMinItemsWarning' => $this->getShowMinItemsWarning($column, $countOfHiddenItems)
]);
}
}

protected function setColumnCollapsedState(int $recordUid, ContainerGridColumn $column): void
protected function getCountOfHiddenItems(ContainerGridColumn $columnObject): int
{
return count(
array_filter(
$columnObject->getItems(),
fn (ContainerGridColumnItem $item) => ($item->getRecord()['hidden'] ?? 0) > 0
)
);
}

protected function getColumnCollapsed(int $recordUid, ContainerGridColumn $columnObject): bool
{
$backendUser = $this->getBackendUser();
$collapseId = $recordUid . ContainerGridColumn::CONTAINER_COL_POS_DELIMITER . $column->getColumnNumber();
$collapseId = $recordUid
. BaseContainerGridColum::CONTAINER_COL_POS_DELIMITER
. $columnObject->getColumnNumber();
if (isset($backendUser->uc['moduleData']['list']['containerExpanded'][$collapseId])) {
$collapsed = $backendUser->uc['moduleData']['list']['containerExpanded'][$collapseId] > 0;
} else {
$collapsed = (bool)($column->getDefinition()['collapsed'] ?? false);
$collapsed = (bool)($columnObject->getDefinition()['collapsed'] ?? false);
}
$column->setCollapsed($collapsed);
return $collapsed;
}

protected function getShowMinItemsWarning(ContainerGridColumn $columnObject, int $hiddenItemCount): bool
{
$itemCount = count($columnObject->getItems());
$minItems = (int)($columnObject->getDefinition()['minitems'] ?? 0);
return $itemCount > 0 && ($itemCount - $hiddenItemCount) < $minItems;
}

protected function getBackendUser(): BackendUserAuthentication
Expand Down
53 changes: 5 additions & 48 deletions Classes/Xclass/ContainerGridColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,19 @@

namespace Evoweb\EwCollapsibleContainer\Xclass;

use B13\Container\Backend\Grid\ContainerGridColumnItem;
use B13\Container\Backend\Grid\ContainerGridColumn as BaseContainerGridColumn;
use B13\Container\Domain\Model\Container;
use TYPO3\CMS\Backend\View\PageLayoutContext;

class ContainerGridColumn extends BaseContainerGridColumn
{
protected bool $collapsed = false;
protected array $override = [];

protected int $minItems = 0;

public function __construct(
PageLayoutContext $context,
array $columnDefinition,
Container $container,
?string $newContentUrl,
bool $skipNewContentElementWizard
) {
parent::__construct(
$context,
$columnDefinition,
$container,
$newContentUrl,
$skipNewContentElementWizard
);
$this->minItems = (int)($columnDefinition['minitems'] ?? 0);
}

public function getCollapsed(): bool
{
return $this->collapsed;
}

public function setCollapsed(bool $collapsed): void
{
$this->collapsed = $collapsed;
}

public function hasShowMinItemsWarning(): bool
{
return count($this->items) > 0
&& (count($this->items) - $this->getCountOfHiddenItems()) < $this->getMinItems();
}

public function getMinItems(): int
public function setOverride(array $override): void
{
return $this->minItems;
$this->override = $override;
}

public function getCountOfHiddenItems(): int
public function getDefinition(): array
{
return count(
array_filter(
$this->items,
fn (ContainerGridColumnItem $item) => ($item->getRecord()['hidden'] ?? 0) > 0
)
);
return array_merge($this->definition, $this->override);
}
}
2 changes: 1 addition & 1 deletion Resources/Private/Partials/PageLayout/Grid/Column.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{f:if(condition: column.unassigned, then: 't3-grid-cell-unassigned')}
{f:if(condition: '!{column.active} && !{column.unused}', then: 't3-grid-cell-restricted')}
{f:if(condition: '!{column.active} && {hideRestrictedColumns} && !{column.unused}', then: 't3-grid-cell-hidden')}
{f:if(condition: column.collapsed, then: 'collapsed')}
{f:if(condition: column.definition.collapsed, then: 'collapsed')}
</f:variable>

<td valign="top" colspan="{column.colSpan}" rowspan="{column.rowSpan}"
Expand Down
8 changes: 4 additions & 4 deletions Resources/Private/Partials/PageLayout/Grid/ColumnHeader.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
</f:if>
</div>
<span id="{columnIdentifier}">{column.title}</span>
<f:if condition="{column.showMinItemsWarning}"><span class="fw-bold link-danger">
<f:if condition="{column.hiddenItemCount}">
<f:if condition="{column.definition.showMinItemsWarning}"><span class="fw-bold link-danger">
<f:if condition="{column.definition.countOfHiddenItems}">
<f:then>
<f:translate id="LLL:EXT:ew_collapsible_container/Resources/Private/Language/locallang.xlf:minimum-item-count-not-reached-with-hidden" arguments="{1: column.minitems, 2: column.hiddenItemCount}"/>
<f:translate id="LLL:EXT:ew_collapsible_container/Resources/Private/Language/locallang.xlf:minimum-item-count-not-reached-with-hidden" arguments="{1: column.definition.minitems, 2: column.definition.countOfHiddenItems}"/>
</f:then>
<f:else>
<f:translate id="LLL:EXT:ew_collapsible_container/Resources/Private/Language/locallang.xlf:minimum-item-count-not-reached" arguments="{1: column.minitems}"/>
<f:translate id="LLL:EXT:ew_collapsible_container/Resources/Private/Language/locallang.xlf:minimum-item-count-not-reached" arguments="{1: column.definition.minitems}"/>
</f:else>
</f:if>
</span></f:if>
Expand Down

0 comments on commit eca793c

Please sign in to comment.