简体   繁体   中英

Read metatags from TypoScript in PHP classes

I am using TYPO3 9 and I want to implement a sharing functionality based on Fluid. Therefore I want to use the already provided metatags.

I wrote a ViewHelper class to make the metatags available to my Fluid template. Researching for possible solutions I found two things:

  1. Setting meta data via TSFE with $GLOBALS['TSFE']->page['description'] = $newDescription; . But I can't get the data with $GLOBALS['TSFE']->page['description']

  2. Using TYPO3's new MetaTagManager. But it seems that the Manager does not include the metatags set via TypoScript.

Is there another way to read all set metatags in PHP classes?

Accessing the Meta-Tags added via page.meta TypoScript is not possible during content rendering.

The reason is simple: the content is rendered before the meta tags are processed (see method generatePageContentWithHeader in TYPO3\\CMS\\Frontend\\Http\\RequestHandler

A simple workaround would be to process all related meta tags with your Extension / View Helper only.

A more sophisticated approach is a registry of some kind (eg a static property) that collects all changes you want to make to existing meta tags. The registry gets filled by your view helper.

Then you can call a userFunc that is executed after the TypoScript meta tags are collected:

page.jsFooterInline.323 = USER
page.jsFooterInline.323.userFunc = My\Extension\Hooks\MetaHook->processMeta

Within the processMeta you can now access the page meta data (eg page.meta.description like this:

$registry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
$metaDataArray = $registry->getManagerForProperty('description')->getProperty('description');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM