180690
180690

Reputation: 301

TYPO3 groupedFor from crdate

i just want to know how it is possible to get the month and the year from an extbase article in TYPO3. I have th crdate column. Is it possible to get the two values that i want from this field? I added this

Configuration/Extbase/Persistence/Classes.php

return [
  \Vendor\Name\Domain\Model\Item::class => [
    'tableName' => 'TABLE',
    'properties' => [
        'cruserId' => [
            'fieldName' => 'cruser_id'
        ],
        'year' => [
            'fieldName' => 'crdate'
        ],
    ],
  ],
];

Then in the Model

/**
 * @var string
 */
protected string $year = '';

/**
 * @return string
 */
public function getYear(): string
{
    return date('F', (int)$this->getCrdate());
}

/**
 * @param string $month
 */
public function setYear(string $year): void
{
    $this->year = $year;
}

I see the new value in the f:debug and the crdate is now empty. But the year now have the value from the crdate field. How can i get the right value?

EDIT:

I've tried following without success:

Model:

/**
 * @var \DateTime
 */
protected $crdate;

/**
 * Get year of crdate
 *
 * @return false|string
 */
public function getYearOfCrdate()
{
    return $this->crdate->format('Y');
}

/**
 * Get month of crdate
 *
 * @return false|string
 */
public function getMonthOfCrdate()
{
    return $this->crdate->format('m');
}

TCA:

'columns' => [
   'crdate' => [
        'label' => 'crdate',
        'config' => [
            'type' => 'input',
            'renderType' => 'inputDateTime',
            'eval' => 'datetime',
        ]
    ],

And removed my other code

Partial:

<f:groupedFor each="{articles}" as="item" groupBy="getYearOfCrdate" groupKey="year">
    <f:for each="{item}" as="article" key="title">
      Test
    </f:for>
</f:groupedFor>

Upvotes: 0

Views: 107

Answers (1)

Georg Ringer
Georg Ringer

Reputation: 7939

To make that work you need the following steps:

  1. Add a TCA configuration for crdate

Even though the field crdate is already defined in the section ctrl of the TCA the following code is additionally required because extbase needs this to correctly map the field. The ctrl information is used in the backend to fill this field when creating the record.

'crdate' => [
            'label' => 'crdate',
            'config' => [
                'type' => 'input',
                'renderType' => 'inputDateTime',
                'eval' => 'datetime',
            ]
        ],
  1. Instead of using a year property add the following in the model

    /**
     * @var \DateTime
     */
    protected $crdate;

    /**
     * Get year of crdate
     *
     * @return false|string
     */
    public function getYearOfCrdate()
    {
        return $this->crdate->format('Y');
    }
  1. use this getter in the groupedfor ViewHelper
<f:groupedFor each="{articles}" as="item" groupBy="yearOfCrdate" groupKey="year">

code is taken from https://github.com/georgringer/news/

Upvotes: 1

Related Questions