-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Added TypoScriptStructuredDataProvider to add structured da…
…ta based on typoscript (premium functionality)
- Loading branch information
Riny van Tiggelen
committed
Oct 23, 2024
1 parent
fae1d8a
commit 358dd14
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Classes/StructuredData/TypoScriptStructuredDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Build PHP (12, php8.2, false)
Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php GitHub Actions / Build PHP (13, php8.2, false)
Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php GitHub Actions / Build PHP (12, php8.2, false)
Check failure on line 37 in Classes/StructuredData/TypoScriptStructuredDataProvider.php GitHub Actions / Build PHP (13, php8.2, false)
|
||
} | ||
$key = in_array($key, ['type', 'context']) ? '@' . $key : $key; | ||
|
||
$item[$key] = $value; | ||
} | ||
$data[] = $item; | ||
} | ||
} | ||
|
||
return (array)$data; | ||
} | ||
|
||
protected function getTyposcriptFrontendController(): TypoScriptFrontendController | ||
{ | ||
return $GLOBALS['TSFE']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters