访问fluid_styled_content元素中的页面属性

时间:2017-05-25 22:36:23

标签: typo3 fluid typo3-7.6.x

我正在尝试扩展fluid_styled_content元素“Menu”。在我的部分内部(例如typo3conf/ext/my_theme/Resources/Private/Templates/Content/Partials/Menu/Type-1.html我需要访问菜单CE所在页面的页面属性。如何存档?{data}仅包含内容元素的数据。

5 个答案:

答案 0 :(得分:1)

{data.pid}中你有页面的uid 您可以使用viewhelper来获取完整的页面记录(在ext:vhs中有一个viewhelper来获取任何类型的记录)。
或者您可以使用<f:cObject>和一些typoscript来访问单个值。

答案 1 :(得分:1)

使用\TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor

我现在无法检查代码,但您可以将此作为起点:

tt_content.menu.dataProcessing {
  30 = \TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
  30 {
    table = pages
    where.dataWrap = uid = {TSFE:id}
    as = page
  }
}

之后,您可以通过{page.0.property}访问当前页面的属性。

使用这种方法每个菜单内容对象只有一个查询,而大多数视图帮助程序解决方案往往会增加发出的数据库查询的数量。

答案 2 :(得分:1)

@undko:DatabaseQueryProcessor是完美的提示。但是你的代码片段有两个我必须解决的问题: - TypoScript代码需要pidInList才能工作 - 在流体模板中缺少datapageproperties.0.data.myproperty

这是我的最终代码,对我来说很合适:

tt_content.menu.dataProcessing {
  30 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
  30 {
    table = pages
    where.dataWrap = uid = {TSFE:id}
    pidInList = 1
    as = pageproperties
  }
}

在Fluid模板中,我使用{pageproperties.0.data.tx_mytheme_fieldname}

答案 3 :(得分:0)

我不知道确切原因,但我无法使用建议的解决方案访问页面属性。我使用的是 Typo3 10.4.9。

我找到了一个替代解决方案:

tt_content.menu dataProcessing {
   30 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
   30 {
      table = pages
      pidInList = 0
      recursive = 99
      uidInList = this
      as = pageproperties
   }
}

也许这会帮助其他人。

答案 4 :(得分:0)

更残酷的做法;在任何地方访问页面属性,例如在自定义内容元素中。在站点包 Classes/ViewHelper/GetPagePropertiesViewHelper.php 中创建:

<?php namespace Xxx\Sitepackage\ViewHelpers;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

    class GetPagePropertiesViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper
    {

public function initializeArguments()
{
    $this->registerArgument('pid', 'int', 'The page uid to get the pageproperties from', true, 1);
    $this->registerArgument('property', 'string', 'A specific page property to be returned', false, null);
}

public function render()
{
    $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
    $queryBuilder = $connectionPool->getQueryBuilderForTable('pages');

    $pageProperties = [];

    $statement = $queryBuilder
        ->select('*')
        ->from('pages')
        ->where(
            $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($this->arguments['pid'], \PDO::PARAM_INT))
        )
        ->execute();

    while ($row = $statement->fetch()) {
        $pageProperties[] = $row;
    }

    if ($property) {
        return $pageProperties[0][$property];
    }

    return $pageProperties[0];
}
}

在模板或部分中的使用:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
  xmlns:c="http://typo3.org/ns/Xxx/Sitepackage/ViewHelpers"
  data-namespace-typo3-fluid="true">

<c:getPageProperties pid="{data.pid}" property="description"/>
<f:debug><c:getPageProperties pid="{data.pid}"/></f:debug>