weiss
weiss

Reputation: 29

Output page category in Fluid using Viewhelper in TYPO3 11.5

I am creating a little news page for our website which includes a content element to fetch subpages by using an menu array. Therefore i have assigned a category to each article. But i am starting to become really desperate in finding a solution to output the category assigned to a page in TYPO3 for the sys_category table.

I have tried multiple solutions like SysCategoryViewHelper by Sebastian Schmal or the CategoriesOutput ViewHelper by Hagen Gebauer or various TS solutions – but i always end up failing.

Either the ViewHelper doesn't register a variable (and i have named everything correctly) or I get an array but i can't output the title of the category corresponding to the individual page.

Here is the (cleaned up) code for the news section i have so far:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" xmlns:e="http://typo3.org/ns/Evocan/EvocanTemplate/ViewHelpers" data-namespace-typo3-fluid="true">

<f:layout name="Default" />
<f:section name="Main">

<f:if condition="{menu}">
  <f:for each="{menu}" as="page">

<article href="{page.link}"{f:if(condition: page.target, then: ' target="{page.target}"')} title="{page.title}"></a>
 <h3>{page.title}</h3>
  <p>{page.data.abstract}</p>
 
    <f:for each="{e:SysCategory(uid: page.data.uid)}" as="category">
    <span>{category.title}</span>
    </f:for>
            
</article>
</f:for>
</f:if>

</f:section>
</html>

In the current setup i have so far, i get sysCategoryDetailArray which shows an "array" of each category with its title for example. But the output in the fluid code is empty.

This is the ViewHelper i am using at the moment:

<?php
namespace Evocan\EvocanTemplate\ViewHelpers;
 
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
 

class SysCategoryViewHelper extends AbstractViewHelper {
 
     /**@param integer $uid (CE)
     @param string $tableCell (of sys_category)
     @return CategorieName*/


    public function initializeArguments() {
        $this->registerArgument('uid', 'integer', 'enthaelt die UID des CE', TRUE);
        /* $this->registerArgument('tableCell', 'string', 'enthaelt das Tabellenelement aus sys_category', TRUE);
        $this->registerArgument('table', 'string', 'enthaelt den Tabellennamen aus sys_category_record_mm', TRUE); */
    }
 
    /**
    * @param mixed $uid
    * @param boolean $byParentId
    */

    public function render($uid = null, $byParentId = false) {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
        $query = $queryBuilder
            ->select('*')
            ->from('sys_category');
         
        if ($uid !== null) {
            $uid = is_array($uid) ? $uid : [$uid];
             
            $query->where(
                $queryBuilder->expr()->in(
                    $byParentId ? 'parent' : 'uid',
                    $uid
                )
            );
        }
         
        $result = $query->execute();
        $res = [];
        while ($row = $result->fetch()) {
            $res[] = $row;
        }
         
        $this->templateVariableContainer->add('sysCategoryDetailArray', $res);
    }
 
}

This is the generated menu in typoscript for the news content element:

news =< lib.contentElement
news {
    templateName = News
    dataProcessing {
        140 = TYPO3\CMS\Frontend\DataProcessing\MenuProcessor
        140 {
            special = directory
            includeNotInMenu = 1
            special.value.field = pages
        }
        150 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
        150 {
            references.fieldName = image
        }
    }
}

I already tried different versions of fluid code like <id:SysCategory uid="{page.data.uid}" /> like in the ViewHelper file.

I would be really grateful if someone could point me in the right direction!

Upvotes: 0

Views: 654

Answers (1)

weiss
weiss

Reputation: 29

I think i have found a much simpler solution: Instead of using the system categories i will create subpages as categories and use the title of each subpage as category name (because i only need one category per news article). This will save me a lot of effort.

Upvotes: 0

Related Questions