-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Introduce support of SEO related APIs from TYPO3 Core
Patch introduces proper support of SEO related settings & APIs in TYPO3 core: Patch adds ability to use: - Canonical API - MetaTag API - Page title API Respects `page.meta` TypoScript definitions.
- Loading branch information
1 parent
2eadaa7
commit 7c6a7fb
Showing
13 changed files
with
708 additions
and
18 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
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,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Seo; | ||
|
||
use FriendsOfTYPO3\Headless\Utility\HeadlessMode; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use TYPO3\CMS\Core\Domain\Page; | ||
use TYPO3\CMS\Core\Domain\Repository\PageRepository; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent; | ||
|
||
use function htmlspecialchars; | ||
use function json_encode; | ||
|
||
/** | ||
* Overridden core version with headless implementation | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
class CanonicalGenerator extends \TYPO3\CMS\Seo\Canonical\CanonicalGenerator | ||
{ | ||
protected TypoScriptFrontendController $typoScriptFrontendController; | ||
protected PageRepository $pageRepository; | ||
protected EventDispatcherInterface $eventDispatcher; | ||
|
||
public function handle(array &$params): string | ||
{ | ||
if ($this->typoScriptFrontendController->config['config']['disableCanonical'] ?? false) { | ||
return ''; | ||
} | ||
|
||
$event = new ModifyUrlForCanonicalTagEvent('', $params['request'], new Page($params['page'])); | ||
$event = $this->eventDispatcher->dispatch($event); | ||
$href = $event->getUrl(); | ||
|
||
if (empty($href) && (int)$this->typoScriptFrontendController->page['no_index'] === 1) { | ||
return ''; | ||
} | ||
|
||
if (empty($href)) { | ||
// 1) Check if page has canonical URL set | ||
$href = $this->checkForCanonicalLink(); | ||
} | ||
if (empty($href)) { | ||
// 2) Check if page show content from other page | ||
$href = $this->checkContentFromPid(); | ||
} | ||
if (empty($href)) { | ||
// 3) Fallback, create canonical URL | ||
$href = $this->checkDefaultCanonical(); | ||
} | ||
|
||
if (!empty($href)) { | ||
if (GeneralUtility::makeInstance(HeadlessMode::class)->withRequest($params['request'])->isEnabled()) { | ||
$canonical = [ | ||
'href' => htmlspecialchars($href), | ||
'rel' => 'canonical', | ||
]; | ||
|
||
$params['_seoLinks'][] = $canonical; | ||
$canonical = json_encode($canonical); | ||
} else { | ||
$canonical = '<link ' . GeneralUtility::implodeAttributes([ | ||
'rel' => 'canonical', | ||
'href' => $href, | ||
], true) . '/>' . LF; | ||
$this->typoScriptFrontendController->additionalHeaderData[] = $canonical; | ||
} | ||
|
||
return $canonical; | ||
} | ||
return ''; | ||
} | ||
} |
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,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Seo\MetaTag; | ||
|
||
use FriendsOfTYPO3\Headless\Utility\HeadlessMode; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
use function array_merge; | ||
use function json_decode; | ||
use function json_encode; | ||
|
||
/** | ||
* Overridden core version with headless implementation | ||
*/ | ||
abstract class AbstractMetaTagManager extends \TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager | ||
{ | ||
public function renderAllProperties(): string | ||
{ | ||
if (GeneralUtility::makeInstance(HeadlessMode::class)->withRequest($GLOBALS['TYPO3_REQUEST'])->isEnabled()) { | ||
return $this->renderAllHeadlessProperties(); | ||
} | ||
|
||
return parent::renderAllProperties(); | ||
} | ||
|
||
public function renderProperty(string $property): string | ||
{ | ||
if (GeneralUtility::makeInstance(HeadlessMode::class)->withRequest($GLOBALS['TYPO3_REQUEST'])->isEnabled()) { | ||
return $this->renderHeadlessProperty($property); | ||
} | ||
|
||
return parent::renderProperty($property); | ||
} | ||
|
||
/** | ||
* Render a meta tag for a specific property | ||
* | ||
* @param string $property Name of the property | ||
*/ | ||
public function renderHeadlessProperty(string $property): string | ||
{ | ||
$property = strtolower($property); | ||
$metaTags = []; | ||
|
||
$nameAttribute = $this->defaultNameAttribute; | ||
if (isset($this->handledProperties[$property]['nameAttribute']) | ||
&& !empty((string)$this->handledProperties[$property]['nameAttribute'])) { | ||
$nameAttribute = (string)$this->handledProperties[$property]['nameAttribute']; | ||
} | ||
|
||
$contentAttribute = $this->defaultContentAttribute; | ||
if (isset($this->handledProperties[$property]['contentAttribute']) | ||
&& !empty((string)$this->handledProperties[$property]['contentAttribute'])) { | ||
$contentAttribute = (string)$this->handledProperties[$property]['contentAttribute']; | ||
} | ||
|
||
if ($nameAttribute && $contentAttribute) { | ||
foreach ($this->getProperty($property) as $propertyItem) { | ||
$metaTags[] = [ | ||
htmlspecialchars($nameAttribute) => htmlspecialchars($property), | ||
htmlspecialchars($contentAttribute) => htmlspecialchars($propertyItem['content']), | ||
]; | ||
|
||
if (!count($propertyItem['subProperties'])) { | ||
continue; | ||
} | ||
foreach ($propertyItem['subProperties'] as $subProperty => $subPropertyItems) { | ||
foreach ($subPropertyItems as $subPropertyItem) { | ||
$metaTags[] = [ | ||
htmlspecialchars($nameAttribute) => htmlspecialchars($property . $this->subPropertySeparator . $subProperty), | ||
htmlspecialchars($contentAttribute) => htmlspecialchars((string)$subPropertyItem), | ||
]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return json_encode($metaTags); | ||
} | ||
|
||
/** | ||
* Render all registered properties of this manager | ||
*/ | ||
public function renderAllHeadlessProperties(): string | ||
{ | ||
$metatags = []; | ||
foreach (array_keys($this->properties) as $property) { | ||
$metatags = array_merge($metatags, json_decode($this->renderHeadlessProperty($property), true)); | ||
} | ||
|
||
return json_encode($metatags); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Seo\MetaTag; | ||
|
||
/** | ||
* Overridden core version with headless implementation | ||
*/ | ||
class EdgeMetaTagManager extends AbstractMetaTagManager | ||
{ | ||
/** | ||
* @var string[][] | ||
*/ | ||
protected $handledProperties = [ | ||
'x-ua-compatible' => ['nameAttribute' => 'http-equiv'], | ||
]; | ||
} |
Oops, something went wrong.