clickstorm / cs_seo

[clickstorm] SEO
Other
33 stars 49 forks source link

TYPO3 8.7.19 | Translated Meta Tags for tx_news get ignored/overwritten? #212

Closed econsor-ag closed 5 years ago

econsor-ag commented 5 years ago

my translated news article (english) shows the default meta tags (default lang is german ) instead of the translated ones.

this is the TSconfig on my Root Page:

pageconfig

this is the HTML structure of my translated article:

metatags

I have added the SEO Meta Tags for both the default and translated articles. The translated articles get shown correctly but the meta tags get overwritten or ignored?

Thank you in advance!

econsor-ag commented 5 years ago

I looked further into this problem and found inside the /cs_seo/Classes/UserFunc/HeaderData.php File the following function:

/**
 * @return bool|string meta tags, if available
 */
public function getMetaTags($content, $conf)
{
    // get table settings
    $tables = ConfigurationUtility::getPageTSconfig();

    if ($tables) {
        // get active table name und settings
        $tableSettings = $this->getCurrentTable($tables, $this->cObj);

        if ($tableSettings) {
            // get record
            $record = $this->getRecord($tableSettings);

            if ($record['_LOCALIZED_UID']) {
                $tableSettings['uid'] = $record['_LOCALIZED_UID'];
            }
            // db meta
            $meta = $this->getMetaProperties($tableSettings);

            // db fallback
            if (isset($tableSettings['fallback'])) {
                foreach ($tableSettings['fallback'] as $seoField => $fallbackField) {
                    if (empty($meta[$seoField]) && !empty($record[$fallbackField])) {
                        $meta[$seoField] = $record[$fallbackField];
                        if ($seoField == 'og_image' || $seoField == 'tw_image') {
                            $meta[$seoField] = [
                                'field' => $fallbackField,
                                'table' => $tableSettings['table'],
                                'uid_foreign' => $tableSettings['uid']
                            ];
                        }
                    }
                }
            }

            // render content
            $headerData = $this->renderContent($meta);

            return $headerData;
        }
    }

    return false;
}

The [if] Statement checks for $record['_LOCALIZED_UID'] but this field does never exists inside the record, where does this field come from?

Since the function uses the default news uid which in my example is 53 I tried setting $tableSettings['uid'] to 54 which is my english news article and it showed the correct meta tags.

econsor-ag commented 5 years ago

In case someone has the same problem as I do, I solved this problem by fetching the uid from the translated article with a querybuilder and overwriting the $tableSettings['uid']:

            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_news_domain_model_news');
            $queryUID = $tableSettings['uid'];
            $localizedUID = $queryBuilder
            ->select('uid')
            ->from('tx_news_domain_model_news')
            ->where($queryBuilder->expr()->eq('l10n_parent', $queryBuilder->createNamedParameter($queryUID, \PDO::PARAM_INT)))
            ->andWhere($queryBuilder->expr()->eq('sys_language_uid', $queryBuilder->createNamedParameter('1')))
            ->execute()->fetchAll();

            if ($GLOBALS['TSFE']->lang != 'de') {
                $tableSettings['uid'] = $localizedUID[0]['uid'];
            }