0
votes

I'm new to Typo3. I want to add a custom content element with a specifc select field within this content element. For the custom content element, I modified the pageTSConfig with

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig and added my element. This is working. In order to store my content value, I added the "ext_tables.sql" file with

#
# Table structure for table 'tt_content'
#
CREATE TABLE tt_content (
    tx_spk_shopware_category varchar(20) NULL
);

and finally I override the TCA configuration for tt_content and this is not working. I see the new content element with my custom type with my items within the config, but I cannot modify / add any items in the class referenced in itemsProcFunc.

tt_content.php:

<?php declare(strict_types=1);

defined('TYPO3_MODE') || die();

// static TypoScript
(static function () {
    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPlugin(
        array(
            'LLL:EXT:spk_shopware/Resources/Private/Language/Tca.xlf:spk_shopware_category.wizard.title',
            'spk_shopware_category',
            'EXT:spk_shopware/Resources/Public/Icons/ContentElements/spk_shopware_category.gif'
        ),
        'CType',
        'spk_shopware'
    );

    $temporaryColumn = array(
        'tx_spk_shopware_category' => array (
            'exclude' => 1,
            'label' => 'LLL:EXT:spk_shopware/Resources/Private/Language/Tca.xlf:spk_shopware_category.title',
            'config' => array (
                'type' => 'select',
                'itemsProcFunc' => SPK\Shopware\Hook\ShopwareCategorySelect::class . '->listAvailableShopwareCategories',
                'items' => array(
                    array('test 1', '8'),
                    array('test 2', '10'),
                ),
                'maxitems' => 1,
                'minitems' => 1,
                'required' => true,
            )
        )
    );

    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
        'tt_content',
        $temporaryColumn
    );
})();


// Configure the default backend fields for the content element
$GLOBALS['TCA']['tt_content']['types']['spk_shopware_category'] = array(
    'showitem' => '
         --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xml:palette.general;general,
         tx_spk_shopware_category
');

CategorySelect.php

<?php

namespace SPK\Shopware\Hook;

class ShopwareCategorySelect
{
    public function listAvailableShopwareCategories(&$config)
    {
        $config['items'][] = ["Tim", 0];
        $config['items'][] = ["Tom", 1];
        $config['items'][] = ["Jerry", 2];
        array_push($config['items'], ["as", 1]);
        return $config;
    }
}

I see the custom element and new select field, but only with the test elements in the config. I don't know what I'm missing.

2
Are you sure, TYPO3 "knows" your Class? Have you some kind of autoloading information (composer.json or an autoload section in ext_emconf.php)? - Julian Hofmann
@JulianHofmann I added the following part within the ext_emconf.php: 'autoload' => [ 'psr-4' => ['SPK\\Shopware\\' => 'Classes/'] ] - mys3lF
Ehm, your snippets describe class ShopwareCategorySelect is in file CategorySelect.php - that's not, how autoloading can work. Classname and filename should be the same. - Julian Hofmann
@JulianHofmann sorry. This was my fault. The file name is ShopwareCategorySelect.php and my Classname is also ShopwareCategorySelect. - mys3lF
@JulianHofmann it was the autoloading part. I worked on a cloned website which had no composer file.It worked after I added the composer file and adjusted the autoloading part. Would you like to write an answer. I could accept it as the solution. Danke nochmal dafür. - mys3lF

2 Answers

0
votes

You do not really need to return anything. You just want to modify the items of your column. Try the following:

public function listAvailableShopwareCategories(&$config)
{
    $config['items'][] = ["Tim", 0];
    $config['items'][] = ["Tom", 1];
    $config['items'][] = ["Jerry", 2];
   
    $this->getItems = $config['items'];
}

You modify the &$config by adding the items just before it is rendered on your TCA. That's the reason why your PHP file is under the Hooks folder. It just hooks the items between the response :)

0
votes

Seems, TYPO3 does not find your class. Please check if you have provided appropriate information:

via composer.json, section "autoload":

{
    "name": "vendorname/my-extension",
    "type": "typo3-cms-extension",
    // ...
    "autoload": {
        "psr-4": {
            "Vendorname\\MyExtension\\": "Classes/"
        }
    }
}

...or via ext_emconf.php, section "autoload":

<?php
    $EM_CONF[$_EXTKEY] = [
        'title' => 'My Extension',
        // ...
        'autoload' => [
            'psr-4' => [
                'Vendorname\\MyExtension\\' => 'Classes'
            ]
        ]
    ];