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

[TASK] Meta Tag Refactoring #837

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions Classes/Seo/NewsPageTitleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace GeorgRinger\News\Seo;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider;

/**
* This class will take care of the seo title that can be set in the backend
* $titleProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\GeorgRinger\News\Seo\NewsPageTitleProvider::class);
* $titleProvider->setTitle('Title');
*/
class NewsPageTitleProvider extends AbstractPageTitleProvider
{
public function setTitle($title)
{
$this->title = (string)$title;
}
}
47 changes: 12 additions & 35 deletions Classes/ViewHelpers/MetaTagViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,40 @@
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;

/**
* ViewHelper to render meta tags
*
* # Example: Basic Example: News title as og:title meta tag
* # MetaTagManager from the core decide if "property" or "name" is used
* <code>
* <n:metaTag property="og:title" content="{newsItem.title}" />
* </code>
* <output>
* <meta property="og:title" content="TYPO3 is awesome" />
* </output>
*
* # Example: Force the attribute "name"
* <code>
* <n:metaTag name="keywords" content="{newsItem.keywords}" />
* <n:metaTag property="keywords" content="{newsItem.keywords}" />
* </code>
* <output>
* <meta name="keywords" content="news 1, news 2" />
* </output>
*/
class MetaTagViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
class MetaTagViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{

/**
* @var string
*/
protected $tagName = 'meta';

/**
* Arguments initialization
*
*/
public function initializeArguments()
{
$this->registerTagAttribute('property', 'string', 'Property of meta tag');
$this->registerTagAttribute('name', 'string', 'Content of meta tag using the name attribute');
$this->registerTagAttribute('content', 'string', 'Content of meta tag');
$this->registerArgument('property', 'string', 'Property of meta tag', true);
$this->registerArgument('content', 'string', 'Content of meta tag');
$this->registerArgument('useCurrentDomain', 'boolean', 'Use current domain', false, false);
$this->registerArgument('forceAbsoluteUrl', 'boolean', 'Force absolut domain', false, false);
}

/**
Expand All @@ -67,29 +60,13 @@ public function render()
return;
}

$useCurrentDomain = $this->arguments['useCurrentDomain'];
$forceAbsoluteUrl = $this->arguments['forceAbsoluteUrl'];

// set current domain
if ($useCurrentDomain) {
$this->tag->addAttribute('content', GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
}

// prepend current domain
if ($forceAbsoluteUrl) {
$parsedPath = parse_url($this->arguments['content']);
if (is_array($parsedPath) && !isset($parsedPath['host'])) {
$this->tag->addAttribute('content',
rtrim(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'), '/')
. '/'
. ltrim($this->arguments['content'], '/')
);
}
}
$metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
$manager = $metaTagManagerRegistry->getManagerForProperty($this->arguments['property']);

if ($useCurrentDomain || (isset($this->arguments['content']) && !empty($this->arguments['content']))) {
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addMetaTag($this->tag->render());
if ($this->arguments['useCurrentDomain']) {
$manager->addProperty($this->arguments['property'], GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
}else if(isset($this->arguments['content']) && !empty($this->arguments['content'])){
$manager->addProperty($this->arguments['property'], $this->arguments['content']);
}
}
}
5 changes: 3 additions & 2 deletions Classes/ViewHelpers/TitleTagViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
Expand Down Expand Up @@ -52,8 +53,8 @@ public static function renderStatic(

$content = trim($renderChildrenClosure());
if (!empty($content)) {
$GLOBALS['TSFE']->altPageTitle = $content;
$GLOBALS['TSFE']->indexedDocTitle = $content;
$titleProvider = GeneralUtility::makeInstance(\GeorgRinger\News\Seo\NewsPageTitleProvider::class);
$titleProvider->setTitle($content);
}
}
}
10 changes: 10 additions & 0 deletions Configuration/TypoScript/setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ module.tx_news {
settings.list.paginate.itemsPerPage = 25
}

# ==============================================
# Page Title Provider for EXT:news
# ==============================================
config.pageTitleProviders{
newsPageTitle {
provider = GeorgRinger\News\Seo\NewsPageTitleProvider
before = record, seo
}
}

# ==============================================
# Persistence object mapping configuration
# ==============================================
Expand Down
34 changes: 20 additions & 14 deletions Resources/Private/Partials/Detail/Opengraph.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@
data-namespace-typo3-fluid="true">

<f:if condition="{newsItem.alternativeTitle}">
<f:then><n:metaTag property="og:title" content="{newsItem.alternativeTitle}" /></f:then>
<f:else><n:metaTag property="og:title" content="{newsItem.title}" /></f:else>
<f:then>
<n:metaTag property="og:title" content="{newsItem.alternativeTitle}" />
<n:metaTag property="twitter:title" content="{newsItem.alternativeTitle}" />
</f:then>
<f:else>
<n:metaTag property="og:title" content="{newsItem.title}" />
<n:metaTag property="twitter:title" content="{newsItem.title}" />
</f:else>
</f:if>
<n:metaTag property="og:type" content="{settings.opengraph.type}" />
<n:metaTag property="og:url" content="" useCurrentDomain="1" />
<n:metaTag property="og:site_name" content="{settings.opengraph.site_name}" />
<f:if condition="{newsItem.firstPreview}">
<n:metaTag
property="og:image"
content="{f:uri.image(src:'{newsItem.firstPreview.uid}', treatIdAsReference:1, maxWidth:'500')}"
forceAbsoluteUrl="1" />
<n:metaTag property="og:image" content="{f:uri.image(src:'{newsItem.firstPreview.uid}', treatIdAsReference:1, maxWidth:'500', absolute:1)}" />
<n:metaTag property="og:image:width" content="{n:imageSize(property:'width')}" />
<n:metaTag property="og:image:height" content="{n:imageSize(property:'height')}" />
<n:metaTag property="twitter:image" content="{f:uri.image(src:'{newsItem.firstPreview.uid}', treatIdAsReference:1, maxWidth:'500', absolute:1)}" />
</f:if>

<f:if condition="{newsItem.description}">
<f:then>
<n:metaTag name="description" content="{newsItem.description}" />
<n:metaTag property="description" content="{newsItem.description}" />
<n:metaTag property="og:description" content="{newsItem.description}" />
<n:metaTag property="twitter:description" content="{newsItem.description}" />
</f:then>
<f:else>
<n:metaTag name="description" content="{newsItem.teaser -> f:format.stripTags()}" />
<n:metaTag property="description" content="{newsItem.teaser -> f:format.stripTags()}" />
<n:metaTag property="og:description" content="{newsItem.teaser -> f:format.stripTags()}" />
<n:metaTag property="twitter:description" content="{newsItem.teaser -> f:format.stripTags()}" />
</f:else>
</f:if>
<n:metaTag name="keywords" content="{newsItem.keywords}" />
<n:metaTag property="keywords" content="{newsItem.keywords}" />
<n:metaTag property="fb:admins" content="{settings.opengraph.admins}" />
<n:metaTag property="og:email" content="{settings.opengraph.email}" />
<n:metaTag property="og:phone_number" content="{settings.opengraph.phone_number}" />
Expand All @@ -41,9 +47,9 @@
<n:metaTag property="og:region" content="{settings.opengraph.region}" />
<n:metaTag property="og:postal-code" content="{settings.opengraph.postal-code}" />
<n:metaTag property="og:country-name" content="{settings.opengraph.country-name}" />
<f:if condition="{settings.opengraph.twitter.site}">
<n:metaTag name="twitter:card" content="{settings.opengraph.twitter.card}" />
<n:metaTag name="twitter:site" content="{settings.opengraph.twitter.site}" />
<n:metaTag name="twitter:creator" content="{settings.opengraph.twitter.creator}" />
</f:if>

<n:metaTag property="twitter:card" content="{settings.opengraph.twitter.card}" />
<n:metaTag property="twitter:site" content="{settings.opengraph.twitter.site}" />
<n:metaTag property="twitter:creator" content="{settings.opengraph.twitter.creator}" />

</html>