-->

How can I get the category object in Fluid of Typo

2019-04-12 08:20发布

问题:

after a long time of Google, I can get the categories of FAL Objects in Typo3 7.6 Fluid. But I can only return a String. I want to get an object like {data}.

What I do: TypoScript

lib.category = CONTENT
lib.category {
    table=sys_category
    wrap=|
    select {
        pidInList = root,0,1 
        recursive = 99
        max=10
        selectFields=sys_category.title,sys_category.uid
        join = sys_category_record_mm on sys_category_record_mm.uid_local = sys_category.uid join sys_file_metadata on sys_file_metadata.uid = sys_category_record_mm.uid_foreign join sys_file_reference on sys_file_reference.uid_local = sys_file_metadata.file
        where.field = fuid
        where.wrap = sys_file_reference.uid=|
    }
    renderObj=COA
    renderObj {
        1=TEXT
        1.field = uid
        2=TEXT
        2.field = title
    }
}

In Fluid I have:

<f:for each="{files}" as="file">
    <p>
    - {file.uid}<br />
    - <f:cObject typoscriptObjectPath="lib.category" data="{fuid:file.uid}" />
    </p>
</f:for>

On the webpage it prints:

  • 88
  • 3Black7Small

  • 89

  • 2Blue7Big

  • 90

  • 1Red

But I think an object in Fluid is better, so I can use f:for each etc. But I don't know how I can return it.

Can anybody help me?

回答1:

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.