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

[FEATURE] Add support for config.htmlTag & page.bodyTagAdd #802

Merged
merged 1 commit into from
Jan 23, 2025
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
36 changes: 36 additions & 0 deletions Classes/Seo/MetaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent;

use function htmlspecialchars;

class MetaHandler
{
public function __construct(
Expand Down Expand Up @@ -69,6 +72,26 @@ public function process(ServerRequestInterface $request, TypoScriptFrontendContr
$content['seo']['link'] = $seoLinks;
}

/**
* @var SiteLanguage $language
*/
$language = $request->getAttribute('language');

$rawHtmlTagAttrs = $controller->config['config']['htmlTag.']['attributes.'] ?? [];
$htmlTagAttrs = $this->normalizeAttr($rawHtmlTagAttrs);

$rawBodyTagAttrs = GeneralUtility::get_tag_attributes(trim($request->getAttribute('frontend.typoscript')->getSetupArray()['page.']['bodyTagAdd'] ?? ''));
$bodyTagAttrs = $this->normalizeAttr($rawBodyTagAttrs);

$content['seo']['htmlAttrs'] = array_merge([
'lang' => $language->getLocale()->getLanguageCode(),
'dir' => $language->getLocale()->isRightToLeftLanguageDirection() ? 'rtl' : null,
], $htmlTagAttrs);

if ($bodyTagAttrs !== []) {
$content['seo']['bodyAttrs'] = $bodyTagAttrs;
}

return $content;
}

Expand Down Expand Up @@ -130,4 +153,17 @@ private function setMetaTag(string $type, string $name, string $content, array $
$manager = $this->metaTagRegistry->getManagerForProperty($name);
$manager->addProperty($name, $content, $subProperties, $replace, $type);
}

/**
* @codeCoverageIgnore
*/
private function normalizeAttr(array $rawHtmlAttrs): array
{
$htmlAttrs = [];

foreach ($rawHtmlAttrs as $attr => $value) {
$htmlAttrs[htmlspecialchars((string)$attr)] = htmlspecialchars((string)$value);
}
return $htmlAttrs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\EventDispatcher\ListenerProvider;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\TypoScript\AST\Node\RootNode;
use TYPO3\CMS\Core\TypoScript\FrontendTypoScript;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand Down Expand Up @@ -128,6 +132,19 @@ public function testModifiedPageTitle(): void

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(Argument::is('headless'))->willReturn(new Headless(HeadlessMode::FULL));
$request->getAttribute(Argument::is('language'))->willReturn(new SiteLanguage(
0,
'en',
new Uri('/en'),
[]
));

$frontendTyposcript = new FrontendTypoScript(new RootNode(), [], [], []);
$frontendTyposcript->setSetupTree(new RootNode());
$frontendTyposcript->setSetupArray([]);

$request->getAttribute(Argument::is('frontend.typoscript'))->willReturn($frontendTyposcript);

$controller = $this->prophesize(TypoScriptFrontendController::class);
$controller->content = json_encode(['meta' => ['title' => 'test before event'], 'seo' => ['title' => 'test before event']]);
$controller->cObj = $this->prophesize(ContentObjectRenderer::class)->reveal();
Expand All @@ -137,7 +154,7 @@ public function testModifiedPageTitle(): void

$listener($event);

self::assertSame(json_encode(['meta' => ['title' => 'test before event'], 'seo' => ['title' => 'Modified title via PageTitleProviderManager', 'meta' => []]]), $event->getController()->content);
self::assertSame(json_encode(['meta' => ['title' => 'test before event'], 'seo' => ['title' => 'Modified title via PageTitleProviderManager', 'meta' => [], 'htmlAttrs' => ['lang' => 'en', 'dir' => null]]]), $event->getController()->content);
}

public function testHreflangs(): void
Expand All @@ -158,6 +175,19 @@ public function testHreflangs(): void

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(Argument::is('headless'))->willReturn(new Headless(HeadlessMode::FULL));
$request->getAttribute(Argument::is('language'))->willReturn(new SiteLanguage(
0,
'en',
new Uri('/en'),
[]
));

$frontendTyposcript = new FrontendTypoScript(new RootNode(), [], [], []);
$frontendTyposcript->setSetupTree(new RootNode());
$frontendTyposcript->setSetupArray([]);

$request->getAttribute(Argument::is('frontend.typoscript'))->willReturn($frontendTyposcript);

$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$controller = $this->prophesize(TypoScriptFrontendController::class);
$controller->content = json_encode(['meta' => ['title' => 'test before event'], 'seo' => ['title' => 'test before event']]);
Expand All @@ -184,6 +214,6 @@ public function handle(): void {}
['rel' => 'alternate', 'hreflang' => 'pl-PL', 'href' => 'https://example.com/pl'],
['rel' => 'alternate', 'hreflang' => 'en-US', 'href' => 'https://example.com/us'],
['rel' => 'alternate', 'hreflang' => 'en-UK', 'href' => 'https://example.com/uk'],
]]]), $event->getController()->content);
], 'htmlAttrs' => ['lang' => 'en', 'dir' => null]]]), $event->getController()->content);
}
}
Loading