The title tag and the meta description are extremely important for the search engines. It is not a problem for the normal standard pages in the TYPO3 tree and you can easily maintain the meta description and the title tag via the page properties. Unfortunately, this does not work automatically with self-programmed extensions and you have to do it manually. TYPO3 now has the title tag provider and the meta tag API for this. How to use it in your own extension, I show in this article.

 

Meta Tag API

In order to fill meta tags such as the description from our own TYPO3 extension, we first have to go into our controller and include the two namespaces that we need in this case.

use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Now we go to the respective action, where the data records are read from the database and made available. For example, if the meta description is to be set, then we add the following to the action.

$metaTagManager = GeneralUtility::makeInstance(MetaTagManagerRegistry::class)->getManagerForProperty('description');

If the meta description is already filled, it can sometimes happen that it is not automatically replaced. In this case we have to remove the standard description in our action with the following call.

$metaTagManager->removeProperty('description');

To fill the meta description now, just a single call is enough, which reads as follows.

$metaTagManager->addProperty('description', $descriptionFromData);

Instead of the $descriptionFromData variable, simply insert the variable or database call that is to be used as the meta description.

$metaTagManager = GeneralUtility::makeInstance(MetaTagManagerRegistry::class)->getManagerForProperty('og:title');

$metaTagManager->addProperty('og:title', 'Title here');

Title-Tag API

In order to set the title tag from your own TYPO3 extension, a little more work is currently required. First create a new PHP file in the "Classes/Controller" directory, e.g. TitleTag.php .

namespace Vendor\Extension\Controller;
   
 use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider;
   
 class TitleProvider extends AbstractPageTitleProvider
 {
   public function setTitle($title)
   {
     $this->title = $title;
   }
 }

The title for the title tag is passed in the above function.

In the next step, the configuration for our title tag provider must be made in the "ext_localconf.php" file in the main directory of the extension.

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(trim('
   config.pageTitleProviders {
     titletag {
       provider = Vendor\Extension\Controller\TitleProvider
       before = record
       after = altPageTitle
     }
   }
 '));

Now you can fill the title tag within the action in your controller with the following code, for example.

$titleTag = GeneralUtility::makeInstance(TitleProvider::class);
$titleTag->setBlogTitle('Here the title');

The standard title tag from TYPO3 Core is now automatically overwritten.

With these possibilities, it is much easier nowadays to fill the metas and the title tag from your own extensions.

Comments are disabled for this post.

0 comments