首页 > 解决方案 > TYPO3: tx_news 获取 sys_category sys_categories 用于 BE 列表中的自定义标题

问题描述

我扩展了 tx_news 以举办一些课程。有些课程针对不同的论点处理相同的主题(我选择为 sys_categories)。这意味着它们的标题是相同的,现在我试图通过在列表中包含所选类别来使列表更适合编辑器...

隐含自定义标题Configuration/TCA/Overrides/tx_news_domain_model_news.php

$GLOBALS['TCA']['tx_news_domain_model_news']['ctrl']['label_userFunc'] = 'Vendor\\NewsExt\\Userfuncs\\Tca->customTitle';

到目前为止的用户功能Classes/Userfuncs/Tca.php

<?php
namespace Vendor\NewsExt\Userfuncs;

use GeorgRinger\News\Domain\Model\News;

/**
 * Class Tca
 */
class Tca
{
    /**
     * Loads a custom title for the news list view
     *
     * @return void
     */
    public function customTitle(
        &$parameters,
        $parentObject
    ){
        $record = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord($parameters['table'], $parameters['row']['uid']);
        $newTitle = $record['title'];
        if($record['is_course']){
            $newTitle .= ' (' . $record['categories'] . ')' ;
        }
        $parameters['title'] = $newTitle;
    }
}

这显然给出了所选类别的数量......我没有包括我的任何尝试,因为它们没有任何结果......

标签: model-view-controllertypo3extbasetx-news

解决方案


您可以进行 mm 查询来解析分配的类别标题:

<?php
  namespace Vendor\NewsExt\Userfuncs;

  use GeorgRinger\News\Domain\Model\News;

  /**
   * Class Tca
   */
  class Tca
  {
    /**
     * Loads a custom title for the news list view
     *
     * @return void
     */
    public function customTitle(&$parameters, $parentObject)
    {
      # fetch all categories assigned to this news
      $result = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
        'sys_category.uid, sys_category.title',
        'sys_category',
        'sys_category_record_mm',
        $parameters['table'],
        'AND sys_category_record_mm.tablenames = "' . $parameters['table'] . '" ' .
        'AND sys_category_record_mm.fieldname = "categories" ' .
        'AND sys_category_record_mm.uid_foreign = ' . $parameters['row']['uid']
      );

      # walk the categories an get the title of them
      $categoriesLabels = [];
      foreach ($result->fetch_all(MYSQLI_ASSOC) as $category) {
        $categoriesLabels[] = $category['title'];
      }

      # if at least one category put them into the title
      if (!empty(array_filter($categoriesLabels))) {
        $record = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord($parameters['table'], $parameters['row']['uid']);
        $parameters['title'] = $record['title'] . ' ('. implode(', ', $categoriesLabels) .')';
      }
    }
  }

注意:此代码在 TYPO3 8.7.12 中测试


推荐阅读