webdevops / TYPO3-metaseo

TYPO3 MetaSEO Extension
https://typo3.org/extensions/repository/view/metaseo
GNU General Public License v3.0
38 stars 24 forks source link

Add documentation: How to use the connector #494

Open thomaszbz opened 7 years ago

thomaszbz commented 7 years ago

MetaSEO version: 2.1.0/3.0.0

Add documentation: How to use the connector

Use one of the code examples from #477

topfender commented 7 years ago

How to user metaseo connector class to override page title and meta tags: Use connector class in a cacheable action: I would advice to create an extra action, so the plugin can be used in different cases. (e.g. form with extension data over get request).

ext_localconf.php

if (!defined('TYPO3_MODE')) {
    die('Access denied.');
}

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
    'VENDOR.' . $_EXTKEY,
    'PLUGIN',
    array(
        'MyController' => 'list, show, meta',

    ),
    // non-cacheable actions
    array(
        'MyController' => 'show',
    )
);

Classes/Controller/MyController.php

    /**
     * meta action
     *
     * @param \VENDOR\MyExt\Domain\Model\MyObj $myObj
     * @ignorevalidation $myObj
     * @return void
     */
    public function metaAction(\VENDOR\MyExt\Domain\Model\MyObj $myObj = null) {
        if($myObj) {
            if(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('metaseo')) {
                $title = trim($this->settings['title']['prefix']." ".$myObj->getTitle()." ".$this->settings['title']['suffix']);
                $connector = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Metaseo\\Metaseo\\Connector');
                $connector->setPageTitle($title);
                $connector->setMetaTag('title', $title);
                $connector->setOpenGraphTag('title',$title);
                if(trim($myObj->getMetakeywords()) != '') {
                    $connector->setMetaTag('keywords', $myObj->getMetakeywords());
                }
                if(trim($myObj->getMetadescription()) != '') {
                    $connector->setMetaTag('description', $myObj->getMetadescription());
                }
            }
        }
    }

The prefix and suffix settings could be set via flexform or typoscript constants if needed.

@thomaszbz please check and correct my spelling and grammar before you add this to the documentation :)

dsteinborn commented 7 years ago

@topfender The description should state that it has to be a cacheable action. It won't work with a non cacheable action.

topfender commented 7 years ago

@dsteinborn indeed, thanks.