简体   繁体   中英

TYPO3 foreign_table: How to specify a specific column to a select drop down menu?

I want to add records in the TYPO3 backend. For that I made an extension containing 4 different classes. While adding a record of one specific class to a folder via the backend I want to have a select that lets me chose from items of a column from another class. I know I have to use a foreign_table for this like in this code:

 'kundeuid' => array(
                   'exclude' => 1,
                   'label' => 'LLL:EXT:icingaconfgen/Resources/Private/Language/locallang_db.xlf:tx_icingaconfgen_domain_model_appliance.kundeuid',
                   'config' => array(
                                   'type' => 'select',
                                   'renderType' => 'selectSingle',
                                   'foreign_table' => 'tx_icingaconfgen_domain_model_kunde',
                                   'foreign_table_where' => 'ORDER BY tx_icingaconfgen_domain_model_kunde.kundeuid asc',
                                   'items' => array(
                                                   array('-- Select Kunde --', 0),
                                   ),
                                   'size' => 1,
                                   'maxitems' => 1
                   ),
           ),

But the problem is I can't specify that the selection should only include properties of a determined column. Instead the select drop down menu seems to use properties of the very first column. How can I specify the column of the 'kundeuid' property?

Edit: I forgot to mention, the code is used in the specific PHP file in the TCA folder.

Creating a select from field values of another table

You can fill the select with whatever you want if you implement a PHP method that provides the content of the select box. For this you have to use itemsProcFunc .

Could look like this:

'kundeuid' => [
     'config' => [
        'type' => 'select',
        'renderType' => 'selectSingle',
        'itemsProcFunc' => 'Vendor\Ext\ItemsProcFunc\KundeUid->fetchItems',
    ],
]

In your method you can then fill the items array with the values from your kunde table (look in the database yourself):

/**
 * Add two items to existing ones
 *
 * @param $params
 */
public function fetchItems(&$params)
{
    $params['items'][] = ['customer01 (label)', 'customer01'];
    $params['items'][] = ['customer02 (label)', 'customer02'];
}

Note that $params is passed by reference and the method does not need to return anything. Also whatever you write in your TCA config into items will be there as well. Whatever you select, the value of that option will be stored in yourdatabase field.

Creating a relation

If you use the 'foreign_table' in your TCA conf then you will create a relation to the foreign table, wich always means you are storing the uid of the selected record in your field (unless you are using a MM table).

In TYPO3 every recods has a uid that is on auto_increment in the database and therefore unique in each table. The following TCA configuration

'kundeuid' => [
     'config' => [
        'type' => 'select',
        'renderType' => 'selectSingle',
        'foreign_table' => 'tx_icingaconfgen_domain_model_kunde',
    ],
]

will let you choose your kunde records in a select box and store its uid in the field kundeuid . So if you let the property mapper resolve this dependency in your PHP code (or let extbase do it for you, if you use the repository and domain models), you will end up with something like this:

$appliance = $this->applianceRepository->findBySomething('something');
$kunde = $appliance->getKundeuid();
$kundeuid = $kunde->getKundeuid();

Therefore I would recommend that you rename the field, that the TCA configuration was for to something like client , So that the PHP code becomes a bit less confusing.

$appliance = $this->applianceRepository->findBySomething('something');
$client = $appliance->getClient();
$kundeuid = $client->getKundeuid();

When you will set property label to Kunde 's name table field in ctrl section in Kunde record in TCA you should see it also in Appliance . Maybe other properties label_ * will be useful for you. Link to ctrl documentation: TCA reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM