如何从 TypoScript RECORDS 设置 TYPO3 7 元标签?

How to set TYPO3 7 metatags from TypoScript RECORDS?

我正在尝试用 TypoScript RECORDS object.

覆盖 TYPO3 7.5 中的 page.meta.og:title

不幸的是,以下 TypoScript 片段似乎不起作用:

[globalVar = GP:tx_myext_pi1|article > 0]

    temp.newsTitle = RECORDS
    temp.newsTitle {
        dontCheckPid = 1
        tables = tx_myext_domain_model_article
        source.data = GP:tx_myext_pi1|article
        source.intval = 1
        conf.tx_myext_domain_model_article = TEXT
        conf.tx_myext_domain_model_article {
            stdWrap.field = title
            stdWrap.wrap = |
        }
    }

    # Overrides the template pageTitle
    page.10.variables.pageTitle >
    page.10.variables.pageTitle < temp.newsTitle

    # Overrides the meta og:title   
    page.meta.og:title >
    page.meta.og:title < temp.newsTitle

[global]

我得到:

<meta name="og:title" content="RECORDS">

虽然覆盖页面标题对我有用。

是否有任何方法可以使用 TypoScript 实现此目的?

我很快就提出了我的问题 :)

可以通过寄存器来实现,举例如下:

[globalVar = GP:tx_myext_pi1|article > 0]

    page.9 = LOAD_REGISTER
    page.9 {
        newsTitle.cObject = RECORDS
        newsTitle.cObject {
            dontCheckPid = 1
            tables = tx_myext_domain_model_article
            source.data = GP:tx_myext_pi1|article
            source.intval = 1
            conf.tx_myext_domain_model_article = TEXT
            conf.tx_myext_domain_model_article {
                stdWrap.field = title
                stdWrap.wrap = |
            }
        }
    }

    page.10.variables.pageTitle >
    page.10.variables.pageTitle = TEXT
    page.10.variables.pageTitle {
        data = register:newsTitle
    }

    page.meta.og:title >
    page.meta.og:title {
        attribute = property
        override.data = register:newsTitle
    }

[global]

虽然它是您的分机,但是是什么阻止您直接将它添加到操作中,而不是使用一些奇怪的 TS 进行操作? ;)

public function showAction($article) {
    $ogTitle = trim(htmlentities($article->getTitle()));
    $GLOBALS['TSFE']->getPageRenderer()->addMetaTag('<meta name="og:title" content="' . $ogTitle . '">');

    // ... rest of action
}

另请查看 Georg Ringer 的 News 扩展,了解他如何使用 metaTagViewHeplper(实际上他也在做同样的事情,但在 VH 中)- 您可以使用它来收集其他 og 视图上的标签,例如 og:image 和其他标签。

编辑:

(有关防止重复条目的更多信息)

请记住,重复的 meta 组合 这不是一个错误,它是一个功能 根据 OGP Arrays spec ;) 事实上你不能声明两个相同的 TypoScript 元标记是一个错误,原因是什么? TypoScript 不是一种编程语言,它只是配置 table(严格来说是数组)。正如我们所知,在关联数组的 PHP 中,后面的键会覆盖前面的键。当我们讨论 og:* 元数据时,我们需要记住,有时它们会在每页重复出现,这是完全有效的,即:og:image.

作为一名程序员,您在行动中拥有比在 TS 中更多的权力,即使您使用的是一些 即用型 ext,它填充了 og:title 来自pages 记录,没有什么能阻止你用...丢弃它在 TS 中的简单技巧,在你的 TS 中添加一个条件:

[globalVar = GP:tx_myext_pi1|article > 0]
  page.meta.og:title >
[end]

然后确保将其添加到 showAction 中,如开头所示。

最后,这样您就不需要从 TS 站点为每个具有单一视图的模型进行昂贵的查找(相信我,我知道这意味着什么)

顺便说一句,我同意应该有可靠的 API 来解决这个问题,但是我为我的一个项目写了一些 ext 来处理这些事情,那是几个小时而不是几天的问题,如果我能找到的话它,我会发布到 TER。

RECORDS 仅适用于 cObject。 https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Records/Index.html

[globalVar = GP:tx_myext_pi1|article > 0]
    page.meta{
        og:title {
        attribute = property
            stdWrap.cObject = RECORDS
            stdWrap.cObject {
                source = {GP:tx_myext_pi1|article}
                source.insertData = 1
                tables = tx_myext_domain_model_article
                conf.tx_myext_domain_model_article= TEXT
                conf.tx_myext_domain_model_article.field = title
            }
        }
[global]