Typo3 带有系统类别的列表视图的模板

Typo3 带有系统类别的列表视图的模板,typo3,typo3-7.6.x,Typo3,Typo3 7.6.x,我有一个extbase扩展typo37,带有一个简单的联系人模型。 这个人有名字和照片 到目前为止,这是清楚的 但每个人都有一个类别,例如他工作的地方。办公室、市场等 因此,我使用系统类别,如下所述: 通过web>列表创建人员时,我可以指定类别 现在是模板化的问题: 如果我调试联系人,我会得到如下屏幕所示的输出 我想有一个列表,其中每个类别的标题显示其联系人 如何做到这一点? 此逻辑仅在模板中还是在控制器中 有人举过这样的例子吗 致意 Markus我想您所需要的逻辑可能与Fluid一起使用Gr

我有一个extbase扩展typo37,带有一个简单的联系人模型。 这个人有名字和照片

到目前为止,这是清楚的

但每个人都有一个类别,例如他工作的地方。办公室、市场等

因此,我使用系统类别,如下所述:

通过web>列表创建人员时,我可以指定类别

现在是模板化的问题: 如果我调试联系人,我会得到如下屏幕所示的输出

我想有一个列表,其中每个类别的标题显示其联系人

如何做到这一点? 此逻辑仅在模板中还是在控制器中

有人举过这样的例子吗

致意
Markus

我想您所需要的逻辑可能与Fluid一起使用GroupedFor ViewHelper和许多其他工具。因为一个人可以有多个类别,这将成为一个巨大的ViewHelper嵌套,所以即使可能,我也不建议使用Fluid。这种逻辑属于控制器、模型和存储库

解决这个逻辑有多种方法。下面是一个如何在控制器中实现这一点的示例

控制器:

/**
 * @var \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
 * @inject
 */
protected $categoryRespoitory = NULL;

/**
 * action list
 * @return void
 */
public function listAction()
{
    $allCategories = $this->categoryRespoitory->findAll();
    $categoriesWithContacts = [];
    /** @var \TYPO3\CMS\Extbase\Domain\Model\Category $category */
    foreach($allCategories as $category) {
        $contactsInCategory= $this->contactRepository->findByCategory($category);
        if($contactsInCategory->count()>0) {
            $categoriesWithContacts[] = [
                'category' => $category,
                'contacts' => $contactsInCategory
            ];
        }
    }
    $this->view->assignMultiple([
       'categoriesWithContacts' => $categoriesWithContacts
    ]);
}
注入CategoryResposition需要清除安装工具中的缓存或重新安装扩展

也许您需要在ContactRepository中使用此功能:

/**
 * @param \TYPO3\CMS\Extbase\Domain\Model\Category $category
 * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
 */
public function findByCategory(\TYPO3\CMS\Extbase\Domain\Model\Category $category) {
    $query = $this->createQuery();
    return $query->matching($query->contains('categories', $category))->execute();
}
然后在流体中,可以执行如下操作:

<f:for each="{categoriesWithContacts}" as="categoryWithContact">
    {categoryWithContact.category.title}
    <f:for each="{categoryWithContact.contacts}" as="contact">
        {contact.name}
    </f:for>
</f:for>