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

feat: provide content record and additionalInputData in WebcomponentRenderingData #5

Merged
merged 1 commit into from
Mar 21, 2024
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
10 changes: 6 additions & 4 deletions Classes/ContentObject/WebcomponentContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class WebcomponentContentObject extends AbstractContentObject
public function render($conf = []): string
{
$webcomponentRenderingData = GeneralUtility::makeInstance(WebcomponentRenderingData::class);
if ($this->cObj->getCurrentTable() === 'tt_content') {
$webcomponentRenderingData->setContentRecord($this->cObj->data);
}
$webcomponentRenderingData = $this->evaluateDataProvider($webcomponentRenderingData, $conf['dataProvider'] ?? '', $this->cObj);
$webcomponentRenderingData = $this->evaluateTypoScriptConfiguration($webcomponentRenderingData, $conf);

$contentElementRecordData = $this->cObj->getCurrentTable() === 'tt_content' ? $this->cObj->data : [];
$event = GeneralUtility::makeInstance(WebComponentWillBeRendered::class, $webcomponentRenderingData, $contentElementRecordData);
$event = GeneralUtility::makeInstance(WebComponentWillBeRendered::class, $webcomponentRenderingData);
$eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
$eventDispatcher->dispatch($event);

Expand Down Expand Up @@ -58,8 +60,8 @@ private function evaluateTypoScriptConfiguration(WebcomponentRenderingData $webc
private function renderMarkup(WebcomponentRenderingData $webcomponentRenderingData): string
{
$tagName = $webcomponentRenderingData->getTagName();
$content = $webcomponentRenderingData->getContent();
$properties = $webcomponentRenderingData->getProperties();
$content = $webcomponentRenderingData->getTagContent();
$properties = $webcomponentRenderingData->getTagProperties();

return $this->renderComponent($tagName, $content, $properties);
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/DataProvider/DataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

interface DataProviderInterface
{
public function provide(array $inputData, WebcomponentRenderingData $webcomponentRenderingData): WebcomponentRenderingData;
public function provide(WebcomponentRenderingData $webcomponentRenderingData): WebcomponentRenderingData;

public function setContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer): void;
}
3 changes: 1 addition & 2 deletions Classes/DataProvider/Traits/RenderComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ private function evaluateDataProvider(WebcomponentRenderingData $webcomponentRen
if (empty($dataProviderClassName)) {
return $webcomponentRenderingData;
}
$inputData = $contentObjectRenderer->data ?? [];
$dataProvider = GeneralUtility::makeInstance($dataProviderClassName);
if ($dataProvider instanceof DataProviderInterface) {
$dataProvider->setContentObjectRenderer($contentObjectRenderer);
$webcomponentRenderingData = $dataProvider->provide($inputData, $webcomponentRenderingData);
$webcomponentRenderingData = $dataProvider->provide($webcomponentRenderingData);
}
return $webcomponentRenderingData;
}
Expand Down
9 changes: 5 additions & 4 deletions Classes/DataProvider/Traits/RenderSubComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ trait RenderSubComponent
use ContentObjectRendererTrait;
use RenderComponent;

protected function renderSubComponent(string $dataProviderClassName, $inputData = []): ?string
protected function renderSubComponent(string $dataProviderClassName, $additionalInputData = []): ?string
{
$dataProvider = GeneralUtility::makeInstance($dataProviderClassName);
if (!$dataProvider instanceof DataProviderInterface) {
throw new \RuntimeException('DataProvider must implement DataProviderInterface');
}
$dataProvider->setContentObjectRenderer($this->contentObjectRenderer);
$webcomponentRenderingData = GeneralUtility::makeInstance(WebcomponentRenderingData::class);
$webcomponentRenderingData = $dataProvider->provide($inputData, $webcomponentRenderingData);
$webcomponentRenderingData->setAdditionalInputData($additionalInputData);
$webcomponentRenderingData = $dataProvider->provide($webcomponentRenderingData);

if (!$webcomponentRenderingData->isRenderable()) {
return null;
}

return $this->renderComponent(
$webcomponentRenderingData->getTagName(),
$webcomponentRenderingData->getContent(),
$webcomponentRenderingData->getProperties()
$webcomponentRenderingData->getTagContent(),
$webcomponentRenderingData->getTagProperties()
);
}
}
6 changes: 0 additions & 6 deletions Classes/Dto/Events/WebComponentWillBeRendered.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ class WebComponentWillBeRendered
{
public function __construct(
private readonly WebcomponentRenderingData $webcomponentRenderingData,
private readonly array $inputData,
) {
}

public function getInputData(): array
{
return $this->inputData;
}

public function getWebcomponentRenderingData(): WebcomponentRenderingData
{
return $this->webcomponentRenderingData;
Expand Down
46 changes: 34 additions & 12 deletions Classes/Dto/WebcomponentRenderingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,55 @@

class WebcomponentRenderingData
{
private ?string $content = null;
private array $properties = [];
private ?array $additionalInputData = null;
private ?array $contentRecord = null;
private ?string $tagContent = null;
private ?string $tagName = null;
private array $tagProperties = [];

public function getContent(): ?string
public function getAdditionalInputData(): ?array
{
return $this->content;
return $this->additionalInputData;
}

public function setContent(?string $content): void
public function setAdditionalInputData(?array $additionalInputData): void
{
$this->content = $content;
$this->additionalInputData = $additionalInputData;
}

public function getProperties(): ?array
public function getContentRecord(): ?array
{
return $this->properties;
return $this->contentRecord;
}

public function setProperty(string $key, $value): void
public function setContentRecord(?array $contentRecord): void
{
$this->properties[$key] = $value;
$this->contentRecord = $contentRecord;
}

public function setProperties(?array $properties): void
public function getTagContent(): ?string
{
$this->properties = $properties;
return $this->tagContent;
}

public function setTagContent(?string $tagContent): void
{
$this->tagContent = $tagContent;
}

public function getTagProperties(): ?array
{
return $this->tagProperties;
}

public function setTagProperty(string $key, $value): void
{
$this->tagProperties[$key] = $value;
}

public function setTagProperties(?array $tagProperties): void
{
$this->tagProperties = $tagProperties;
}

public function getTagName(): ?string
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ class MyContentElementDataProvider implements DataProviderInterface
{
use ContentObjectRendererTrait;

public function provide(array $inputData, WebcomponentRenderingData $webcomponentRenderingData): WebcomponentRenderingData
public function provide(WebcomponentRenderingData $webcomponentRenderingData): WebcomponentRenderingData
{
$record = $webcomponentRenderingData->getContentRecord();
$properties = [
'title' => $inputData['header'],
'title' => $record['header'],
'greeting' => 'Hello World!',
];

$webcomponentRenderingData->setTagName('my-web-component');
$webcomponentRenderingData->setProperties($properties);
$webcomponentRenderingData->setTagProperties($properties);
}
}
```
Expand Down