Display list of elements grouped by year and by month in TYPO3 Fluid

前端 未结 3 1803
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 04:49

I have a model where one field is a date. I want to display elements from that model, grouped by year and by month, like this:

== 2013 ==
=== April ===
* Ele         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-12 05:14

    A simple solution which have not been added here properly is this:

    For year and month grouping, add the fields as Transient to your model:

    ...
    /**
     * @var int
     * @transient
     */
    protected $year;
    
    
    /**
     * @var int
     * @transient
     */
    protected $month;
    /**
     * @return int
     */
    public function getYear()
    {
        if (!$this->year) {
            $this->year = $this->getDateTimeField()->format('Y');
        }
        return $this->year;
    }
    
    /**
     * @return int
     */
    public function getMonth()
    {
        if (!this->month) {
           $this->month = $this->getDateTimeField('n');
        }
        return $this->month;
    }
    

    Then in the template:

    
        
            ...
        
    
    

提交回复
热议问题