首页 > 解决方案 > TYPO3 10 : 如何在我的扩展中显示图像的元数据类别?

问题描述

我创建了一个 TYPO3 扩展,它允许选择多个图像。我通过文件元数据扩展激活了元数据。当我在流畅的模板循环中浏览图像文件时,我尝试显示元数据。这有效{file.properties.uid} {file.properties.categories},但对于类别,我得到一个数字。现在我想选择类别。

我用这个: https ://coding.musikinsnetz.de/typo3/fluid-viewhelpers/access-system-categories-in-content-elements-templates

<f: if condition = "{files}">
<f: for each = "{files}" as = "file">
            <f: for each = "{bg2yg: CategoriesOutput (recUid: data.uid)}" as = "category">
                <b style = 'color: blue'> <span class = "{category.title}"> CATEGORY: {category.title} </span> </b> <br />
            </ f: for>
</ f: for>
</ f: if>

这将显示主要类别,因为“data.uid”:{bg2yg: CategoriesOutput (recUid: data.uid)}

但是,我想要图像的类别,我对此进行了测试:

<f: for each = "{bg2yg: CategoriesOutput (recUid: file.properties.uid)}" as = "category">

没有成功 !你有想法吗 ?

最好的问候,布鲁诺

标签: templatestypo3categoriesfluidview-helpers

解决方案


感谢你们各自的帮助。我在这里编码:

    <?php
/**
 * This file is part of the "hexagonalgallery" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE file that was distributed with this source code.
 */

namespace BG2YG\Hexagonalgallery\ViewHelpers;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
 
/**
 * will return certain system categories (sys_category) data of an element
 * either as an array or as a string with certain parameters
 *
 * EXAMPLES:
 *
 * EMBEDDING IN TEMPLATE: {namespace my = YourVendor\YourExtension\ViewHelpers}
 *
 * call an array with all category data to be used in a loop, e.g. for an HTML tag for each files:
 *    <f:if condition="{file}">
 *        <f:for each="{my:FileCategoriesOutput(recUid: data.uid)}" as="category">
 *            <span class="{category.title}">{category.title}</span>
 *        </f:for>
 *    </f:if>
 *
 * call a “data-categories” attribute with the slug field of the categories, comma-separated (default):
 *     {my:FileCategoriesOutput(recUid: file.properties.uid, tableName: 'sys_file_metadata', fieldString: 'title', htmlAttr: 'data-categories')}
 *     output: ' data-categories="catx,caty"'
 *
 * call all categories as CSS classes (space as string separator, prefix 'cat-' for each files)
 *     {my:FileCategoriesOutput(recUid: file.properties.uid, tableName: 'sys_file_metadata', fieldString: 'title', stringSeparator: ' ', catPrefix: 'cat-')}
 *     output: 'cat-catx cat-caty'
 */
class FileCategoriesOutputViewHelper extends AbstractViewHelper
{
    protected $escapeOutput = false;

    public function initializeArguments()
    {
        $this->registerArgument('recUid', 'integer', 'record UID, e.g. of a content element', true);
        $this->registerArgument('tableName', 'string', 'optional: table of records you want the categories returned for (default: tt_content)', false, 'tt_content');
        $this->registerArgument('fieldString', 'string', 'optional: name of sys_categories table field – if given, the return value will be a string', false, null);
        $this->registerArgument('stringSeparator', 'string', 'optional: separator for string', false, ',');
        $this->registerArgument('htmlAttr', 'string', 'optional: wrap in attribute for HTML tag (in case of fieldString given)', false, null);
        $this->registerArgument('catPrefix', 'string', 'optional: prefix for each category (e.g. for CSS classes)', false, null);
    }
 
    /**
     * @return mixed
     */
    public function render()
    {
        $recUid = $this->arguments['recUid'];
        $tableName = $this->arguments['tableName'];
        $fieldString = $this->arguments['fieldString'];
        $stringSeparator = $this->arguments['stringSeparator'];
        $htmlAttr = $this->arguments['htmlAttr'];
        $catPrefix = $this->arguments['catPrefix'];
        define(DEBUG,false);

        /*
            SELECT uid_local FROM sys_file_reference WHERE uid = 152
        */        
        if (DEBUG) 
            echo "<b style='color:blue;'>\$recUid=".$recUid."</b><br />";
        /**
         * default query for sys_file_reference table
         * SQL : SELECT uid_local FROM sys_file_reference WHERE uid = $recUid
         */
        $queryBuilder0 = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
        $queryBuilder0->select('uid_local');
        $queryBuilder0->from('sys_file_reference');
        $queryBuilder0->where(
            $queryBuilder0->expr()->eq('sys_file_reference.uid', $queryBuilder0->createNamedParameter($recUid, \PDO::PARAM_INT)));
        $result_uid = $queryBuilder0->execute();
        $uid=$result_uid->fetch();
        $uid=$uid['uid_local'];
        
        if (DEBUG) 
            echo "<b style='color:blue;'>\$uid=".print_r($uid)."</b><br />";
        
        /**
         * default query for sys_category table
         */
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
        /**
         * select the fields that will be returned, use asterisk for all
         */
        $queryBuilder->select('sys_category.uid', 'sys_category.title', 'sys_category_record_mm.uid_foreign', 'sys_category_record_mm.tablenames');
        $queryBuilder->from('sys_category');
        $queryBuilder->join(
            'sys_category',
            'sys_category_record_mm',
            'sys_category_record_mm',
            $queryBuilder->expr()->eq('sys_category_record_mm.uid_local', $queryBuilder->quoteIdentifier('sys_category.uid'))
        );
        $queryBuilder->where(
            $queryBuilder->expr()->eq('sys_category_record_mm.uid_foreign', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)),
            $queryBuilder->expr()->like('sys_category_record_mm.tablenames', $queryBuilder->createNamedParameter($tableName))
        );

        $result = $queryBuilder->execute();
        $res = [];
        $returnString = '';
        $i = 1;
        while ($row = $result->fetch()) {
            $res[] = $row;
            if ($fieldString !== null) {
                if (isset($row[$fieldString])) {
                    $returnString .= ($i === 1) ? '' : $stringSeparator;
                    $returnString .= ($catPrefix !== null) ? $catPrefix : '';
                    $returnString .= $row[$fieldString];
                }
            }
            $i++;
        }
        if (DEBUG) {
            echo "\$returnString=" . $returnString . "<br />";
            echo "\$res=<b style='color:red;'>" . print_r($res) . "</b><br />";
        }
        if ($returnString !== '') {
            return ($htmlAttr !== null)
                ? ' ' . $htmlAttr . '="' . $returnString . '"'
                : $returnString;
        } elseif ($fieldString !== null) {
             return '';
        } else {
             return $res;
        }
    }
}

它有效。感谢您向我指出此代码在您看来是否是按照艺术规则编写的。

最好的问候,布鲁诺


推荐阅读