Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Backport Element body middleware improvements #392

Merged
Merged
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
56 changes: 42 additions & 14 deletions Classes/Middleware/ElementBodyResponseMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,42 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

use function in_array;
use function is_array;
use function json_decode;

final class ElementBodyResponseMiddleware implements MiddlewareInterface
{
/**
* @var TypoScriptFrontendController
*/
private $tsfe;
/**
* @var JsonEncoder
*/
private $jsonEncoder;

public function __construct(
TypoScriptFrontendController $typoScriptFrontendController = null,
JsonEncoder $jsonEncoder = null
) {
$this->tsfe = $typoScriptFrontendController ?? $GLOBALS['TSFE'];
$this->jsonEncoder = $jsonEncoder ?? GeneralUtility::makeInstance(JsonEncoder::class);
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

if (!isset($this->tsfe->tmpl->setup['plugin.']['tx_headless.']['staticTemplate'])
|| (bool)$this->tsfe->tmpl->setup['plugin.']['tx_headless.']['staticTemplate'] === false
) {
/**
* @var Site
*/
$site = $request->getAttribute('site');

if (!($site instanceof Site)) {
return $response;
}

$siteConf = $request->getAttribute('site')->getConfiguration();

if (!($siteConf['headless'] ?? false)) {
return $response;
}

Expand All @@ -55,14 +62,19 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $response;
}

$recursiveElement = (bool)(int)($request->getParsedBody()['responseElementRecursive'] ?? 0);
$responseJson = json_decode($response->getBody()->__toString(), true);

if ($responseJson === null) {
return $response;
}

$stream = new Stream('php://temp', 'r+');
$stream->write($this->jsonEncoder->encode($this->extractElement($responseJson['content'] ?? [], $elementId)));
$stream->write($this->jsonEncoder->encode($this->extractElement(
$responseJson['content'] ?? [],
$elementId,
$recursiveElement
)));

return $response->withBody($stream);
}
Expand All @@ -72,15 +84,31 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
* @param int $elementId
* @return array<string, mixed>
*/
private function extractElement(array $content, int $elementId): array
private function extractElement(array $content, int $elementId, bool $recursiveElement = false): array
{
$body = [];

foreach ($content as $colPos => $items) {
foreach ($content as $items) {
if (!is_array($items)) {
continue;
}

foreach ($items as $item) {
if ((int)$item['id'] === $elementId) {
if ((int)($item['id'] ?? 0) === $elementId) {
return $item;
}

if ($recursiveElement && is_array($item)) {
foreach ($item as $prop) {
if (is_array($prop)) {
$result = $this->extractElement($prop, $elementId, true);

if (!empty($result)) {
return $result;
}
}
}
}
}
}

Expand Down