Extbase: handle images in model

If a model needs an image, it seems to be a good idea to do this via filereference. First, there is the definition of what we may call $image in our model:

/**
 * image
 *
 * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
 */
protected $image = NULL;

/**
 * Returns the image
 *
 * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 */
public function getImage() {
    return $this->image;
}

/**
 * Sets the image
 *
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 * @return void
 */
public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
    $this->image = $image;
}

In the database, the column “image” is an integer, length 11, unsigned. To make the image display in the view, all we have to do is the following:

<f:if condition="{item.image}"><f:image src="{item.image.uid}" treatidasreference="1"></f:image></f:if>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.