Skip to content
Open
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
4 changes: 2 additions & 2 deletions Build/phpstan13.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ parameters:
- %currentWorkingDirectory%/Classes/Listener/PageContentPreviewRendering.php
- %currentWorkingDirectory%/Classes/Listener/ManipulateBackendLayoutColPosConfigurationForPage.php
- %currentWorkingDirectory%/Classes/Hooks/Datahandler/ContentElementRestriction
- %currentWorkingDirectory%/Classes/Listener/IsReferenceConsideredForDependency.php

- %currentWorkingDirectory%/Classes/DataProcessing/ContentAreaProcessor.php
- %currentWorkingDirectory%/Classes/Listener/IsReferenceConsideredForDependency.php
136 changes: 136 additions & 0 deletions Classes/DataProcessing/ContentAreaProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

namespace B13\Container\DataProcessing;

/*
* 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 function array_map;
use B13\Container\Domain\Factory\FrontendContainerFactory;
use B13\Container\Tca\Registry;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Domain\RecordFactory;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Page\ContentArea;
use TYPO3\CMS\Core\Page\ContentAreaClosure;
use TYPO3\CMS\Core\Page\ContentAreaCollection;
use TYPO3\CMS\Core\Page\ContentSlideMode;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;

/**
* Automatically detects if content element has container columns
* adds them lazily to the content variable.
* The ContentArea can be used in f:render.contentArea ViewHelper
*
* Only use this DataProcessor for TYPO3 v14 or higher:
*
* typoscript:
* lib.contentElement.dataProcessing.1773665522 = B13\Container\DataProcessing\ContentAreaProcessor
* #or
* tt_content.b13-2cols < lib.contentElement
* tt_content.b13-2cols {
* templateName = 2Cols
* templateRootPaths.10 = EXT:base/Resources/Private/Templates
* dataProcessing.100 = B13\Container\DataProcessing\ContentAreaProcessor
* }
*
* html:
* <f:render.contentArea contentArea="{content.200}" />
*/
#[Autoconfigure(public: true)]
readonly class ContentAreaProcessor implements DataProcessorInterface
{
public function __construct(
protected ContentDataProcessor $contentDataProcessor,
protected Context $context,
protected FrontendContainerFactory $frontendContainerFactory,
protected Registry $tcaRegistry,
protected RecordFactory $recordFactory,
protected Typo3Version $typo3Version,
protected LoggerInterface $logger,
) {
}

public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData,
): array {
if ($this->typo3Version->getMajorVersion() < 14) {
$this->logger->error(ContentAreaProcessor::class . ' requires TYPO3 v14 or higher. Please check your configuration.');
return $processedData;
}

if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
return $processedData;
}
$contentId = null;
if ($processorConfiguration['contentId.'] ?? false) {
$contentId = (int)$cObj->stdWrap($processorConfiguration['contentId'] ?? '', $processorConfiguration['contentId.']);
} elseif ($processorConfiguration['contentId'] ?? false) {
$contentId = (int)$processorConfiguration['contentId'];
}
if ($contentId !== null) {
$records = $cObj->getRecords('tt_content', ['uidInList' => $contentId, 'pidInList' => 0]);
if (empty($records)) {
return $processedData;
}
$record = $records[0];
} else {
$record = $cObj->data;
}

$CType = $record['CType'] ?? '';
if (!$this->tcaRegistry->isContainerElement($CType)) {
return $processedData;
}

$columnsColPos = $this->tcaRegistry->getAllAvailableColumnsColPos($CType);

$container = null;

$areas = [];
foreach ($columnsColPos as $colPos) {
$areas[$colPos] = new ContentAreaClosure(
function () use (&$container, $CType, $cObj, $record, $colPos): ContentArea {
$container ??= $this->frontendContainerFactory->buildContainer($cObj, $this->context, (int)$record['uid']);

$contentDefenderConfiguration = $this->tcaRegistry->getContentDefenderConfiguration($CType, $colPos);

$rows = $container->getChildrenByColPos($colPos);

$records = array_map(fn ($row) => $this->recordFactory->createFromDatabaseRow('tt_content', $row), $rows);
return new ContentArea(
(string)$colPos,
$this->tcaRegistry->getColPosName($record['CType'], $colPos),
$colPos,
ContentSlideMode::None,
GeneralUtility::trimExplode(',', $contentDefenderConfiguration['allowedContentTypes'] ?? '', true),
GeneralUtility::trimExplode(',', $contentDefenderConfiguration['disallowedContentTypes'] ?? '', true),
[
'container' => $container,
],
$records,
);
},
);
}

$processedData[$processorConfiguration['as'] ?? 'content'] = (new ContentAreaCollection($areas))->withRequest($cObj->getRequest());
return $processedData;
}
}
13 changes: 13 additions & 0 deletions Classes/Tca/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ public function getContainerLabel(string $cType): string
return $GLOBALS['TCA']['tt_content']['containerConfiguration'][$cType]['label'] ?? $cType;
}

