Skip to content

Commit

Permalink
[FEATURE] Added TypoScriptStructuredDataProvider to add structured da…
Browse files Browse the repository at this point in the history
…ta based on typoscript (premium functionality)
  • Loading branch information
Riny van Tiggelen committed Oct 23, 2024
1 parent fae1d8a commit 358dd14
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file.
We will follow [Semantic Versioning](http://semver.org/).

## UNRELEASED
### Added
- `TypoScriptStructuredDataProvider` to add structured data to the page, configured with TypoScript (premium functionality)

### Fixed
- Renamed `JavascriptModules.php` to `JavaScriptModules.php`

Expand Down
54 changes: 54 additions & 0 deletions Classes/StructuredData/TypoScriptStructuredDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\StructuredData;

use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

class TypoScriptStructuredDataProvider implements StructuredDataProviderInterface
{
public function __construct(
protected SiteFinder $siteFinder,
protected PageRepository $pageRepository,
protected TypoScriptService $typoScriptService,
) {}

/**
* @return array<array<string, mixed>>
*/
public function getData(): array
{
$data = [];

foreach (
$this->getTyposcriptFrontendController()->config['config']['structuredData.']['data.'] ?? [] as $dataConfig
) {
if (array_key_exists('type', $dataConfig) && array_key_exists('context', $dataConfig)) {
$item = [];
$config = $this->typoScriptService->convertTypoScriptArrayToPlainArray($dataConfig);

foreach ($config as $key => $value) {
$cObject = $key . '.';
if (isset($dataConfig[$cObject])) {
$value = $this->getTyposcriptFrontendController()->cObj->stdWrap($key, $dataConfig[$cObject]);

Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php

View workflow job for this annotation

GitHub Actions / Build PHP (12, php8.2, false)

Parameter #1 $content of method TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::stdWrap() expects string, int|string given.

Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php

View workflow job for this annotation

GitHub Actions / Build PHP (13, php8.2, false)

Parameter #1 $content of method TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::stdWrap() expects string, int|string given.

Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php

View workflow job for this annotation

GitHub Actions / Build PHP (12, php8.2, false)

Parameter #1 $content of method TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::stdWrap() expects string, int|string given.

Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php

View workflow job for this annotation

GitHub Actions / Build PHP (13, php8.2, false)

Parameter #1 $content of method TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::stdWrap() expects string, int|string given.
}
$key = in_array($key, ['type', 'context']) ? '@' . $key : $key;

$item[$key] = $value;
}
$data[] = $item;
}
}

return (array)$data;
}

protected function getTyposcriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
}
5 changes: 5 additions & 0 deletions Configuration/TypoScript/Setup/Config.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ config {
site {
provider = YoastSeoForTypo3\YoastSeo\StructuredData\SiteStructuredDataProvider
}

typoscript {
provider = YoastSeoForTypo3\YoastSeo\StructuredData\TypoScriptStructuredDataProvider
after = breadcrumb
}
}
pageTitleProviders {
yoastRecord {
Expand Down

0 comments on commit 358dd14

Please sign in to comment.