Building a TYPO3 extension in Extbase/Fluid

Add virtual properties to your domain model

You might want to add some dynamic, virtual properties to your domain model that are not reflected by a specific field in your database. In my example (a simple calendar extension) there are two date and two time properties/database fields: start and end in each case. In my view/template I only want to display some of these fields under certain conditions that I found to complicated to check in a Fluid template.

The idea was:

I let my model return an array of flags that I can easily check in my Fluid template. Here’s what I’ve done:

File paths are relative to the root of your extension /typo3conf/ext/your_extension/

/Classes/Domain/Model/YourModel.php

/**
 * virtual property dateDisplayFlags
 * contains flags which date fields are to be displayed
 *
 * @var array
 */

protected $dateDisplayFlags = '';

/**
 * returns an array of flags which date and time fields should be shown
 * the flags are:
 *              date => oneDay|datePeriod
 *              dateYear => oneYear|yearSpan
 *              timeStart => 1|0
 *              timeEnd => 1|0
 *
 * if dateEnd is smaller than dateStart, the method returns date => oneDay
 * if timeEnd is less that 10 minutes after timeStart, the method returns timeEnd => 0
 *
 * @return array $dateDisplayFlags
 */

public function getDateDisplayFlags() {
        $this->dateDisplayFlags['timeStart'] = 0;
        $this->dateDisplayFlags['timeEnd'] = 0;
        $this->dateDisplayFlags['dateYear'] = 'oneYear';
        if($this->dateStart >= $this->dateEnd) {
                $this->dateDisplayFlags['date'] = 'oneDay';
                if($this->timeStart) {
                        $timeDiff = (int)$this->timeEnd-(int)$this->timeStart;
                        $this->dateDisplayFlags['timeStart'] = $timeDiff;
                        $this->dateDisplayFlags['timeEnd'] = (!$this->timeEnd OR $timeDiff < 600) ? 0 : 1;
                }
        } else {
                $this->dateDisplayFlags['date'] = 'datePeriod';
                $yearStart = $this->dateStart->format('Y');
                $yearEnd = $this->dateEnd->format('Y');
                if($yearStart == $yearEnd) {
                        $this->dateDisplayFlags['dateYear'] = 'yearSpan';
                }
        }
        return $this->dateDisplayFlags;
}
        return $this->dateDisplayFlags;
}

$this->dateStart, $this->dateEnd, $this->timeStart, and $this->timeEnd are the model properties that contain the values of the database entry.
You can also see how to format an instance of TYPO3 Datetime object (for the formatting string see PHP function “date”):

$this->dateObj->format('Y-m-d')

(of course, TYPO3 has its own Datetime object and does not use some boring standard format like e.g. an easily usable timestamp or a string that you can convert to a timestamp using a standard PHP function)

Fluid Template

replace event by the name of your view variable

<f:if condition="{event.dateDisplayFlags.timeStart}">
        <p class="time">{event.timeStart -> f:format.date(format: 'H:i')}</p>
</f:if>
        <p class="time">{event.timeStart -> f:format.date(format: 'H:i')}</p>
</f:if>