【问题标题】:TYPO3: tx_news get sys_category sys_categories for custom title in BE listTYPO3: tx_news 获取 sys_category sys_categories 用于 BE 列表中的自定义标题
【发布时间】:2018-05-30 06:03:39
【问题描述】:

我扩展了 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;
    }
}

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

【问题讨论】:

  • 调试时$record['is_course'] 会显示什么?是否达到预期值?
  • @David,确实如此,它是一个二进制字段,将新闻标记为课程,这样我就可以缩小课程的设置范围,实际上在我的列表中只显示“课程”(1 ) 添加到标题
  • 那么问题解决了吗?
  • @David 绝对不是...而不是$record['categories'] 给出的类别数量,我需要显示 sys_categories 标题!到目前为止我编写的代码功能齐全,这不是问题

标签: model-view-controller typo3 extbase tx-news


【解决方案1】:

您可以进行 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 中测试

【讨论】:

  • # walk the categories an get the title of them 产生错误:array_map(): Argument #2 should be an array
  • 有效!如果没有人使用新闻扩展本身的代码提供解决方案......如果可能的话,我会接受你的回答......给它一两天!
  • 在此内容中,您没有与新闻扩展的连接。您可以尝试通过ObjectManager 初始化NewsRepository 并获得想要的消息。但是你也得到了模型的所有其他属性。因此,为此目的,它有点开销。
【解决方案2】:

可能您最多只能在自己的存储库中进行自定义数据库查询,您可以在其中请求每个应用类别以获取标题。

您可以使用 tx_news 的存储库来避免冗余代码,但您肯定必须包含一些用于实例化请求的代码/函数——无论请求被发送到何处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多