public function getColPosName(string $cType, int $colPos): ?string
{
$grid = $this->getGrid($cType);
foreach ($grid as $row) {
foreach ($row as $column) {
if ($column['colPos'] === $colPos) {
return (string)$column['name'];
}
}
}
return null;
}

public function getAvailableColumns(string $cType): array
{
$columns = [];
Expand Down
5 changes: 4 additions & 1 deletion Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ services:

B13\Container\DataProcessing\ContainerProcessor:
tags:
- { name: 'data.processor', identifier: 'container' }
- { name: 'data.processor', identifier: 'container' }
B13\Container\DataProcessing\ContentAreaProcessor:
tags:
- { name: 'data.processor', identifier: 'container-content-area' }
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,47 @@ The HTML template file goes in the folder you defined in your TypoScript above (

With explicit colPos defined use `{children_200|201}` as set in the example above

## ContentAreaProcessor for TYPO3 v14 or above

for TYPO3 v14 or higher you can use the ConentAreaProcessor
Automatically detects if content element has container columns
adds them lazily to the content variable.
The ContentArea can be used in f:render.contentArea or f:render.record ViewHelper


### TypoScript

tt_content.b13-2cols-with-header-container < lib.contentElement
tt_content.b13-2cols-with-header-container {
templateName = 2ColsWithHeader
templateRootPaths {
10 = EXT:container_example/Resources/Private/Templates
}
dataProcessing {
100 = B13\Container\DataProcessing\ContentAreaProcessor
}
}

### Options for ContentAreaProcessor

| Option | Description | Default | Parameter |
|-----------------------------|-----------------------------------|-------------------------------------------------------|-------------|
| `contentId` | id of container to to process | current uid of content element ``$cObj->data['uid']`` | ``?int`` |
| `as` | variable to use for proceesedData | ``content`` | ``?string`` |

### Template

```html
<f:if condition="{content.200}">
<div class="header-children">{content.200 -> f:render.contentArea()}</div>
</f:if>
<f:if condition="{content.201}">
<f:for each="{content.201}" as="record">
{record -> f:render.record()}
</f:for>
</f:if>
```

## PSR-14 Events

### BeforeContainerConfigurationIsAppliedEvent
Expand Down
90 changes: 90 additions & 0 deletions Tests/Functional/Frontend/ContentArea/DefaultLanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace B13\Container\Tests\Functional\Frontend\ContentArea;

/*
* 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\Frontend\AbstractFrontend;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;

class DefaultLanguageTest extends AbstractFrontend
{
#[Test]
#[Group('frontend')]
#[Group('v14-only')]
public function childrenAreRendered(): void
{
$this->importCSVDataSet(__DIR__ . '/../Fixtures/default_language.csv');
$this->setUpFrontendRootPage(
1,
[
'constants' => ['EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/constants.typoscript'],
'setup' => [
'EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/setup.typoscript',
'EXT:container_example/Configuration/Sets/ContainerExampleContentArea/setup.typoscript',
],
]
);
$response = $this->executeFrontendRequestWrapper(new InternalRequest('http://localhost/'));
$body = (string)$response->getBody();
$body = $this->prepareContent($body);
// rendered content
self::assertStringContainsString('<h1 class="container">container-default</h1>', $body);
self::assertStringContainsString('<header><h2 class="">header-default</h2></header>', $body);
self::assertStringContainsString('<header><h2 class="">left-side-default</h2>', $body);
}

#[Test]
#[Group('frontend')]
#[Group('v14-only')]
public function childrenAreRenderedAsSorted(): void
{
$this->importCSVDataSet(__DIR__ . '/../Fixtures/ContainerWithTwoChildren.csv');
$this->setUpFrontendRootPage(
1,
[
'constants' => ['EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/constants.typoscript'],
'setup' => [
'EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/setup.typoscript',
'EXT:container_example/Configuration/Sets/ContainerExampleContentArea/setup.typoscript',
'EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/container_with_two_children.typoscript',
],
]
);
$response = $this->executeFrontendRequestWrapper(new InternalRequest('http://localhost/'));
$body = (string)$response->getBody();
$body = $this->prepareContent($body);
self::assertStringContainsString('<h6>first child</h6><h6>second child</h6>', $body);
}

#[Test]
#[Group('frontend')]
#[Group('v14-only')]
public function canRenderContainerFromOtherPage(): void
{
$this->importCSVDataSet(__DIR__ . '/../Fixtures/ContainerFromOtherPage.csv');
$this->setUpFrontendRootPage(
1,
[
'constants' => ['EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/constants.typoscript'],
'setup' => [
'EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/setup.typoscript',
'EXT:container_example/Configuration/Sets/ContainerExampleContentArea/setup.typoscript',
'EXT:container/Tests/Functional/Frontend/Fixtures/TypoScript/container_from_other_page.typoscript',
],
]
);
$response = $this->executeFrontendRequestWrapper(new InternalRequest('http://localhost/'));
$body = (string)$response->getBody();
$body = $this->prepareContent($body);
self::assertStringContainsString('<header><h2 class="">child</h2></header>', $body);
}
}
Loading
Loading