How can I get the category object in Fluid of Typo3 Content Element Pictures?

萝らか妹 提交于 2019-12-04 14:26:49

Looks like this task is very tricky. The file object in the {files} array is of type \TYPO3\CMS\Core\Resource\FileReference where properties like uid, title or description are passed through from the original file object of type \TYPO3\CMS\Core\Resource\File. FileReference is actually implemented as a model but not file, so you can not extend it.

The only other way i see is to create a viewhelper that will get the categories with a native sql query and the CategoryRepository would automatically map the result to the Category model. Something like that:

<?php
namespace Vendor\Extension\ViewHelpers;
/**
 *
 * @version $Id$
 * @copyright Dimitri Lavrenuek <lavrenuek.de>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
 */
class GetFileCategoriesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
     * @var \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
     * @inject
     */
    protected $categoryRepository;

    /**
     * @param int $uid 
     * @return array
     */
    public function render ($uid) {
        $query = $this->categoryRepository->createQuery();
        $sql = "SELECT sys_category.* FROM sys_category
            INNER JOIN sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid AND sys_category_record_mm.fieldname = 'categories' AND sys_category_record_mm.tablenames = 'sys_file_metadata'
            INNER JOIN sys_file_metadata ON  sys_category_record_mm.uid_foreign = sys_file_metadata.uid
            WHERE sys_file_metadata.file = '" . (int)$uid . "'
            AND sys_category.deleted = 0
            ORDER BY sys_category_record_mm.sorting_foreign ASC";
        return $query->statement($sql)->execute();
    }
}

I have not tested the actual code, only the sql query, but this should work. Also i hope that you know how to include viewhelpers in your fluid template, if not I will provide an example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!