Typoscript: sys_categories der Seite auslesen.

UID der sys_category auslesen

Seit TYPO3 8 LTS hat sich scheinbar etwas geändert. Meine bisherigen Scripte zum Auslesen der sys_categories funktionieren nicht mehr. Nun habe ich ein neues:

 

lib {
    categories = CONTENT
    categories {
        table = sys_category
        select {
            pidInList = 1
            selectFields = description
            join = sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid
            where.data =field:recordUid
            where.intval = 1
            where.wrap = uid_foreign=|
        }
        renderObj = COA
        renderObj {
            1 = TEXT
            1.field = description
        }
    }
}

 

Der Wert für die "where"-Abfrage kommt aus dem Fluid über den data-Parameter:  

 

<f:cObject typoscriptObjectPath="lib.categories" data="{recordUid: data.uid}" />
{f:cObject(typoscriptObjectPath: 'lib.categories', data:'{recordUid: data.uid}')}

 

ACHTUNG: Das Ganze funktioniert nur, wenn die Kategorien auf der Root-Seite liegen. Ich hoffe, dass sich das in Version 9 ändert.

Category UID: Alle Informationen der Kategorie abrufen

Der oben beschrieben Weg gibt die ID der Kategorie aus. Möchte man aber Informationen wie Beschreibung oder Titel kommt man so nicht weiter. Hierzu habe ich eine Viewhelper-Bastelanleitung gesfunden:

Viewhelper:

 

<?php
namespace Fischhase\FhViewhelpers\ViewHelpers;
/*namespace ID\Simpletemplates\ViewHelpers;*/
/*
 *  The MIT License (MIT)
 *
 *  Copyright (c) 2017 INGENIUMDESIGN, www.ingeniumdesign.de
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

/*
 * <html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" xmlns:id="http://typo3.org/ns/ID/Simpletemplates/ViewHelpers" data-namespace-typo3-fluid="true">
 *
 *
 *
 * // get everything with parent = 1 (array would work too)
 * <id:SysCategory uid="1" byParentId="true" />
 * <f:debug>{sysCategoryDetailArray}</f:debug>
 *
 * // by single ID
 * <id:SysCategory uid="1" />
 * <f:debug>{sysCategoryDetailArray}</f:debug>
 *
 * // by specific IDs
 * <id:SysCategory uid="{0: 1, 1: 2, 2:3}" />
 * <f:debug>{sysCategoryDetailArray}</f:debug>
 *
 *
 * // everything:
 * <id:SysCategory />
 * <f:debug>{sysCategoryDetailArray}</f:debug>
 *
 */

class SysCategoryViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
     * @params integer $uid (CE)
     * @params string $tableCell (of sys_category)
     * @return CategorieName
     * @author INGENIUMDESIGN
     */
    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);
    }

}

 

Dataprocessing Angaben im SETUP:

 

lib.contentElement.dataProcessing {
   999 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
   999 {
      if.isTrue.field = categories
      table = sys_category
      selectFields = sys_category.*
      pidInList = this
      recursive = 999
      join = sys_category_record_mm ON sys_category.uid = sys_category_record_mm.uid_local
      where.data = field:uid
      where.wrap = sys_category_record_mm.tablenames = 'tt_content' and sys_category_record_mm.uid_foreign='|'
      orderBy = sys_category.sorting
      as = categories
   }
}

 

Im Fluid wird es dann so verwendet. Der Viehelper verwendet die lib.category von oben. Achtung: Ihr müsst im RenderObj den Wrap entfernen. Nun kommt ein Array heraus, dass Ihr verwenden könnt:

 

<fh:SysCategory uid="{f:cObject(typoscriptObjectPath:'lib.categories', data:{recordUid: data.uid})}" />
<div class="logo {sysCategoryDetailArray.0.description}"></div>

 

Quelle: http://www.typo3-probleme.de/2017/09/08/typo3-sys-category-via-typoscript-und-fluid-viewhelper-auslesen-2129/