在前端按年份分组新闻(TYPO3,新闻系统)

group news by year in frontend (TYPO3, news system)

我正在使用TYPO3 9, News Plugin(新闻系统)

我正在寻找一种按年份对新闻文章进行分组的方法,如下所示:

2019
--------
article 1
article 2
article 3
--------
2018
--------
article 1
article 2
...

我找不到一个简单的解决方案,很难相信实现它的唯一方法是编辑 TYPO3 的源代码...

有人能帮忙吗?

------------------------ 编辑

Bernd Wilke 建议的固定和工作代码:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
      xmlns:n="http://typo3.org/ns/GeorgRinger/News/ViewHelpers"
      data-namespace-typo3-fluid="true">
<f:layout name="General" />
<!--
    =====================
        Templates/News/List.html
-->

<f:section name="content">
    <!--TYPO3SEARCH_end-->
    <f:if condition="{news}">
        <f:then>
            <f:variable name="oldYear">2010</f:variable>
            <f:for each="{news}" as="newsItem" iteration="iterator">
                <f:variable name="currentYear"><f:format.date format="%Y">{newsItem.datetime}</f:format.date></f:variable>
                <f:if condition="{oldYear} < {currentYear}">
                    <hr />
                    <h3>{currentYear}</h3>
                    <hr />
                </f:if>
                <f:render partial="List/Item" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
            </f:for>
        </f:then>
        <f:else>
            <div class="alert ">
                <f:translate key="list_nonewsfound" />
            </div>
        </f:else>
    </f:if>
    <!--TYPO3SEARCH_begin-->
</f:section>
</html>

不过,我把这个问题交给了 Georg Ringer,因为他的解决方案马上就奏效了。

像这样分组没有打字错误选项。

但你可以在 FLUID 中做到:
将新闻列表的模板文件复制到您的(站点)扩展名中,并根据您的需要进行修改:在组处理中构建。

<f:variable.set name="oldYear">0000</f:variabel.set>
<f:for each="{news}" as="newsItem" iteration="iterator">
    <f:variable.set name="currentYear">{newsItem.datetime->f:format.date("Y")}</f:varaible.set>
    <f:if condition="{oldYear} == {currentYear}">
        <hr />
        <h3>{currentYear}</h3>
        <hr />
        <f:variable.set name="oldYear">{currentYear}</f:variable.set>
    </f:if>
    <f:render partial="List/Item" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
</f:for>

您可能会使用 this answer or this answer 中提到的视图助手之一。

当然这可以通过使用 ViewHelper f:groupedFor 来实现,参见 official documentation

docs of the news extension中也有例子。所以这个例子应该像那样工作

<f:groupedFor each="{news}" as="groupedNews" groupBy="yearOfDatetime" groupKey="year">
        <div style="border:1px solid blue;padding:10px;margin:10px;">
                <h1>{year}</h1>
                <f:for each="{groupedNews}" as="newsItem">
                        <div style="border:1px solid pink;padding:5px;margin:5px;">
                                {newsItem.title}
                        </div>
                </f:for>
        </div>
</f:groupedFor>

但是我还想警告以下内容

Keep an eye on performance!

To be able to group the records, fluid will load every record itself and groups those afterwards. If you plan to group many records just for getting something like a count, maybe it is better to fire the query directly and don’t use fluid for that.

However if the result is on a cacheable page, the issue is only relevant on the first hit.