Search code examples
model-view-controllertypo3extbasetx-news

TYPO3: tx_news get sys_category sys_categories for custom title in BE list


I extended tx_news to hold a number of courses. there are courses that treat the same subject matter for different arguments (which I select as sys_categories). This means that their title is identical, now I'm trying to make the list better for the editor by including the selected category in the list...

Imply a custom title in Configuration/TCA/Overrides/tx_news_domain_model_news.php:

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

The userfunction so far 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;
    }
}

which obviously gives the number of selected categories ... I did not include any of my attempts because they lead to nothing ...


Solution

  • You can make an mm query to resolve the assigned category title:

    <?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) .')';
          }
        }
      }
    

    Note: This code is was tested in TYPO3 8.7.12