model-view-controller - 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;
}
}
这显然给出了所选类别的数量......我没有包括我的任何尝试,因为它们没有任何结果......
解决方案
您可以进行 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 中测试
推荐阅读
- ios - 将 TableView 点击位置转换为单元格视图位置
- javascript - Jquery 到 Pure Vanilla JS
- ios - Appcelerator - 在 Facebook 上分享链接
- azure-service-fabric - Azure Service Fabric 部署
- database-schema - 创建 ERD 和关系模式
- ruby-on-rails - 使用 ruby on rails 执行 SQL 命令时遇到问题
- python - 使用 cnn 的 TensorFlow 对象检测 API
- php - PHP 小时与小时之间的差异
- ruby - 使用 Ruby OpenSSL 从私钥获取椭圆曲线公钥
- swift - 自动布局遇到问题 - 为什么在堆栈视图下添加标签时会出错?