You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 tx_news后台列表自定义标题:关联sys_categories显示

Solution: Show Category Names in tx_news Backend List View

Got it, the core issue here is that $record['categories'] only stores a comma-separated string of category UIDs, not their actual display names. To fix this, we need to fetch the category titles from the sys_categories table using those UIDs and append them to the news title.

Here's the updated version of your customTitle method that achieves this:

<?php
namespace Vendor\NewsExt\Userfuncs;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;

/**
 * Class Tca
 */
class Tca {
    /**
     * Loads a custom title for the news list view with category names
     *
     * @param array $parameters
     * @param object $parentObject
     * @return void
     */
    public function customTitle( &$parameters, $parentObject ){
        $record = BackendUtility::getRecord($parameters['table'], $parameters['row']['uid']);
        $newTitle = $record['title'];

        if($record['is_course'] && !empty($record['categories'])){
            // Split the comma-separated category UIDs into a clean integer array
            $categoryUids = GeneralUtility::intExplode(',', $record['categories'], true);
            
            // Fetch category names from the sys_categories table
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_categories');
            $categoryNames = $queryBuilder
                ->select('title')
                ->from('sys_categories')
                ->where(
                    $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($categoryUids, ConnectionPool::PARAM_INT_ARRAY))
                )
                ->executeQuery()
                ->fetchFirstColumn();
            
            // Append category names to the title if any were found
            if(!empty($categoryNames)){
                $newTitle .= ' (' . implode(', ', $categoryNames) . ')';
            }
        }

        $parameters['title'] = $newTitle;
    }
}

Key Details:

  • Modern Database Access: We use TYPO3's ConnectionPool (the recommended approach for TYPO3 10 LTS and later) to query category data. For older TYPO3 versions (9 or below), you could replace this with $GLOBALS['TYPO3_DB'], but ConnectionPool is more secure and future-proof.
  • Safe UID Handling: GeneralUtility::intExplode ensures we only work with valid integers, avoiding potential SQL injection risks.
  • Clean Display: If multiple categories are selected, their names are joined with commas. We also skip adding parentheses if no categories are linked, keeping the list view tidy.

Optional Enhancements:

  • Localized Titles: If your categories are localized, you can modify the query to join with language fields and fetch the title matching the current backend language.
  • Sorted Names: Add ->orderBy('title ASC') to the query builder to display category names in alphabetical order.

内容的提问来源于stack exchange,提问作者webman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:05:36